Debian Codegraph @

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

raw
1 /*
2 Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com>
3 This file is part of GlusterFS.
4
5 This file is licensed to you under your choice of the GNU Lesser
6 General Public License, version 3 or any later version (LGPLv3 or
7 later), or the GNU General Public License, version 2 (GPLv2), in all
8 cases as published by the Free Software Foundation.
9 */
10
11 #include <libgen.h>
12 #include <unistd.h>
13 #include <fnmatch.h>
14 #include <sys/time.h>
15 #include <stdlib.h>
16 #include <signal.h>
17
18 #include <urcu/uatomic.h>
19
20 #include "libxlator.h" // for gf_get_max_stime()
21 #include <glusterfs/dict.h>
22 #include <glusterfs/logging.h>
23 #include <glusterfs/list.h>
24 #include <glusterfs/compat-errno.h>
25 #include <glusterfs/compat.h>
26 #include <glusterfs/quota-common-utils.h>
27
28 #include "afr-transaction.h"
29 #include "afr-messages.h"
30
31 /*
32 * Quota size xattrs are not maintained by afr. There is a
33 * possibility that they differ even when both the directory changelog xattrs
34 * suggest everything is fine. So if there is at least one 'source' check among
35 * the sources which has the maximum quota size. Otherwise check among all the
36 * available ones for maximum quota size. This way if there is a source and
37 * stale copies it always votes for the 'source'.
38 * */
39
40 int
41 afr_handle_quota_size(afr_local_t *local, xlator_t *this)
42 {
43 unsigned char *readable = NULL;
44 afr_private_t *priv = NULL;
45 struct afr_reply *replies = NULL;
46 int i = 0;
47 int ret = 0;
48 quota_meta_t size = {
49 0,
50 };
51 quota_meta_t max_size = {
52 0,
53 };
54 int readable_cnt = 0;
55 int read_subvol = -1;
56
57 priv = this->private;
58 replies = local->replies;
59
60 readable = alloca0(priv->child_count);
61
62 afr_inode_read_subvol_get(local->inode, this, readable, 0, 0);
63
64 readable_cnt = AFR_COUNT(readable, priv->child_count);
65
66 for (i = 0; i < priv->child_count; i++) {
67 if (!replies[i].valid || replies[i].op_ret == -1)
68 continue;
69 if (readable_cnt && !readable[i])
70 continue;
71 if (!replies[i].xdata)
72 continue;
73 ret = quota_dict_get_meta(replies[i].xdata, QUOTA_SIZE_KEY,
74 SLEN(QUOTA_SIZE_KEY), &size);
75 if (ret == -1)
76 continue;
77 if (read_subvol == -1)
78 read_subvol = i;
79 if (size.size > max_size.size ||
80 (size.file_count + size.dir_count) >
81 (max_size.file_count + max_size.dir_count))
82 read_subvol = i;
83
84 if (size.size > max_size.size)
85 max_size.size = size.size;
86 if (size.file_count > max_size.file_count)
87 max_size.file_count = size.file_count;
88 if (size.dir_count > max_size.dir_count)
89 max_size.dir_count = size.dir_count;
90 }
91
92 if (max_size.size == 0 && max_size.file_count == 0 &&
93 max_size.dir_count == 0)
94 return read_subvol;
95
96 for (i = 0; i < priv->child_count; i++) {
97 if (!replies[i].valid || replies[i].op_ret == -1)
98 continue;
99 if (readable_cnt && !readable[i])
100 continue;
101 if (!replies[i].xdata)
102 continue;
103 quota_dict_set_meta(replies[i].xdata, QUOTA_SIZE_KEY, &max_size,
104 IA_IFDIR);
105 }
106
107 return read_subvol;
108 }
109
110 /* {{{ access */
111
112 static int
113 afr_access_cbk(call_frame_t *frame, void *cookie, xlator_t *this, int op_ret,
114 int op_errno, dict_t *xdata)
115 {
116 afr_local_t *local = NULL;
117
118 local = frame->local;
119
120 if (op_ret < 0) {
121 local->op_ret = op_ret;
122 local->op_errno = op_errno;
123
124 afr_read_txn_continue(frame, this, (long)cookie);
125 return 0;
126 }
127
128 AFR_STACK_UNWIND(access, frame, op_ret, op_errno, xdata);
129
130 return 0;
131 }
132
133 static int
134 afr_access_wind(call_frame_t *frame, xlator_t *this, int subvol)
135 {
136 afr_private_t *priv = NULL;
137 afr_local_t *local = NULL;
138
139 priv = this->private;
140 local = frame->local;
141
142 if (subvol == -1) {
143 AFR_STACK_UNWIND(access, frame, local->op_ret, local->op_errno, 0);
144 return 0;
145 }
146
147 STACK_WIND_COOKIE(frame, afr_access_cbk, (void *)(long)subvol,
148 priv->children[subvol],
149 priv->children[subvol]->fops->access, &local->loc,
150 local->cont.access.mask, local->xdata_req);
151 return 0;
152 }
153
154 int
155 afr_access(call_frame_t *frame, xlator_t *this, loc_t *loc, int mask,
156 dict_t *xdata)
157 {
158 afr_local_t *local = NULL;
159 int op_errno = 0;
160
161 local = AFR_FRAME_INIT(frame, op_errno);
162 if (!local)
163 goto out;
164
165 local->op = GF_FOP_ACCESS;
166 loc_copy(&local->loc, loc);
167 local->cont.access.mask = mask;
168 if (xdata)
169 local->xdata_req = dict_ref(xdata);
170
171 afr_read_txn(frame, this, loc->inode, afr_access_wind,
172 AFR_METADATA_TRANSACTION);
173
174 return 0;
175 out:
176 AFR_STACK_UNWIND(access, frame, -1, op_errno, NULL);
177
178 return 0;
179 }
180
181 /* }}} */
182
183 /* {{{ stat */
184
185 static int
186 afr_stat_cbk(call_frame_t *frame, void *cookie, xlator_t *this, int32_t op_ret,
187 int32_t op_errno, struct iatt *buf, dict_t *xdata)
188 {
189 afr_local_t *local = NULL;
190
191 local = frame->local;
192
193 if (op_ret < 0) {
194 local->op_ret = op_ret;
195 local->op_errno = op_errno;
196
197 afr_read_txn_continue(frame, this, (long)cookie);
198 return 0;
199 }
200
201 AFR_STACK_UNWIND(stat, frame, op_ret, op_errno, buf, xdata);
202
203 return 0;
204 }
205
206 static int
207 afr_stat_wind(call_frame_t *frame, xlator_t *this, int subvol)
208 {
209 afr_private_t *priv = NULL;
210 afr_local_t *local = NULL;
211
212 priv = this->private;
213 local = frame->local;
214
215 if (subvol == -1) {
216 AFR_STACK_UNWIND(stat, frame, local->op_ret, local->op_errno, 0, 0);
217 return 0;
218 }
219
220 STACK_WIND_COOKIE(
221 frame, afr_stat_cbk, (void *)(long)subvol, priv->children[subvol],
222 priv->children[subvol]->fops->stat, &local->loc, local->xdata_req);
223 return 0;
224 }
225
226 int
227 afr_stat(call_frame_t *frame, xlator_t *this, loc_t *loc, dict_t *xdata)
228 {
229 afr_local_t *local = NULL;
230 int op_errno = 0;
231
232 local = AFR_FRAME_INIT(frame, op_errno);
233 if (!local)
234 goto out;
235
236 local->op = GF_FOP_STAT;
237 loc_copy(&local->loc, loc);
238 if (xdata)
239 local->xdata_req = dict_ref(xdata);
240
241 afr_read_txn(frame, this, loc->inode, afr_stat_wind, AFR_DATA_TRANSACTION);
242
243 return 0;
244 out:
245 AFR_STACK_UNWIND(stat, frame, -1, op_errno, NULL, NULL);
246
247 return 0;
248 }
249
250 /* }}} */
251
252 /* {{{ fstat */
253
254 static int
255 afr_fstat_cbk(call_frame_t *frame, void *cookie, xlator_t *this, int32_t op_ret,
256 int32_t op_errno, struct iatt *buf, dict_t *xdata)
257 {
258 afr_local_t *local = NULL;
259
260 local = frame->local;
261
262 if (op_ret < 0) {
263 local->op_ret = op_ret;
264 local->op_errno = op_errno;
265
266 afr_read_txn_continue(frame, this, (long)cookie);
267 return 0;
268 }
269
270 AFR_STACK_UNWIND(fstat, frame, op_ret, op_errno, buf, xdata);
271
272 return 0;
273 }
274
275 static int
276 afr_fstat_wind(call_frame_t *frame, xlator_t *this, int subvol)
277 {
278 afr_private_t *priv = NULL;
279 afr_local_t *local = NULL;
280
281 priv = this->private;
282 local = frame->local;
283
284 if (subvol == -1) {
285 AFR_STACK_UNWIND(fstat, frame, local->op_ret, local->op_errno, 0, 0);
286 return 0;
287 }
288
289 STACK_WIND_COOKIE(
290 frame, afr_fstat_cbk, (void *)(long)subvol, priv->children[subvol],
291 priv->children[subvol]->fops->fstat, local->fd, local->xdata_req);
292 return 0;
293 }
294
295 int32_t
296 afr_fstat(call_frame_t *frame, xlator_t *this, fd_t *fd, dict_t *xdata)
297 {
298 afr_local_t *local = NULL;
299 int op_errno = 0;
300
301 AFR_ERROR_OUT_IF_FDCTX_INVALID(fd, this, op_errno, out);
302 local = AFR_FRAME_INIT(frame, op_errno);
303 if (!local)
304 goto out;
305
306 local->op = GF_FOP_FSTAT;
307 local->fd = fd_ref(fd);
308 if (xdata)
309 local->xdata_req = dict_ref(xdata);
310
311 afr_fix_open(fd, this);
312
313 afr_read_txn(frame, this, fd->inode, afr_fstat_wind, AFR_DATA_TRANSACTION);
314
315 return 0;
316 out:
317 AFR_STACK_UNWIND(fstat, frame, -1, op_errno, NULL, NULL);
318
319 return 0;
320 }
321
322 /* }}} */
323
324 /* {{{ readlink */
325
326 static int
327 afr_readlink_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
328 int32_t op_ret, int32_t op_errno, const char *buf,
329 struct iatt *sbuf, dict_t *xdata)
330 {
331 afr_local_t *local = NULL;
332
333 local = frame->local;
334
335 if (op_ret < 0) {
336 local->op_ret = -1;
337 local->op_errno = op_errno;
338
339 afr_read_txn_continue(frame, this, (long)cookie);
340 return 0;
341 }
342
343 AFR_STACK_UNWIND(readlink, frame, op_ret, op_errno, buf, sbuf, xdata);
344 return 0;
345 }
346
347 static int
348 afr_readlink_wind(call_frame_t *frame, xlator_t *this, int subvol)
349 {
350 afr_local_t *local = NULL;
351 afr_private_t *priv = NULL;
352
353 local = frame->local;
354 priv = this->private;
355
356 if (subvol == -1) {
357 AFR_STACK_UNWIND(readlink, frame, local->op_ret, local->op_errno, 0, 0,
358 0);
359 return 0;
360 }
361
362 STACK_WIND_COOKIE(frame, afr_readlink_cbk, (void *)(long)subvol,
363 priv->children[subvol],
364 priv->children[subvol]->fops->readlink, &local->loc,
365 local->cont.readlink.size, local->xdata_req);
366 return 0;
367 }
368
369 int
370 afr_readlink(call_frame_t *frame, xlator_t *this, loc_t *loc, size_t size,
371 dict_t *xdata)
372 {
373 afr_local_t *local = NULL;
374 int32_t op_errno = 0;
375
376 local = AFR_FRAME_INIT(frame, op_errno);
377 if (!local)
378 goto out;
379
380 local->op = GF_FOP_READLINK;
381 loc_copy(&local->loc, loc);
382 local->cont.readlink.size = size;
383 if (xdata)
384 local->xdata_req = dict_ref(xdata);
385
386 afr_read_txn(frame, this, loc->inode, afr_readlink_wind,
387 AFR_DATA_TRANSACTION);
388
389 return 0;
390 out:
391 AFR_STACK_UNWIND(readlink, frame, -1, op_errno, 0, 0, 0);
392
393 return 0;
394 }
395
396 /* }}} */
397
398 /* {{{ getxattr */
399
400 struct _xattr_key {
401 char *key;
402 struct list_head list;
403 };
404
405 static int
406 __gather_xattr_keys(dict_t *dict, char *key, data_t *value, void *data)
407 {
408 struct list_head *list = data;
409 struct _xattr_key *xkey = NULL;
410
411 if (!strncmp(key, AFR_XATTR_PREFIX, SLEN(AFR_XATTR_PREFIX))) {
412 xkey = GF_MALLOC(sizeof(*xkey), gf_afr_mt_xattr_key);
413 if (!xkey)
414 return -1;
415
416 xkey->key = key;
417 INIT_LIST_HEAD(&xkey->list);
418
419 list_add_tail(&xkey->list, list);
420 }
421 return 0;
422 }
423
424 static void
425 afr_filter_xattrs(dict_t *dict)
426 {
427 struct list_head keys = {
428 0,
429 };
430 struct _xattr_key *key = NULL;
431 struct _xattr_key *tmp = NULL;
432
433 INIT_LIST_HEAD(&keys);
434
435 dict_foreach(dict, __gather_xattr_keys, (void *)&keys);
436
437 list_for_each_entry_safe(key, tmp, &keys, list)
438 {
439 dict_del(dict, key->key);
440
441 list_del_init(&key->list);
442
443 GF_FREE(key);
444 }
445 }
446
447 static gf_boolean_t
448 afr_getxattr_ignorable_errnos(int32_t op_errno)
449 {
450 if (op_errno == ENODATA || op_errno == ENOTSUP || op_errno == ERANGE ||
451 op_errno == ENAMETOOLONG)
452 return _gf_true;
453
454 return _gf_false;
455 }
456 static int
457 afr_getxattr_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
458 int32_t op_ret, int32_t op_errno, dict_t *dict, dict_t *xdata)
459 {
460 afr_local_t *local = NULL;
461
462 local = frame->local;
463
464 if (op_ret < 0 && !afr_getxattr_ignorable_errnos(op_errno)) {
465 local->op_ret = op_ret;
466 local->op_errno = op_errno;
467
468 afr_read_txn_continue(frame, this, (long)cookie);
469 return 0;
470 }
471
472 if (dict)
473 afr_filter_xattrs(dict);
474
475 AFR_STACK_UNWIND(getxattr, frame, op_ret, op_errno, dict, xdata);
476
477 return 0;
478 }
479
480 static int
481 afr_getxattr_wind(call_frame_t *frame, xlator_t *this, int subvol)
482 {
483 afr_local_t *local = NULL;
484 afr_private_t *priv = NULL;
485
486 local = frame->local;
487 priv = this->private;
488
489 if (subvol == -1) {
490 AFR_STACK_UNWIND(getxattr, frame, local->op_ret, local->op_errno, NULL,
491 NULL);
492 return 0;
493 }
494
495 STACK_WIND_COOKIE(frame, afr_getxattr_cbk, (void *)(long)subvol,
496 priv->children[subvol],
497 priv->children[subvol]->fops->getxattr, &local->loc,
498 local->cont.getxattr.name, local->xdata_req);
499 return 0;
500 }
501
502 static int32_t
503 afr_getxattr_unwind(call_frame_t *frame, int op_ret, int op_errno, dict_t *dict,
504 dict_t *xdata)
505
506 {
507 AFR_STACK_UNWIND(getxattr, frame, op_ret, op_errno, dict, xdata);
508 return 0;
509 }
510
511 static int32_t
512 afr_fgetxattr_clrlk_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
513 int32_t op_ret, int32_t op_errno, dict_t *dict,
514 dict_t *xdata)
515 {
516 afr_local_t *local = NULL;
517 afr_private_t *priv = NULL;
518 xlator_t **children = NULL;
519 dict_t *xattr = NULL;
520 char *tmp_report = NULL;
521 char lk_summary[1024] = {
522 0,
523 };
524 int serz_len = 0;
525 int32_t callcnt = 0;
526 long int cky = 0;
527 int ret = 0;
528 int keylen = 0;
529 int children_keylen = 0;
530
531 priv = this->private;
532 children = priv->children;
533
534 local = frame->local;
535 cky = (long)cookie;
536 keylen = strlen(local->cont.getxattr.name);
537 children_keylen = strlen(children[cky]->name);
538
539 LOCK(&frame->lock);
540 {
541 callcnt = --local->call_count;
542 if (op_ret == -1)
543 local->replies[cky].op_errno = op_errno;
544
545 if (!local->dict)
546 local->dict = dict_new();
547 if (local->dict) {
548 ret = dict_get_strn(dict, local->cont.getxattr.name, keylen,
549 &tmp_report);
550 if (ret)
551 goto unlock;
552 ret = dict_set_dynstrn(local->dict, children[cky]->name,
553 children_keylen, gf_strdup(tmp_report));
554 if (ret)
555 goto unlock;
556 }
557 }
558 unlock:
559 UNLOCK(&frame->lock);
560
561 if (!callcnt) {
562 xattr = dict_new();
563 if (!xattr) {
564 op_ret = -1;
565 op_errno = ENOMEM;
566 goto unwind;
567 }
568 ret = dict_serialize_value_with_delim(local->dict, lk_summary,
569 &serz_len, '\n');
570 if (ret) {
571 op_ret = -1;
572 op_errno = ENOMEM;
573 goto unwind;
574 }
575 if (serz_len == -1)
576 snprintf(lk_summary, sizeof(lk_summary), "No locks cleared.");
577 ret = dict_set_dynstrn(xattr, local->cont.getxattr.name, keylen,
578 gf_strdup(lk_summary));
579 if (ret) {
580 op_ret = -1;
581 op_errno = ENOMEM;
582 gf_msg(this->name, GF_LOG_ERROR, ENOMEM, AFR_MSG_DICT_SET_FAILED,
583 "Error setting dictionary");
584 goto unwind;
585 }
586
587 op_errno = afr_final_errno(local, priv);
588
589 unwind:
590 AFR_STACK_UNWIND(fgetxattr, frame, op_ret, op_errno, xattr, xdata);
591 if (xattr)
592 dict_unref(xattr);
593 }
594
595 return ret;
596 }
597
598 static int32_t
599 afr_getxattr_clrlk_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
600 int32_t op_ret, int32_t op_errno, dict_t *dict,
601 dict_t *xdata)
602 {
603 afr_local_t *local = NULL;
604 afr_private_t *priv = NULL;
605 xlator_t **children = NULL;
606 dict_t *xattr = NULL;
607 char *tmp_report = NULL;
608 char lk_summary[1024] = {
609 0,
610 };
611 int serz_len = 0;
612 int32_t callcnt = 0;
613 long int cky = 0;
614 int ret = 0;
615 int keylen = 0;
616 int children_keylen = 0;
617
618 priv = this->private;
619 children = priv->children;
620
621 local = frame->local;
622 cky = (long)cookie;
623
624 keylen = strlen(local->cont.getxattr.name);
625 children_keylen = strlen(children[cky]->name);
626
627 LOCK(&frame->lock);
628 {
629 callcnt = --local->call_count;
630 if (op_ret == -1)
631 local->replies[cky].op_errno = op_errno;
632
633 if (!local->dict)
634 local->dict = dict_new();
635 if (local->dict) {
636 ret = dict_get_strn(dict, local->cont.getxattr.name, keylen,
637 &tmp_report);
638 if (ret)
639 goto unlock;
640 ret = dict_set_dynstrn(local->dict, children[cky]->name,
641 children_keylen, gf_strdup(tmp_report));
642 if (ret)
643 goto unlock;
644 }
645 }
646 unlock:
647 UNLOCK(&frame->lock);
648
649 if (!callcnt) {
650 xattr = dict_new();
651 if (!xattr) {
652 op_ret = -1;
653 op_errno = ENOMEM;
654 goto unwind;
655 }
656 ret = dict_serialize_value_with_delim(local->dict, lk_summary,
657 &serz_len, '\n');
658 if (ret) {
659 op_ret = -1;
660 op_errno = ENOMEM;
661 goto unwind;
662 }
663 if (serz_len == -1)
664 snprintf(lk_summary, sizeof(lk_summary), "No locks cleared.");
665 ret = dict_set_dynstrn(xattr, local->cont.getxattr.name, keylen,
666 gf_strdup(lk_summary));
667 if (ret) {
668 op_ret = -1;
669 op_errno = ENOMEM;
670 gf_msg(this->name, GF_LOG_ERROR, ENOMEM, AFR_MSG_DICT_SET_FAILED,
671 "Error setting dictionary");
672 goto unwind;
673 }
674
675 op_errno = afr_final_errno(local, priv);
676
677 unwind:
678 AFR_STACK_UNWIND(getxattr, frame, op_ret, op_errno, xattr, xdata);
679
680 if (xattr)
681 dict_unref(xattr);
682 }
683
684 return ret;
685 }
686
687 /**
688 * node-uuid cbk uses next child querying mechanism
689 */
690 static int32_t
691 afr_getxattr_node_uuid_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
692 int32_t op_ret, int32_t op_errno, dict_t *dict,
693 dict_t *xdata)
694 {
695 afr_private_t *priv = NULL;
696 afr_local_t *local = NULL;
697 xlator_t **children = NULL;
698 int unwind = 1;
699 int curr_call_child = 0;
700
701 priv = this->private;
702 children = priv->children;
703
704 local = frame->local;
705
706 if (op_ret == -1) { /** query the _next_ child */
707
708 /**
709 * _current_ becomes _next_
710 * If done with all children and yet no success; give up !
711 */
712 curr_call_child = (int)((long)cookie);
713 if (++curr_call_child == priv->child_count)
714 goto unwind;
715
716 gf_msg_debug(this->name, op_errno,
717 "op_ret (-1): Re-querying afr-child (%d/%d)",
718 curr_call_child, priv->child_count);
719
720 unwind = 0;
721 STACK_WIND_COOKIE(
722 frame, afr_getxattr_node_uuid_cbk, (void *)(long)curr_call_child,
723 children[curr_call_child],
724 children[curr_call_child]->fops->getxattr, &local->loc,
725 local->cont.getxattr.name, local->xdata_req);
726 }
727
728 unwind:
729 if (unwind)
730 AFR_STACK_UNWIND(getxattr, frame, op_ret, op_errno, dict, xdata);
731
732 return 0;
733 }
734
735 /**
736 * list-node-uuids cbk returns the list of node_uuids for the subvolume.
737 */
738 static int32_t
739 afr_getxattr_list_node_uuids_cbk(call_frame_t *frame, void *cookie,
740 xlator_t *this, int32_t op_ret,
741 int32_t op_errno, dict_t *dict, dict_t *xdata)
742 {
743 afr_local_t *local = NULL;
744 afr_private_t *priv = NULL;
745 int32_t callcnt = 0;
746 int ret = 0;
747 char *xattr_serz = NULL;
748 long cky = 0;
749 int32_t tlen = 0;
750
751 local = frame->local;
752 priv = this->private;
753 cky = (long)cookie;
754
755 LOCK(&frame->lock);
756 {
757 callcnt = --local->call_count;
758 local->replies[cky].valid = 1;
759 local->replies[cky].op_ret = op_ret;
760 local->replies[cky].op_errno = op_errno;
761
762 if (op_ret < 0)
763 goto unlock;
764
765 local->op_ret = 0;
766
767 if (!local->xdata_rsp && xdata)
768 local->xdata_rsp = dict_ref(xdata);
769 local->replies[cky].xattr = dict_ref(dict);
770 }
771
772 unlock:
773 UNLOCK(&frame->lock);
774
775 if (!callcnt) {
776 if (local->op_ret != 0) {
777 /* All bricks gave an error. */
778 local->op_errno = afr_final_errno(local, priv);
779 goto unwind;
780 }
781
782 /*Since we store the UUID0_STR as node uuid for down bricks and
783 *for non zero op_ret, assigning length to priv->child_count
784 *number of uuids*/
785 local->cont.getxattr.xattr_len = (SLEN(UUID0_STR) + 2) *
786 priv->child_count;
787
788 if (!local->dict)
789 local->dict = dict_new();
790 if (!local->dict) {
791 local->op_ret = -1;
792 local->op_errno = ENOMEM;
793 goto unwind;
794 }
795
796 xattr_serz = GF_CALLOC(local->cont.getxattr.xattr_len, sizeof(char),
797 gf_common_mt_char);
798
799 if (!xattr_serz) {
800 local->op_ret = -1;
801 local->op_errno = ENOMEM;
802 goto unwind;
803 }
804
805 ret = afr_serialize_xattrs_with_delimiter(
806 frame, this, xattr_serz, local->cont.getxattr.xattr_len, UUID0_STR,
807 &tlen, ' ');
808 if (ret) {
809 local->op_ret = -1;
810 local->op_errno = ENOMEM;
811 GF_FREE(xattr_serz);
812 goto unwind;
813 }
814 ret = dict_set_dynstr_sizen(local->dict, GF_XATTR_LIST_NODE_UUIDS_KEY,
815 xattr_serz);
816 if (ret) {
817 gf_msg(this->name, GF_LOG_ERROR, -ret, AFR_MSG_DICT_SET_FAILED,
818 "Cannot set node_uuid key in dict");
819 local->op_ret = -1;
820 local->op_errno = ENOMEM;
821 if (ret == -EINVAL)
822 GF_FREE(xattr_serz);
823 } else {
824 local->op_ret = local->cont.getxattr.xattr_len - 1;
825 local->op_errno = 0;
826 }
827
828 unwind:
829 AFR_STACK_UNWIND(getxattr, frame, local->op_ret, local->op_errno,
830 local->dict, local->xdata_rsp);
831 }
832
833 return ret;
834 }
835
836 static int32_t
837 afr_getxattr_quota_size_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
838 int32_t op_ret, int32_t op_errno, dict_t *dict,
839 dict_t *xdata)
840 {
841 int idx = (long)cookie;
842 int call_count = 0;
843 afr_local_t *local = frame->local;
844 int read_subvol = -1;
845
846 local->replies[idx].valid = 1;
847 local->replies[idx].op_ret = op_ret;
848 local->replies[idx].op_errno = op_errno;
849 if (dict)
850 local->replies[idx].xdata = dict_ref(dict);
851 call_count = afr_frame_return(frame);
852 if (call_count == 0) {
853 local->inode = inode_ref(local->loc.inode);
854 read_subvol = afr_handle_quota_size(local, this);
855 if (read_subvol != -1) {
856 op_ret = local->replies[read_subvol].op_ret;
857 op_errno = local->replies[read_subvol].op_errno;
858 dict = local->replies[read_subvol].xdata;
859 }
860 AFR_STACK_UNWIND(getxattr, frame, op_ret, op_errno, dict, xdata);
861 }
862
863 return 0;
864 }
865
866 static int32_t
867 afr_update_local_dicts(call_frame_t *frame, dict_t *dict, dict_t *xdata)
868 {
869 afr_local_t *local;
870 dict_t *local_dict;
871 dict_t *local_xdata;
872 int32_t ret;
873
874 local = frame->local;
875 local_dict = NULL;
876 local_xdata = NULL;
877
878 ret = -ENOMEM;
879
880 if ((dict != NULL) && (local->dict == NULL)) {
881 local_dict = dict_new();
882 if (local_dict == NULL) {
883 goto done;
884 }
885 }
886
887 if ((xdata != NULL) && (local->xdata_rsp == NULL)) {
888 local_xdata = dict_new();
889 if (local_xdata == NULL) {
890 goto done;
891 }
892 }
893
894 if ((local_dict != NULL) || (local_xdata != NULL)) {
895 /* TODO: Maybe it would be better to preallocate both dicts before
896 * sending the requests. This way we don't need to use a LOCK()
897 * here. */
898 LOCK(&frame->lock);
899
900 if ((local_dict != NULL) && (local->dict == NULL)) {
901 local->dict = local_dict;
902 local_dict = NULL;
903 }
904
905 if ((local_xdata != NULL) && (local->xdata_rsp == NULL)) {
906 local->xdata_rsp = local_xdata;
907 local_xdata = NULL;
908 }
909
910 UNLOCK(&frame->lock);
911 }
912
913 if (dict != NULL) {
914 if (dict_copy(dict, local->dict) == NULL) {
915 goto done;
916 }
917 }
918
919 if (xdata != NULL) {
920 if (dict_copy(xdata, local->xdata_rsp) == NULL) {
921 goto done;
922 }
923 }
924
925 ret = 0;
926
927 done:
928 if (local_dict != NULL) {
929 dict_unref(local_dict);
930 }
931
932 if (local_xdata != NULL) {
933 dict_unref(local_xdata);
934 }
935
936 return ret;
937 }
938
939 static void
940 afr_getxattr_lockinfo_cbk_common(call_frame_t *frame, int32_t op_ret,
941 int32_t op_errno, dict_t *dict, dict_t *xdata,
942 bool is_fgetxattr)
943 {
944 int len = 0;
945 char *lockinfo_buf = NULL;
946 dict_t *lockinfo = NULL, *newdict = NULL;
947 afr_local_t *local = NULL;
948
949 local = frame->local;
950
951 if ((op_ret >= 0) && (dict != NULL)) {
952 op_ret = dict_get_ptr_and_len(dict, GF_XATTR_LOCKINFO_KEY,
953 (void **)&lockinfo_buf, &len);
954 if (lockinfo_buf != NULL) {
955 lockinfo = dict_new();
956 if (lockinfo == NULL) {
957 op_ret = -1;
958 } else {
959 op_ret = dict_unserialize(lockinfo_buf, len, &lockinfo);
960 }
961 }
962 }
963
964 if ((op_ret >= 0) && ((lockinfo != NULL) || (xdata != NULL))) {
965 op_ret = afr_update_local_dicts(frame, lockinfo, xdata);
966 if (lockinfo != NULL) {
967 dict_unref(lockinfo);
968 }
969 }
970
971 if (op_ret < 0) {
972 local->op_ret = -1;
973 local->op_errno = ENOMEM;
974 }
975
976 if (uatomic_sub_return(&local->call_count, 1) == 0) {
977 newdict = dict_new();
978 if (!newdict) {
979 local->op_ret = -1;
980 local->op_errno = op_errno = ENOMEM;
981 goto unwind;
982 }
983
984 op_ret = dict_allocate_and_serialize(
985 local->dict, (char **)&lockinfo_buf, (unsigned int *)&len);
986 if (op_ret != 0) {
987 local->op_ret = -1;
988 local->op_errno = op_errno = ENOMEM;
989 goto unwind;
990 }
991
992 op_ret = dict_set_dynptr(newdict, GF_XATTR_LOCKINFO_KEY,
993 (void *)lockinfo_buf, len);
994 if (op_ret < 0) {
995 GF_FREE(lockinfo_buf);
996 local->op_ret = op_ret = -1;
997 local->op_errno = op_errno = -op_ret;
998 goto unwind;
999 }
1000
1001 unwind:
1002 /* TODO: These unwinds use op_ret and op_errno instead of local->op_ret
1003 * and local->op_errno. This doesn't seem right because any
1004 * failure during processing of each answer could be silently
1005 * ignored. This is kept this was the old behavior and because
1006 * local->op_ret is initialized as -1 and local->op_errno is
1007 * initialized as EUCLEAN, which makes these values useless. */
1008 if (is_fgetxattr) {
1009 AFR_STACK_UNWIND(fgetxattr, frame, op_ret, op_errno, newdict,
1010 local->xdata_rsp);
1011 } else {
1012 AFR_STACK_UNWIND(getxattr, frame, op_ret, op_errno, newdict,
1013 local->xdata_rsp);
1014 }
1015
1016 if (newdict != NULL) {
1017 dict_unref(newdict);
1018 }
1019 }
1020 }
1021
1022 static int32_t
1023 afr_getxattr_lockinfo_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
1024 int32_t op_ret, int32_t op_errno, dict_t *dict,
1025 dict_t *xdata)
1026 {
1027 afr_getxattr_lockinfo_cbk_common(frame, op_ret, op_errno, dict, xdata,
1028 false);
1029
1030 return 0;
1031 }
1032
1033 static int32_t
1034 afr_fgetxattr_lockinfo_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
1035 int32_t op_ret, int32_t op_errno, dict_t *dict,
1036 dict_t *xdata)
1037 {
1038 afr_getxattr_lockinfo_cbk_common(frame, op_ret, op_errno, dict, xdata,
1039 true);
1040
1041 return 0;
1042 }
1043
1044 static int32_t
1045 afr_fgetxattr_pathinfo_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
1046 int32_t op_ret, int32_t op_errno, dict_t *dict,
1047 dict_t *xdata)
1048 {
1049 afr_local_t *local = NULL;
1050 int32_t callcnt = 0;
1051 int ret = 0;
1052 char *xattr = NULL;
1053 char *xattr_serz = NULL;
1054 int keylen = 0;
1055 char xattr_cky[1024] = {
1056 0,
1057 };
1058 int xattr_cky_len = 0;
1059 dict_t *nxattr = NULL;
1060 long cky = 0;
1061 int32_t padding = 0;
1062 int32_t tlen = 0;
1063
1064 if (!frame || !frame->local || !this) {
1065 gf_msg("", GF_LOG_ERROR, 0, AFR_MSG_INVALID_ARG, "possible NULL deref");
1066 goto out;
1067 }
1068
1069 local = frame->local;
1070 cky = (long)cookie;
1071 keylen = strlen(local->cont.getxattr.name);
1072 xattr_cky_len = snprintf(xattr_cky, sizeof(xattr_cky), "%s-%ld",
1073 local->cont.getxattr.name, cky);
1074 LOCK(&frame->lock);
1075 {
1076 callcnt = --local->call_count;
1077
1078 if (op_ret < 0) {
1079 local->op_errno = op_errno;
1080 } else {
1081 local->op_ret = op_ret;
1082 if (!local->xdata_rsp && xdata)
1083 local->xdata_rsp = dict_ref(xdata);
1084 }
1085
1086 if (!dict || (op_ret < 0))
1087 goto unlock;
1088
1089 if (!local->dict) {
1090 local->dict = dict_new();
1091 if (!local->dict)
1092 goto unlock;
1093 }
1094 ret = dict_get_strn(dict, local->cont.getxattr.name, keylen, &xattr);
1095 if (ret)
1096 goto unlock;
1097
1098 xattr = gf_strdup(xattr);
1099
1100 ret = dict_set_dynstrn(local->dict, xattr_cky, xattr_cky_len, xattr);
1101 if (ret) {
1102 UNLOCK(&frame->lock);
1103 gf_msg(this->name, GF_LOG_ERROR, -ret, AFR_MSG_DICT_SET_FAILED,
1104 "Cannot set xattr cookie key");
1105 goto post_unlock;
1106 }
1107
1108 local->cont.getxattr.xattr_len += strlen(xattr) + 1;
1109 }
1110 unlock:
1111 UNLOCK(&frame->lock);
1112 post_unlock:
1113 if (!callcnt) {
1114 if (!local->cont.getxattr.xattr_len)
1115 goto unwind;
1116
1117 nxattr = dict_new();
1118 if (!nxattr)
1119 goto unwind;
1120
1121 /* extra bytes for decorations (brackets and <>'s) */
1122 padding += strlen(this->name) + SLEN(AFR_PATHINFO_HEADER) + 4;
1123 local->cont.getxattr.xattr_len += (padding + 2);
1124
1125 xattr_serz = GF_MALLOC(local->cont.getxattr.xattr_len,
1126 gf_common_mt_char);
1127
1128 if (!xattr_serz)
1129 goto unwind;
1130
1131 /* the xlator info */
1132 int xattr_serz_len = sprintf(
1133 xattr_serz, "(<" AFR_PATHINFO_HEADER "%s> ", this->name);
1134
1135 /* actual series of pathinfo */
1136 ret = dict_serialize_value_with_delim(
1137 local->dict, xattr_serz + xattr_serz_len, &tlen, ' ');
1138 if (ret) {
1139 GF_FREE(xattr_serz);
1140 goto unwind;
1141 }
1142
1143 /* closing part */
1144 *(xattr_serz + padding + tlen) = ')';
1145 *(xattr_serz + padding + tlen + 1) = '\0';
1146
1147 ret = dict_set_dynstrn(nxattr, local->cont.getxattr.name, keylen,
1148 xattr_serz);
1149 if (ret) {
1150 gf_msg(this->name, GF_LOG_ERROR, -ret, AFR_MSG_DICT_SET_FAILED,
1151 "Cannot set pathinfo key in dict");
1152 if (ret == -EINVAL)
1153 GF_FREE(xattr_serz);
1154 }
1155
1156 unwind:
1157 AFR_STACK_UNWIND(fgetxattr, frame, local->op_ret, local->op_errno,
1158 nxattr, local->xdata_rsp);
1159
1160 if (nxattr)
1161 dict_unref(nxattr);
1162 }
1163
1164 out:
1165 return ret;
1166 }
1167
1168 static int32_t
1169 afr_getxattr_pathinfo_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
1170 int32_t op_ret, int32_t op_errno, dict_t *dict,
1171 dict_t *xdata)
1172 {
1173 afr_local_t *local = NULL;
1174 int32_t callcnt = 0;
1175 int ret = 0;
1176 char *xattr = NULL;
1177 char *xattr_serz = NULL;
1178 char xattr_cky[1024] = {
1179 0,
1180 };
1181 int keylen = 0;
1182 int xattr_cky_len = 0;
1183 dict_t *nxattr = NULL;
1184 long cky = 0;
1185 int32_t padding = 0;
1186 int32_t tlen = 0;
1187
1188 if (!frame || !frame->local || !this) {
1189 gf_msg("", GF_LOG_ERROR, 0, AFR_MSG_INVALID_ARG, "possible NULL deref");
1190 goto out;
1191 }
1192
1193 local = frame->local;
1194 cky = (long)cookie;
1195 keylen = strlen(local->cont.getxattr.name);
1196 xattr_cky_len = snprintf(xattr_cky, sizeof(xattr_cky), "%s-%ld",
1197 local->cont.getxattr.name, cky);
1198 LOCK(&frame->lock);
1199 {
1200 callcnt = --local->call_count;
1201
1202 if (op_ret < 0) {
1203 local->op_errno = op_errno;
1204 } else {
1205 local->op_ret = op_ret;
1206 if (!local->xdata_rsp && xdata)
1207 local->xdata_rsp = dict_ref(xdata);
1208 }
1209
1210 if (!dict || (op_ret < 0))
1211 goto unlock;
1212
1213 if (!local->dict) {
1214 local->dict = dict_new();
1215 if (!local->dict)
1216 goto unlock;
1217 }
1218 ret = dict_get_strn(dict, local->cont.getxattr.name, keylen, &xattr);
1219 if (ret)
1220 goto unlock;
1221
1222 xattr = gf_strdup(xattr);
1223
1224 ret = dict_set_dynstrn(local->dict, xattr_cky, xattr_cky_len, xattr);
1225 if (ret) {
1226 UNLOCK(&frame->lock);
1227 gf_msg(this->name, GF_LOG_ERROR, -ret, AFR_MSG_DICT_SET_FAILED,
1228 "Cannot set xattr cookie key");
1229 goto post_unlock;
1230 }
1231
1232 local->cont.getxattr.xattr_len += strlen(xattr) + 1;
1233 }
1234 unlock:
1235 UNLOCK(&frame->lock);
1236 post_unlock:
1237 if (!callcnt) {
1238 if (!local->cont.getxattr.xattr_len)
1239 goto unwind;
1240
1241 nxattr = dict_new();
1242 if (!nxattr)
1243 goto unwind;
1244
1245 /* extra bytes for decorations (brackets and <>'s) */
1246 padding += strlen(this->name) + SLEN(AFR_PATHINFO_HEADER) + 4;
1247 local->cont.getxattr.xattr_len += (padding + 2);
1248
1249 xattr_serz = GF_MALLOC(local->cont.getxattr.xattr_len,
1250 gf_common_mt_char);
1251
1252 if (!xattr_serz)
1253 goto unwind;
1254
1255 /* the xlator info */
1256 int xattr_serz_len = sprintf(
1257 xattr_serz, "(<" AFR_PATHINFO_HEADER "%s> ", this->name);
1258
1259 /* actual series of pathinfo */
1260 ret = dict_serialize_value_with_delim(
1261 local->dict, xattr_serz + xattr_serz_len, &tlen, ' ');
1262 if (ret) {
1263 GF_FREE(xattr_serz);
1264 goto unwind;
1265 }
1266
1267 /* closing part */
1268 *(xattr_serz + padding + tlen) = ')';
1269 *(xattr_serz + padding + tlen + 1) = '\0';
1270
1271 ret = dict_set_dynstrn(nxattr, local->cont.getxattr.name, keylen,
1272 xattr_serz);
1273 if (ret) {
1274 gf_msg(this->name, GF_LOG_ERROR, -ret, AFR_MSG_DICT_SET_FAILED,
1275 "Cannot set pathinfo key in dict");
1276 if (ret == -EINVAL)
1277 GF_FREE(xattr_serz);
1278 }
1279
1280 unwind:
1281 AFR_STACK_UNWIND(getxattr, frame, local->op_ret, local->op_errno,
1282 nxattr, local->xdata_rsp);
1283
1284 if (nxattr)
1285 dict_unref(nxattr);
1286 }
1287
1288 out:
1289 return ret;
1290 }
1291
1292 static int
1293 afr_aggregate_stime_xattr(dict_t *this, char *key, data_t *value, void *data)
1294 {
1295 int ret = 0;
1296
1297 if (fnmatch(GF_XATTR_STIME_PATTERN, key, FNM_NOESCAPE) == 0)
1298 ret = gf_get_max_stime(THIS, data, key, value);
1299
1300 return ret;
1301 }
1302
1303 static int32_t
1304 afr_common_getxattr_stime_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
1305 int32_t op_ret, int32_t op_errno, dict_t *dict,
1306 dict_t *xdata)
1307 {
1308 afr_local_t *local = NULL;
1309 int32_t callcnt = 0;
1310
1311 if (!frame || !frame->local || !this) {
1312 gf_msg("", GF_LOG_ERROR, 0, AFR_MSG_INVALID_ARG, "possible NULL deref");
1313 goto out;
1314 }
1315
1316 local = frame->local;
1317
1318 LOCK(&frame->lock);
1319 {
1320 callcnt = --local->call_count;
1321
1322 if (!dict || (op_ret < 0)) {
1323 local->op_errno = op_errno;
1324 goto cleanup;
1325 }
1326
1327 if (!local->dict)
1328 local->dict = dict_copy_with_ref(dict, NULL);
1329 else
1330 dict_foreach(dict, afr_aggregate_stime_xattr, local->dict);
1331 local->op_ret = 0;
1332 }
1333
1334 cleanup:
1335 UNLOCK(&frame->lock);
1336
1337 if (!callcnt) {
1338 AFR_STACK_UNWIND(getxattr, frame, local->op_ret, local->op_errno,
1339 local->dict, xdata);
1340 }
1341
1342 out:
1343 return 0;
1344 }
1345
1346 static gf_boolean_t
1347 afr_is_special_xattr(const char *name, fop_getxattr_cbk_t *cbk,
1348 gf_boolean_t is_fgetxattr)
1349 {
1350 gf_boolean_t is_spl = _gf_true;
1351
1352 GF_ASSERT(cbk);
1353 if (!cbk || !name) {
1354 is_spl = _gf_false;
1355 goto out;
1356 }
1357
1358 if (!strcmp(name, GF_XATTR_PATHINFO_KEY) ||
1359 !strcmp(name, GF_XATTR_USER_PATHINFO_KEY)) {
1360 if (is_fgetxattr) {
1361 *cbk = afr_fgetxattr_pathinfo_cbk;
1362 } else {
1363 *cbk = afr_getxattr_pathinfo_cbk;
1364 }
1365 } else if (!strncmp(name, GF_XATTR_CLRLK_CMD, SLEN(GF_XATTR_CLRLK_CMD)) ||
1366 !strncmp(name, GF_XATTR_INTRLK_CMD, SLEN(GF_XATTR_INTRLK_CMD))) {
1367 if (is_fgetxattr) {
1368 *cbk = afr_fgetxattr_clrlk_cbk;
1369 } else {
1370 *cbk = afr_getxattr_clrlk_cbk;
1371 }
1372 } else if (!strncmp(name, GF_XATTR_LOCKINFO_KEY,
1373 SLEN(GF_XATTR_LOCKINFO_KEY))) {
1374 if (is_fgetxattr) {
1375 *cbk = afr_fgetxattr_lockinfo_cbk;
1376 } else {
1377 *cbk = afr_getxattr_lockinfo_cbk;
1378 }
1379 } else if (fnmatch(GF_XATTR_STIME_PATTERN, name, FNM_NOESCAPE) == 0) {
1380 *cbk = afr_common_getxattr_stime_cbk;
1381 } else if (strcmp(name, QUOTA_SIZE_KEY) == 0) {
1382 *cbk = afr_getxattr_quota_size_cbk;
1383 } else if (!strcmp(name, GF_XATTR_LIST_NODE_UUIDS_KEY)) {
1384 *cbk = afr_getxattr_list_node_uuids_cbk;
1385 } else {
1386 is_spl = _gf_false;
1387 }
1388
1389 out:
1390 return is_spl;
1391 }
1392
1393 static void
1394 afr_getxattr_all_subvols(afr_private_t *priv, call_frame_t *frame,
1395 const char *name, loc_t *loc, fop_getxattr_cbk_t cbk)
1396 {
1397 afr_local_t *local = NULL;
1398 int i = 0;
1399 int call_count = 0;
1400
1401 local = frame->local;
1402 // local->call_count set in afr_local_init
1403 call_count = local->call_count;
1404
1405 if (!strcmp(name, GF_XATTR_LIST_NODE_UUIDS_KEY)) {
1406 GF_FREE(local->cont.getxattr.name);
1407 local->cont.getxattr.name = gf_strdup(GF_XATTR_NODE_UUID_KEY);
1408 }
1409
1410 // If up-children count is 0, afr_local_init would have failed already
1411 // and the call would have unwound so not handling it here.
1412 for (i = 0; i < priv->child_count; i++) {
1413 if (local->child_up[i]) {
1414 STACK_WIND_COOKIE(frame, cbk, (void *)(long)i, priv->children[i],
1415 priv->children[i]->fops->getxattr, loc,
1416 local->cont.getxattr.name, NULL);
1417 if (!--call_count)
1418 break;
1419 }
1420 }
1421 return;
1422 }
1423
1424 static int
1425 afr_marker_populate_args(call_frame_t *frame, int type, int *gauge,
1426 xlator_t **subvols)
1427 {
1428 xlator_t *this = frame->this;
1429 afr_private_t *priv = this->private;
1430
1431 memcpy(subvols, priv->children, sizeof(*subvols) * priv->child_count);
1432
1433 if (type == MARKER_XTIME_TYPE) {
1434 /*Don't error out on ENOENT/ENOTCONN */
1435 gauge[MCNT_NOTFOUND] = 0;
1436 gauge[MCNT_ENOTCONN] = 0;
1437 }
1438 return priv->child_count;
1439 }
1440
1441 static int
1442 afr_handle_heal_xattrs(call_frame_t *frame, xlator_t *this, loc_t *loc,
1443 const char *heal_op)
1444 {
1445 int ret = -1;
1446 afr_spb_status_t *data = NULL;
1447
1448 if (!strcmp(heal_op, GF_HEAL_INFO)) {
1449 afr_get_heal_info(frame, this, loc);
1450 ret = 0;
1451 goto out;
1452 }
1453
1454 if (!strcmp(heal_op, GF_AFR_HEAL_SBRAIN)) {
1455 afr_heal_splitbrain_file(frame, this, loc);
1456 ret = 0;
1457 goto out;
1458 }
1459
1460 if (!strcmp(heal_op, GF_AFR_SBRAIN_STATUS)) {
1461 data = GF_CALLOC(1, sizeof(*data), gf_afr_mt_spb_status_t);
1462 if (!data) {
1463 ret = 1;
1464 goto out;
1465 }
1466 data->frame = frame;
1467 data->loc = loc;
1468 ret = synctask_new(this->ctx->env, afr_get_split_brain_status,
1469 afr_get_split_brain_status_cbk, NULL, data);
1470 if (ret) {
1471 gf_msg(this->name, GF_LOG_ERROR, 0, AFR_MSG_SPLIT_BRAIN_STATUS,
1472 "Failed to create"
1473 " synctask. Unable to fetch split-brain status"
1474 " for %s.",
1475 loc->name);
1476 ret = 1;
1477 goto out;
1478 }
1479 goto out;
1480 }
1481
1482 out:
1483 if (ret == 1) {
1484 AFR_STACK_UNWIND(getxattr, frame, -1, ENOMEM, NULL, NULL);
1485 if (data)
1486 GF_FREE(data);
1487 ret = 0;
1488 }
1489 return ret;
1490 }
1491
1492 int32_t
1493 afr_getxattr(call_frame_t *frame, xlator_t *this, loc_t *loc, const char *name,
1494 dict_t *xdata)
1495 {
1496 afr_private_t *priv = NULL;
1497 afr_local_t *local = NULL;
1498 xlator_t **children = NULL;
1499 int i = 0;
1500 int32_t op_errno = 0;
1501 int ret = -1;
1502 fop_getxattr_cbk_t cbk = NULL;
1503
1504 local = AFR_FRAME_INIT(frame, op_errno);
1505 if (!local)
1506 goto out;
1507
1508 priv = this->private;
1509
1510 children = priv->children;
1511
1512 loc_copy(&local->loc, loc);
1513
1514 local->op = GF_FOP_GETXATTR;
1515
1516 if (xdata)
1517 local->xdata_req = dict_ref(xdata);
1518
1519 if (!name)
1520 goto no_name;
1521
1522 local->cont.getxattr.name = gf_strdup(name);
1523
1524 if (!local->cont.getxattr.name) {
1525 op_errno = ENOMEM;
1526 goto out;
1527 }
1528
1529 if (!strncmp(name, AFR_XATTR_PREFIX, SLEN(AFR_XATTR_PREFIX))) {
1530 op_errno = ENODATA;
1531 goto out;
1532 }
1533
1534 if (cluster_handle_marker_getxattr(frame, loc, name, priv->vol_uuid,
1535 afr_getxattr_unwind,
1536 afr_marker_populate_args) == 0)
1537 return 0;
1538
1539 ret = afr_handle_heal_xattrs(frame, this, &local->loc, name);
1540 if (ret == 0)
1541 return 0;
1542
1543 /*
1544 * Heal daemons don't have IO threads ... and as a result they
1545 * send this getxattr down and eventually crash :(
1546 */
1547 GF_CHECK_XATTR_KEY_AND_GOTO(name, IO_THREADS_QUEUE_SIZE_KEY, op_errno, out);
1548
1549 /*
1550 * Special xattrs which need responses from all subvols
1551 */
1552 if (afr_is_special_xattr(name, &cbk, 0)) {
1553 afr_getxattr_all_subvols(priv, frame, name, loc, cbk);
1554 return 0;
1555 }
1556
1557 if (XATTR_IS_NODE_UUID(name)) {
1558 i = 0;
1559 STACK_WIND_COOKIE(frame, afr_getxattr_node_uuid_cbk, (void *)(long)i,
1560 children[i], children[i]->fops->getxattr, loc, name,
1561 xdata);
1562 return 0;
1563 }
1564
1565 no_name:
1566
1567 afr_read_txn(frame, this, local->loc.inode, afr_getxattr_wind,
1568 AFR_METADATA_TRANSACTION);
1569
1570 ret = 0;
1571 out:
1572 if (ret < 0)
1573 AFR_STACK_UNWIND(getxattr, frame, -1, op_errno, NULL, NULL);
1574
1575 return 0;
1576 }
1577
1578 /* {{{ fgetxattr */
1579
1580 static int32_t
1581 afr_fgetxattr_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
1582 int32_t op_ret, int32_t op_errno, dict_t *dict, dict_t *xdata)
1583 {
1584 afr_local_t *local = NULL;
1585
1586 local = frame->local;
1587
1588 if (op_ret < 0) {
1589 local->op_ret = -1;
1590 local->op_errno = op_errno;
1591
1592 afr_read_txn_continue(frame, this, (long)cookie);
1593 return 0;
1594 }
1595
1596 if (dict)
1597 afr_filter_xattrs(dict);
1598
1599 AFR_STACK_UNWIND(fgetxattr, frame, op_ret, op_errno, dict, xdata);
1600
1601 return 0;
1602 }
1603
1604 static int
1605 afr_fgetxattr_wind(call_frame_t *frame, xlator_t *this, int subvol)
1606 {
1607 afr_local_t *local = NULL;
1608 afr_private_t *priv = NULL;
1609
1610 local = frame->local;
1611 priv = this->private;
1612
1613 if (subvol == -1) {
1614 AFR_STACK_UNWIND(fgetxattr, frame, local->op_ret, local->op_errno, NULL,
1615 NULL);
1616 return 0;
1617 }
1618
1619 STACK_WIND_COOKIE(frame, afr_fgetxattr_cbk, (void *)(long)subvol,
1620 priv->children[subvol],
1621 priv->children[subvol]->fops->fgetxattr, local->fd,
1622 local->cont.getxattr.name, local->xdata_req);
1623 return 0;
1624 }
1625
1626 static void
1627 afr_fgetxattr_all_subvols(afr_private_t *priv, call_frame_t *frame,
1628 fop_fgetxattr_cbk_t cbk)
1629 {
1630 afr_local_t *local = NULL;
1631 int i = 0;
1632 int call_count = 0;
1633
1634 local = frame->local;
1635 // local->call_count set in afr_local_init
1636 call_count = local->call_count;
1637
1638 // If up-children count is 0, afr_local_init would have failed already
1639 // and the call would have unwound so not handling it here.
1640
1641 for (i = 0; i < priv->child_count; i++) {
1642 if (local->child_up[i]) {
1643 STACK_WIND_COOKIE(frame, cbk, (void *)(long)i, priv->children[i],
1644 priv->children[i]->fops->fgetxattr, local->fd,
1645 local->cont.getxattr.name, NULL);
1646 if (!--call_count)
1647 break;
1648 }
1649 }
1650
1651 return;
1652 }
1653
1654 int
1655 afr_fgetxattr(call_frame_t *frame, xlator_t *this, fd_t *fd, const char *name,
1656 dict_t *xdata)
1657 {
1658 afr_local_t *local = NULL;
1659 int32_t op_errno = 0;
1660 fop_fgetxattr_cbk_t cbk = NULL;
1661
1662 AFR_ERROR_OUT_IF_FDCTX_INVALID(fd, this, op_errno, out);
1663 local = AFR_FRAME_INIT(frame, op_errno);
1664 if (!local)
1665 goto out;
1666
1667 local->op = GF_FOP_FGETXATTR;
1668 local->fd = fd_ref(fd);
1669 if (name) {
1670 local->cont.getxattr.name = gf_strdup(name);
1671 if (!local->cont.getxattr.name) {
1672 op_errno = ENOMEM;
1673 goto out;
1674 }
1675 }
1676 if (xdata)
1677 local->xdata_req = dict_ref(xdata);
1678
1679 /* pathinfo gets handled only in getxattr(), but we need to handle
1680 * lockinfo.
1681 * If we are doing fgetxattr with lockinfo as the key then we
1682 * collect information from all children.
1683 */
1684 if (afr_is_special_xattr(name, &cbk, 1)) {
1685 afr_fgetxattr_all_subvols(this->private, frame, cbk);
1686 return 0;
1687 }
1688
1689 afr_fix_open(fd, this);
1690
1691 afr_read_txn(frame, this, fd->inode, afr_fgetxattr_wind,
1692 AFR_METADATA_TRANSACTION);
1693
1694 return 0;
1695 out:
1696 AFR_STACK_UNWIND(fgetxattr, frame, -1, op_errno, NULL, NULL);
1697
1698 return 0;
1699 }
1700
1701 /* }}} */
1702
1703 /* {{{ readv */
1704
1705 static int
1706 afr_readv_cbk(call_frame_t *frame, void *cookie, xlator_t *this, int32_t op_ret,
1707 int32_t op_errno, struct iovec *vector, int32_t count,
1708 struct iatt *buf, struct iobref *iobref, dict_t *xdata)
1709 {
1710 afr_local_t *local = NULL;
1711
1712 local = frame->local;
1713
1714 if (op_ret < 0) {
1715 local->op_ret = -1;
1716 local->op_errno = op_errno;
1717
1718 afr_read_txn_continue(frame, this, (long)cookie);
1719 return 0;
1720 }
1721
1722 AFR_STACK_UNWIND(readv, frame, op_ret, op_errno, vector, count, buf, iobref,
1723 xdata);
1724 return 0;
1725 }
1726
1727 static int
1728 afr_readv_wind(call_frame_t *frame, xlator_t *this, int subvol)
1729 {
1730 afr_local_t *local = NULL;
1731 afr_private_t *priv = NULL;
1732
1733 local = frame->local;
1734 priv = this->private;
1735
1736 if (subvol == -1) {
1737 AFR_STACK_UNWIND(readv, frame, local->op_ret, local->op_errno, 0, 0, 0,
1738 0, 0);
1739 return 0;
1740 }
1741
1742 STACK_WIND_COOKIE(
1743 frame, afr_readv_cbk, (void *)(long)subvol, priv->children[subvol],
1744 priv->children[subvol]->fops->readv, local->fd, local->cont.readv.size,
1745 local->cont.readv.offset, local->cont.readv.flags, local->xdata_req);
1746 return 0;
1747 }
1748
1749 int
1750 afr_readv(call_frame_t *frame, xlator_t *this, fd_t *fd, size_t size,
1751 off_t offset, uint32_t flags, dict_t *xdata)
1752 {
1753 afr_local_t *local = NULL;
1754 int32_t op_errno = 0;
1755
1756 AFR_ERROR_OUT_IF_FDCTX_INVALID(fd, this, op_errno, out);
1757 local = AFR_FRAME_INIT(frame, op_errno);
1758 if (!local)
1759 goto out;
1760
1761 local->op = GF_FOP_READ;
1762 local->fd = fd_ref(fd);
1763 local->cont.readv.size = size;
1764 local->cont.readv.offset = offset;
1765 local->cont.readv.flags = flags;
1766 if (xdata)
1767 local->xdata_req = dict_ref(xdata);
1768
1769 afr_fix_open(fd, this);
1770
1771 afr_read_txn(frame, this, fd->inode, afr_readv_wind, AFR_DATA_TRANSACTION);
1772
1773 return 0;
1774 out:
1775 AFR_STACK_UNWIND(readv, frame, -1, op_errno, 0, 0, 0, 0, 0);
1776
1777 return 0;
1778 }
1779
1780 /* }}} */
1781
1782 /* {{{ seek */
1783
1784 static int
1785 afr_seek_cbk(call_frame_t *frame, void *cookie, xlator_t *this, int32_t op_ret,
1786 int32_t op_errno, off_t offset, dict_t *xdata)
1787 {
1788 afr_local_t *local = NULL;
1789
1790 local = frame->local;
1791
1792 if (op_ret < 0) {
1793 local->op_ret = -1;
1794 local->op_errno = op_errno;
1795
1796 afr_read_txn_continue(frame, this, (long)cookie);
1797 return 0;
1798 }
1799
1800 AFR_STACK_UNWIND(seek, frame, op_ret, op_errno, offset, xdata);
1801 return 0;
1802 }
1803
1804 static int
1805 afr_seek_wind(call_frame_t *frame, xlator_t *this, int subvol)
1806 {
1807 afr_local_t *local = NULL;
1808 afr_private_t *priv = NULL;
1809
1810 local = frame->local;
1811 priv = this->private;
1812
1813 if (subvol == -1) {
1814 AFR_STACK_UNWIND(seek, frame, local->op_ret, local->op_errno, 0, NULL);
1815 return 0;
1816 }
1817
1818 STACK_WIND_COOKIE(
1819 frame, afr_seek_cbk, (void *)(long)subvol, priv->children[subvol],
1820 priv->children[subvol]->fops->seek, local->fd, local->cont.seek.offset,
1821 local->cont.seek.what, local->xdata_req);
1822 return 0;
1823 }
1824
1825 int
1826 afr_seek(call_frame_t *frame, xlator_t *this, fd_t *fd, off_t offset,
1827 gf_seek_what_t what, dict_t *xdata)
1828 {
1829 afr_local_t *local = NULL;
1830 int32_t op_errno = 0;
1831
1832 AFR_ERROR_OUT_IF_FDCTX_INVALID(fd, this, op_errno, out);
1833 local = AFR_FRAME_INIT(frame, op_errno);
1834 if (!local)
1835 goto out;
1836
1837 local->op = GF_FOP_SEEK;
1838 local->fd = fd_ref(fd);
1839 local->cont.seek.offset = offset;
1840 local->cont.seek.what = what;
1841 if (xdata)
1842 local->xdata_req = dict_ref(xdata);
1843
1844 afr_fix_open(fd, this);
1845
1846 afr_read_txn(frame, this, fd->inode, afr_seek_wind, AFR_DATA_TRANSACTION);
1847
1848 return 0;
1849 out:
1850 AFR_STACK_UNWIND(seek, frame, -1, op_errno, 0, NULL);
1851
1852 return 0;
1853 }
1854 /* }}} */