Debian Codegraph @

xlators/storage/posix/src/posix-helpers.c

raw symbols SCIP
1 /*
2 Copyright (c) 2006-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 #define __XOPEN_SOURCE 500
11
12 #include <stdint.h>
13 #include <sys/time.h>
14 #include <errno.h>
15 #include <libgen.h>
16 #include <pthread.h>
17 #include <ftw.h>
18 #include <sys/stat.h>
19 #include <signal.h>
20 #include <aio.h>
21
22 #ifdef HAVE_SYS_ACL_H
23 #ifdef HAVE_ACL_LIBACL_H /* for acl_to_any_text() */
24 #include <acl/libacl.h>
25 #else /* FreeBSD and others */
26 #include <sys/acl.h>
27 #endif
28 #endif
29
30 #ifndef GF_BSD_HOST_OS
31 #include <alloca.h>
32 #endif /* GF_BSD_HOST_OS */
33
34 #include <fnmatch.h>
35 #include "posix.h"
36 #include "posix-messages.h"
37 #include "posix-metadata.h"
38 #include "posix-handle.h"
39 #include <glusterfs/compat-errno.h>
40 #include <glusterfs/compat.h>
41 #include <glusterfs/syscall.h>
42 #include <glusterfs/locking.h>
43 #include <glusterfs/glusterfs-acl.h>
44 #include "posix-gfid-path.h"
45 #include <glusterfs/events.h>
46 #include "glusterfs/syncop.h"
47 #include "timer-wheel.h"
48 #include <sys/types.h>
49
50 char *marker_xattrs[] = {"trusted.glusterfs.quota.*",
51 "trusted.glusterfs.*.xtime", NULL};
52
53 static char *marker_contri_key = "trusted.*.*.contri";
54
55 static char *posix_ignore_xattrs[] = {"gfid-req",
56 GLUSTERFS_INTERNAL_FOP_KEY,
57 GLUSTERFS_ENTRYLK_COUNT,
58 GLUSTERFS_INODELK_COUNT,
59 GLUSTERFS_POSIXLK_COUNT,
60 GLUSTERFS_PARENT_ENTRYLK,
61 GF_GFIDLESS_LOOKUP,
62 GLUSTERFS_INODELK_DOM_COUNT,
63 NULL};
64
65 static char *list_xattr_ignore_xattrs[] = {GFID_XATTR_KEY, GF_XATTR_VOL_ID_KEY,
66 GF_SELINUX_XATTR_KEY, NULL};
67
68 gf_boolean_t
69 posix_special_xattr(char **pattern, char *key)
70 {
71 int i = 0;
72 gf_boolean_t flag = _gf_false;
73
74 GF_VALIDATE_OR_GOTO("posix", pattern, out);
75 GF_VALIDATE_OR_GOTO("posix", key, out);
76
77 for (i = 0; pattern[i]; i++) {
78 if (!fnmatch(pattern[i], key, 0)) {
79 flag = _gf_true;
80 break;
81 }
82 }
83 out:
84 return flag;
85 }
86
87 int
88 posix_handle_mdata_xattr(call_frame_t *frame, const char *name, int *op_errno)
89 {
90 int i = 0;
91 int ret = 0;
92 int pid = 1;
93 static const char *const internal_xattr[] = {GF_XATTR_MDATA_KEY, NULL};
94 if (frame && frame->root) {
95 pid = frame->root->pid;
96 }
97
98 if (!name || pid < GF_CLIENT_PID_MAX) {
99 /* No need to do anything here */
100 ret = 0;
101 goto out;
102 }
103
104 for (i = 0; internal_xattr[i]; i++) {
105 if (fnmatch(internal_xattr[i], name, FNM_PERIOD) == 0) {
106 ret = -1;
107 if (op_errno) {
108 *op_errno = ENOATTR;
109 }
110
111 gf_msg_debug("posix", ENOATTR,
112 "Ignoring the key %s as an internal "
113 "xattrs.",
114 name);
115 goto out;
116 }
117 }
118
119 ret = 0;
120 out:
121 return ret;
122 }
123
124 int
125 posix_handle_georep_xattrs(call_frame_t *frame, const char *name, int *op_errno,
126 gf_boolean_t is_getxattr)
127 {
128 int i = 0;
129 int ret = 0;
130 int pid = 1;
131 gf_boolean_t filter_xattr = _gf_true;
132 static const char *georep_xattr[] = {"*.glusterfs.*.stime",
133 "*.glusterfs.*.xtime",
134 "*.glusterfs.lockinfo",
135 "*.glusterfs.*.entry_stime",
136 "*.glusterfs.volume-mark.*",
137 "trusted.SGI_*",
138 NULL};
139
140 if (!name) {
141 /* No need to do anything here */
142 ret = 0;
143 goto out;
144 }
145
146 if (frame && frame->root) {
147 pid = frame->root->pid;
148 }
149
150 if (pid == GF_CLIENT_PID_GSYNCD && is_getxattr) {
151 filter_xattr = _gf_false;
152
153 /* getxattr from gsyncd process should return all the
154 * internal xattr. In other cases ignore such xattrs
155 */
156 }
157
158 for (i = 0; filter_xattr && georep_xattr[i]; i++) {
159 if (fnmatch(georep_xattr[i], name, FNM_PERIOD) == 0) {
160 ret = -1;
161 if (op_errno)
162 *op_errno = ENOATTR;
163
164 gf_msg_debug("posix", ENOATTR,
165 "Ignoring the key %s as an internal "
166 "xattrs.",
167 name);
168 goto out;
169 }
170 }
171
172 ret = 0;
173 out:
174 return ret;
175 }
176
177 int32_t
178 posix_set_mode_in_dict(dict_t *in_dict, dict_t *out_dict, struct iatt *in_stbuf)
179 {
180 int ret = -1;
181 mode_t mode = 0;
182
183 if ((!in_stbuf) || (!out_dict)) {
184 goto out;
185 }
186
187 /* We need this only for files */
188 if (!(IA_ISREG(in_stbuf->ia_type))) {
189 ret = 0;
190 goto out;
191 }
192
193 /* Nobody asked for this */
194 if (!dict_get(in_dict, DHT_MODE_IN_XDATA_KEY)) {
195 ret = 0;
196 goto out;
197 }
198 mode = st_mode_from_ia(in_stbuf->ia_prot, in_stbuf->ia_type);
199
200 ret = dict_set_int32(out_dict, DHT_MODE_IN_XDATA_KEY, mode);
201
202 out:
203 return ret;
204 }
205
206 static gf_boolean_t
207 posix_xattr_ignorable(char *key)
208 {
209 return gf_get_index_by_elem(posix_ignore_xattrs, key) >= 0;
210 }
211
212 static int
213 _posix_xattr_get_set_from_backend(posix_xattr_filler_t *filler, char *key)
214 {
215 ssize_t xattr_size = 256; /* guesstimated initial size of xattr */
216 int ret = -1;
217 char *value = NULL;
218
219 if (!gf_is_valid_xattr_namespace(key)) {
220 goto out;
221 }
222
223 /* Most of the gluster internal xattrs don't exceed 256 bytes. So try
224 * getxattr with ~256 bytes. If it gives ERANGE then go the old way
225 * of getxattr with NULL buf to find the length and then getxattr with
226 * allocated buf to fill the data. This way we reduce lot of getxattrs.
227 */
228
229 value = GF_MALLOC(xattr_size + 1, gf_posix_mt_char);
230 if (!value) {
231 goto out;
232 }
233
234 if (filler->real_path)
235 xattr_size = sys_lgetxattr(filler->real_path, key, value, xattr_size);
236 else
237 xattr_size = sys_fgetxattr(filler->fdnum, key, value, xattr_size);
238
239 if (xattr_size == -1) {
240 if (value) {
241 GF_FREE(value);
242 value = NULL;
243 }
244 /* xattr_size == -1 - failed to fetch the xattr with
245 * current settings.
246 * If it was not because value was too small, abort
247 */
248 if (errno != ERANGE) {
249 goto out;
250 }
251
252 /* Get the real length needed */
253 if (filler->real_path) {
254 xattr_size = sys_lgetxattr(filler->real_path, key, NULL, 0);
255 } else {
256 xattr_size = sys_fgetxattr(filler->fdnum, key, NULL, 0);
257 }
258 if (xattr_size == -1) {
259 goto out;
260 }
261
262 value = GF_MALLOC(xattr_size + 1, gf_posix_mt_char);
263 if (!value) {
264 goto out;
265 }
266
267 if (filler->real_path) {
268 xattr_size = sys_lgetxattr(filler->real_path, key, value,
269 xattr_size);
270 } else {
271 xattr_size = sys_fgetxattr(filler->fdnum, key, value, xattr_size);
272 }
273 if (xattr_size == -1) {
274 GF_FREE(value);
275 value = NULL;
276 if (filler->real_path)
277 gf_msg(filler->this->name, GF_LOG_WARNING, 0,
278 P_MSG_XATTR_FAILED, "getxattr failed. path: %s, key: %s",
279 filler->real_path, key);
280 else
281 gf_msg(filler->this->name, GF_LOG_WARNING, 0,
282 P_MSG_XATTR_FAILED, "getxattr failed. gfid: %s, key: %s",
283 uuid_utoa(filler->fd->inode->gfid), key);
284 goto out;
285 }
286 }
287
288 value[xattr_size] = '\0';
289 ret = dict_set_bin(filler->xattr, key, value, xattr_size);
290
291 if (ret < 0) {
292 if (value)
293 GF_FREE(value);
294 if (filler->real_path)
295 gf_msg_debug(filler->this->name, 0,
296 "dict set failed. path: %s, key: %s",
297 filler->real_path, key);
298 else
299 gf_msg_debug(filler->this->name, 0,
300 "dict set failed. gfid: %s, key: %s",
301 uuid_utoa(filler->fd->inode->gfid), key);
302 goto out;
303 }
304 ret = 0;
305 out:
306 return ret;
307 }
308
309 static int gf_posix_xattr_enotsup_log;
310
311 static int
312 _posix_get_marker_all_contributions(posix_xattr_filler_t *filler)
313 {
314 ssize_t size = -1, remaining_size = -1, list_offset = 0;
315 int ret = -1;
316 int len;
317 char *list = NULL, key[4096] = {
318 0,
319 };
320
321 if (filler->real_path)
322 size = sys_llistxattr(filler->real_path, NULL, 0);
323 else
324 size = sys_flistxattr(filler->fdnum, NULL, 0);
325 if (size == -1) {
326 if ((errno == ENOTSUP) || (errno == ENOSYS)) {
327 GF_LOG_OCCASIONALLY(gf_posix_xattr_enotsup_log, THIS->name,
328 GF_LOG_WARNING,
329 "Extended attributes not "
330 "supported (try remounting brick"
331 " with 'user_xattr' flag)");
332 } else {
333 if (filler->real_path)
334 gf_msg(THIS->name, GF_LOG_WARNING, errno, P_MSG_XATTR_FAILED,
335 "listxattr failed on %s", filler->real_path);
336 else
337 gf_msg(THIS->name, GF_LOG_WARNING, errno, P_MSG_XATTR_FAILED,
338 "listxattr failed on %s",
339 uuid_utoa(filler->fd->inode->gfid));
340 }
341 goto out;
342 }
343
344 if (size == 0) {
345 ret = 0;
346 goto out;
347 }
348
349 list = GF_MALLOC(size, gf_posix_mt_char);
350 if (!list) {
351 goto out;
352 }
353
354 if (filler->real_path)
355 size = sys_llistxattr(filler->real_path, list, size);
356 else
357 size = sys_flistxattr(filler->fdnum, list, size);
358 if (size <= 0) {
359 ret = size;
360 goto out;
361 }
362
363 remaining_size = size;
364 list_offset = 0;
365
366 while (remaining_size > 0) {
367 len = snprintf(key, sizeof(key), "%s", list + list_offset);
368 if (fnmatch(marker_contri_key, key, 0) == 0) {
369 (void)_posix_xattr_get_set_from_backend(filler, key);
370 }
371 remaining_size -= (len + 1);
372 list_offset += (len + 1);
373 }
374
375 ret = 0;
376
377 out:
378 GF_FREE(list);
379 return ret;
380 }
381
382 static int
383 _posix_get_marker_quota_contributions(posix_xattr_filler_t *filler, char *key)
384 {
385 char *saveptr = NULL, *token = NULL, *tmp_key = NULL;
386 char *ptr = NULL;
387 int i = 0, ret = 0;
388
389 tmp_key = ptr = gf_strdup(key);
390 if (tmp_key == NULL) {
391 return -1;
392 }
393 for (i = 0; i < 4; i++) {
394 token = strtok_r(tmp_key, ".", &saveptr);
395 tmp_key = NULL;
396 }
397
398 if (strncmp(token, "contri", SLEN("contri")) == 0) {
399 ret = _posix_get_marker_all_contributions(filler);
400 } else {
401 ret = _posix_xattr_get_set_from_backend(filler, key);
402 }
403
404 GF_FREE(ptr);
405
406 return ret;
407 }
408
409 static inode_t *
410 _get_filler_inode(posix_xattr_filler_t *filler)
411 {
412 if (filler->fd)
413 return filler->fd->inode;
414 else if (filler->loc && filler->loc->inode)
415 return filler->loc->inode;
416 else
417 return NULL;
418 }
419
420 static int
421 _posix_xattr_get_set(dict_t *xattr_req, char *key, data_t *data,
422 void *xattrargs)
423 {
424 posix_xattr_filler_t *filler = xattrargs;
425 int ret = -1;
426 int len = 0;
427 char *databuf = NULL;
428 int _fd = -1;
429 ssize_t req_size = 0;
430 int32_t list_offset = 0;
431 ssize_t remaining_size = 0;
432 char *xattr = NULL;
433 inode_t *inode = NULL;
434 char *value = NULL;
435 struct iatt stbuf = {
436 0,
437 };
438
439 if (posix_xattr_ignorable(key))
440 goto out;
441
442 len = strlen(key);
443 /* should size be put into the data_t ? */
444 if ((filler->stbuf != NULL && IA_ISREG(filler->stbuf->ia_type)) &&
445 (len == SLEN(GF_CONTENT_KEY) && !strcmp(key, GF_CONTENT_KEY))) {
446 if (!filler->real_path)
447 goto out;
448
449 /* file content request */
450 req_size = data_to_uint64(data);
451 if (req_size >= filler->stbuf->ia_size) {
452 _fd = open(filler->real_path, O_RDONLY);
453 if (_fd == -1) {
454 gf_msg(filler->this->name, GF_LOG_ERROR, errno,
455 P_MSG_XDATA_GETXATTR, "Opening file %s failed",
456 filler->real_path);
457 goto err;
458 }
459
460 /*
461 * There could be a situation where the ia_size is
462 * zero. GF_CALLOC will return a pointer to the
463 * memory initialized by gf_mem_set_acct_info.
464 * This function adds a header and a footer to
465 * the allocated memory. The returned pointer
466 * points to the memory just after the header, but
467 * when size is zero, there is no space for user
468 * data. The memory can be freed by calling GF_FREE.
469 */
470 databuf = GF_CALLOC(1, filler->stbuf->ia_size, gf_posix_mt_char);
471 if (!databuf) {
472 goto err;
473 }
474
475 ret = sys_read(_fd, databuf, filler->stbuf->ia_size);
476 if (ret == -1) {
477 gf_msg(filler->this->name, GF_LOG_ERROR, errno,
478 P_MSG_XDATA_GETXATTR, "Read on file %s failed",
479 filler->real_path);
480 goto err;
481 }
482
483 ret = sys_close(_fd);
484 _fd = -1;
485 if (ret == -1) {
486 gf_msg(filler->this->name, GF_LOG_ERROR, errno,
487 P_MSG_XDATA_GETXATTR, "Close on file %s failed",
488 filler->real_path);
489 goto err;
490 }
491
492 ret = dict_set_bin(filler->xattr, key, databuf,
493 filler->stbuf->ia_size);
494 if (ret < 0) {
495 gf_msg(filler->this->name, GF_LOG_ERROR, 0,
496 P_MSG_XDATA_GETXATTR,
497 "failed to set dict value. key: %s,"
498 "path: %s",
499 key, filler->real_path);
500 goto err;
501 }
502
503 /* To avoid double free in cleanup below */
504 databuf = NULL;
505 err:
506 if (_fd != -1)
507 sys_close(_fd);
508 GF_FREE(databuf);
509 }
510 } else if (len == SLEN(GLUSTERFS_OPEN_FD_COUNT) &&
511 !strcmp(key, GLUSTERFS_OPEN_FD_COUNT)) {
512 inode = _get_filler_inode(filler);
513 if (!inode || gf_uuid_is_null(inode->gfid))
514 goto out;
515 ret = dict_set_uint32(filler->xattr, key, inode->fd_count);
516 if (ret < 0) {
517 gf_msg(filler->this->name, GF_LOG_WARNING, 0, P_MSG_DICT_SET_FAILED,
518 "Failed to set dictionary value for %s", key);
519 }
520 } else if (len == SLEN(GLUSTERFS_ACTIVE_FD_COUNT) &&
521 !strcmp(key, GLUSTERFS_ACTIVE_FD_COUNT)) {
522 inode = _get_filler_inode(filler);
523 if (!inode || gf_uuid_is_null(inode->gfid))
524 goto out;
525 ret = dict_set_uint32(filler->xattr, key, inode->active_fd_count);
526 if (ret < 0) {
527 gf_msg(filler->this->name, GF_LOG_WARNING, 0, P_MSG_DICT_SET_FAILED,
528 "Failed to set dictionary value for %s", key);
529 }
530 } else if (len == SLEN(GET_ANCESTRY_PATH_KEY) &&
531 !strcmp(key, GET_ANCESTRY_PATH_KEY)) {
532 /* As of now, the only consumers of POSIX_ANCESTRY_PATH attempt
533 * fetching it via path-based fops. Hence, leaving it as it is
534 * for now.
535 */
536 if (!filler->real_path)
537 goto out;
538 char *path = NULL;
539 ret = posix_get_ancestry(filler->this, filler->loc->inode, NULL, &path,
540 POSIX_ANCESTRY_PATH, &filler->op_errno,
541 xattr_req);
542 if (ret < 0) {
543 goto out;
544 }
545
546 ret = dict_set_dynstr_sizen(filler->xattr, GET_ANCESTRY_PATH_KEY, path);
547 if (ret < 0) {
548 GF_FREE(path);
549 goto out;
550 }
551
552 } else if (fnmatch(marker_contri_key, key, 0) == 0) {
553 ret = _posix_get_marker_quota_contributions(filler, key);
554 } else if (len == SLEN(GF_REQUEST_LINK_COUNT_XDATA) &&
555 strcmp(key, GF_REQUEST_LINK_COUNT_XDATA) == 0) {
556 ret = dict_set_sizen(filler->xattr, GF_REQUEST_LINK_COUNT_XDATA, data);
557 } else if (len == SLEN(GF_GET_SIZE) && strcmp(key, GF_GET_SIZE) == 0) {
558 if (filler->stbuf && IA_ISREG(filler->stbuf->ia_type)) {
559 ret = dict_set_uint64(filler->xattr, GF_GET_SIZE,
560 filler->stbuf->ia_size);
561 }
562 } else if (GF_POSIX_ACL_REQUEST(key)) {
563 if (filler->real_path)
564 ret = posix_pstat(filler->this, NULL, NULL, filler->real_path,
565 &stbuf, _gf_false, _gf_false);
566 else
567 ret = posix_fdstat(filler->this, filler->fd->inode, filler->fdnum,
568 &stbuf, _gf_false);
569 if (ret < 0) {
570 gf_msg(filler->this->name, GF_LOG_ERROR, errno,
571 P_MSG_XDATA_GETXATTR, "lstat on %s failed",
572 filler->real_path ?: uuid_utoa(filler->fd->inode->gfid));
573 goto out;
574 }
575
576 /* Avoid link follow in virt_pacl_get, donot fill acl for symlink.*/
577 if (IA_ISLNK(stbuf.ia_type))
578 goto out;
579
580 /* ACL_TYPE_DEFAULT is not supported for non-directory, skip */
581 if (!IA_ISDIR(stbuf.ia_type) &&
582 !strncmp(key, GF_POSIX_ACL_DEFAULT, SLEN(GF_POSIX_ACL_DEFAULT)))
583 goto out;
584
585 ret = posix_pacl_get(filler->real_path, filler->fdnum, key, &value);
586 if (ret || !value) {
587 gf_msg(filler->this->name, GF_LOG_ERROR, errno,
588 P_MSG_XDATA_GETXATTR, "could not get acl (%s) for %s, %d",
589 key, filler->real_path ?: uuid_utoa(filler->fd->inode->gfid),
590 ret);
591 goto out;
592 }
593
594 ret = dict_set_dynstrn(filler->xattr, (char *)key, len, value);
595 if (ret < 0) {
596 GF_FREE(value);
597 gf_msg(filler->this->name, GF_LOG_ERROR, errno,
598 P_MSG_XDATA_GETXATTR,
599 "could not set acl (%s) for %s in dictionary", key,
600 filler->real_path ?: uuid_utoa(filler->fd->inode->gfid));
601 goto out;
602 }
603 } else {
604 remaining_size = filler->list_size;
605 while (remaining_size > 0) {
606 xattr = filler->list + list_offset;
607 if (fnmatch(key, xattr, 0) == 0)
608 ret = _posix_xattr_get_set_from_backend(filler, xattr);
609 len = strlen(xattr);
610 remaining_size -= (len + 1);
611 list_offset += (len + 1);
612 }
613 }
614 out:
615 return 0;
616 }
617
618 static int
619 posix_fill_gfid_path(const char *path, struct iatt *iatt)
620 {
621 ssize_t size = 0;
622
623 size = sys_lgetxattr(path, GFID_XATTR_KEY, iatt->ia_gfid, 16);
624 /* Return value of getxattr */
625 if ((size == 16) || (size == -1))
626 return 0;
627 else
628 return size;
629 }
630
631 static int
632 posix_fill_gfid_fd(int fd, struct iatt *iatt)
633 {
634 ssize_t size = 0;
635
636 size = sys_fgetxattr(fd, GFID_XATTR_KEY, iatt->ia_gfid, 16);
637 /* Return value of getxattr */
638 if ((size == 16) || (size == -1))
639 return 0;
640 else
641 return size;
642 }
643
644 static void
645 posix_fill_ino_from_gfid(struct iatt *buf)
646 {
647 /* consider least significant 8 bytes of value out of gfid */
648 if (gf_uuid_is_null(buf->ia_gfid)) {
649 buf->ia_ino = -1;
650 } else {
651 buf->ia_flags |= IATT_INO;
652 buf->ia_ino = gfid_to_ino(buf->ia_gfid);
653 }
654 }
655
656 int
657 posix_fdstat(xlator_t *this, inode_t *inode, int fd, struct iatt *stbuf_p,
658 gf_boolean_t fetch_time)
659 {
660 int ret = 0;
661 struct stat fstatbuf;
662 struct posix_private *priv = NULL;
663
664 if (stbuf_p == NULL)
665 goto out;
666
667 ret = sys_fstat(fd, &fstatbuf);
668 if (ret != 0)
669 goto out;
670
671 if (fstatbuf.st_nlink && !S_ISDIR(fstatbuf.st_mode))
672 fstatbuf.st_nlink--;
673
674 iatt_from_stat(stbuf_p, &fstatbuf);
675
676 priv = this->private;
677 if (inode && fetch_time && priv->ctime) {
678 ret = posix_get_mdata_xattr(this, NULL, fd, inode, stbuf_p);
679 if (ret) {
680 gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_GETMDATA_FAILED,
681 "posix get mdata failed on gfid: %s",
682 uuid_utoa(inode->gfid));
683 goto out;
684 }
685 }
686 ret = posix_fill_gfid_fd(fd, stbuf_p);
687 stbuf_p->ia_flags |= IATT_GFID;
688
689 posix_fill_ino_from_gfid(stbuf_p);
690
691 out:
692 return ret;
693 }
694
695 /* The inode here is expected to update posix_mdata stored on disk.
696 * Don't use it as a general purpose inode and don't expect it to
697 * be always exists
698 */
699 int
700 posix_istat(xlator_t *this, inode_t *inode, uuid_t gfid, const char *basename,
701 struct iatt *buf_p, gf_boolean_t fetch_time)
702 {
703 char *real_path = NULL;
704 struct stat lstatbuf = {
705 0,
706 };
707 struct iatt stbuf = {
708 0,
709 };
710 int ret = 0;
711 struct posix_private *priv = NULL;
712
713 priv = this->private;
714
715 MAKE_HANDLE_PATH(real_path, this, gfid, basename);
716 if (!real_path) {
717 gf_msg(this->name, GF_LOG_ERROR, ESTALE, P_MSG_HANDLE_PATH_CREATE,
718 "Failed to create handle path for %s/%s", uuid_utoa(gfid),
719 basename ? basename : "");
720 errno = ESTALE;
721 ret = -1;
722 goto out;
723 }
724
725 ret = sys_lstat(real_path, &lstatbuf);
726
727 if (ret != 0) {
728 if (ret == -1) {
729 if (errno != ENOENT && errno != ELOOP)
730 gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_LSTAT_FAILED,
731 "lstat failed on %s", real_path);
732 } else {
733 // may be some backend filesystem issue
734 gf_msg(this->name, GF_LOG_ERROR, 0, P_MSG_LSTAT_FAILED,
735 "lstat failed on %s and return value is %d "
736 "instead of -1. Please see dmesg output to "
737 "check whether the failure is due to backend "
738 "filesystem issue",
739 real_path, ret);
740 ret = -1;
741 }
742 goto out;
743 }
744
745 if ((lstatbuf.st_ino == priv->handledir_st_ino) &&
746 (lstatbuf.st_dev == priv->handledir_st_dev)) {
747 errno = ENOENT;
748 return -1;
749 }
750
751 if (!S_ISDIR(lstatbuf.st_mode))
752 lstatbuf.st_nlink--;
753
754 iatt_from_stat(&stbuf, &lstatbuf);
755
756 if (inode && fetch_time && priv->ctime) {
757 ret = posix_get_mdata_xattr(this, real_path, -1, inode, &stbuf);
758 if (ret) {
759 gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_GETMDATA_FAILED,
760 "posix get mdata failed on %s", real_path);
761 goto out;
762 }
763 }
764
765 if (basename)
766 posix_fill_gfid_path(real_path, &stbuf);
767 else
768 gf_uuid_copy(stbuf.ia_gfid, gfid);
769 stbuf.ia_flags |= IATT_GFID;
770
771 posix_fill_ino_from_gfid(&stbuf);
772
773 if (buf_p)
774 memcpy(buf_p, &stbuf, sizeof(struct iatt));
775 out:
776 return ret;
777 }
778
779 int
780 posix_pstat(xlator_t *this, inode_t *inode, uuid_t gfid, const char *path,
781 struct iatt *buf_p, gf_boolean_t inode_locked,
782 gf_boolean_t fetch_time)
783 {
784 struct stat lstatbuf;
785 struct iatt stbuf = {
786 0,
787 };
788 int ret = 0;
789 int op_errno = 0;
790 struct posix_private *priv = NULL;
791
792 priv = this->private;
793
794 ret = sys_lstat(path, &lstatbuf);
795 if (ret != 0) {
796 if (errno != ENOENT) {
797 op_errno = errno;
798 gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_LSTAT_FAILED,
799 "lstat failed on %s", path);
800 errno = op_errno; /*gf_msg could have changed errno*/
801 } else {
802 op_errno = errno;
803 gf_msg_debug(this->name, errno, "lstat failed on %s ", path);
804 errno = op_errno; /*gf_msg could have changed errno*/
805 }
806 goto out;
807 }
808
809 if ((lstatbuf.st_ino == priv->handledir_st_ino) &&
810 (lstatbuf.st_dev == priv->handledir_st_dev)) {
811 errno = ENOENT;
812 return -1;
813 }
814
815 if (!S_ISDIR(lstatbuf.st_mode))
816 lstatbuf.st_nlink--;
817
818 if (gfid && !gf_uuid_is_null(gfid))
819 gf_uuid_copy(stbuf.ia_gfid, gfid);
820 else
821 posix_fill_gfid_path(path, &stbuf);
822 stbuf.ia_flags |= IATT_GFID;
823
824 iatt_from_stat(&stbuf, &lstatbuf);
825
826 if (fetch_time && priv->ctime) {
827 if (inode) {
828 if (!inode_locked) {
829 ret = posix_get_mdata_xattr(this, path, -1, inode, &stbuf);
830 } else {
831 ret = __posix_get_mdata_xattr(this, path, -1, inode, &stbuf);
832 }
833 if (ret) {
834 gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_GETMDATA_FAILED,
835 "posix get mdata failed on gfid: %s",
836 uuid_utoa(inode->gfid));
837 goto out;
838 }
839 } else {
840 ret = __posix_get_mdata_xattr(this, path, -1, NULL, &stbuf);
841 if (ret) {
842 gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_GETMDATA_FAILED,
843 "posix get mdata failed on path: %s", path);
844 goto out;
845 }
846 }
847 }
848
849 posix_fill_ino_from_gfid(&stbuf);
850
851 if (buf_p)
852 memcpy(buf_p, &stbuf, sizeof(struct iatt));
853 out:
854 return ret;
855 }
856
857 static void
858 _get_list_xattr(posix_xattr_filler_t *filler)
859 {
860 ssize_t size = 0;
861
862 if ((!filler) || ((!filler->real_path) && (filler->fdnum < 0)))
863 goto out;
864
865 if (filler->real_path)
866 size = sys_llistxattr(filler->real_path, NULL, 0);
867 else
868 size = sys_flistxattr(filler->fdnum, NULL, 0);
869
870 if (size <= 0)
871 goto out;
872
873 filler->list = GF_CALLOC(1, size, gf_posix_mt_char);
874 if (!filler->list)
875 goto out;
876
877 if (filler->real_path)
878 size = sys_llistxattr(filler->real_path, filler->list, size);
879 else
880 size = sys_flistxattr(filler->fdnum, filler->list, size);
881
882 filler->list_size = size;
883 out:
884 return;
885 }
886
887 static void
888 _handle_list_xattr(posix_xattr_filler_t *filler)
889 {
890 int32_t list_offset = 0;
891 ssize_t remaining_size = 0;
892 char *key = NULL;
893 int len;
894
895 remaining_size = filler->list_size;
896 while (remaining_size > 0) {
897 key = filler->list + list_offset;
898 len = strlen(key);
899
900 if (gf_get_index_by_elem(list_xattr_ignore_xattrs, key) >= 0)
901 goto next;
902
903 if (posix_special_xattr(marker_xattrs, key))
904 goto next;
905
906 if (posix_handle_georep_xattrs(NULL, key, NULL, _gf_false))
907 goto next;
908
909 if (posix_is_gfid2path_xattr(key))
910 goto next;
911
912 if (dict_getn(filler->xattr, key, len))
913 goto next;
914
915 (void)_posix_xattr_get_set_from_backend(filler, key);
916 next:
917 remaining_size -= (len + 1);
918 list_offset += (len + 1);
919
920 } /* while (remaining_size > 0) */
921 return;
922 }
923
924 dict_t *
925 posix_xattr_fill(xlator_t *this, const char *real_path, loc_t *loc, fd_t *fd,
926 int fdnum, dict_t *xattr_req, struct iatt *buf)
927 {
928 dict_t *xattr = NULL;
929 posix_xattr_filler_t filler = {
930 0,
931 };
932 gf_boolean_t list = _gf_false;
933
934 if (dict_get_sizen(xattr_req, "list-xattr")) {
935 dict_del_sizen(xattr_req, "list-xattr");
936 list = _gf_true;
937 }
938
939 xattr = dict_new();
940 if (!xattr) {
941 goto out;
942 }
943
944 filler.this = this;
945 filler.real_path = real_path;
946 filler.xattr = xattr;
947 filler.stbuf = buf;
948 filler.loc = loc;
949 filler.fd = fd;
950 filler.fdnum = fdnum;
951
952 _get_list_xattr(&filler);
953 dict_foreach(xattr_req, _posix_xattr_get_set, &filler);
954 if (list)
955 _handle_list_xattr(&filler);
956
957 GF_FREE(filler.list);
958 out:
959 return xattr;
960 }
961
962 void
963 posix_gfid_unset(xlator_t *this, dict_t *xdata)
964 {
965 uuid_t uuid = {
966 0,
967 };
968 int ret = 0;
969
970 if (xdata == NULL)
971 goto out;
972
973 ret = dict_get_gfuuid(xdata, "gfid-req", &uuid);
974 if (ret) {
975 goto out;
976 }
977
978 posix_handle_unset_gfid(this, uuid);
979 out:
980 return;
981 }
982
983 int
984 posix_gfid_set(xlator_t *this, const char *path, loc_t *loc, dict_t *xattr_req,
985 pid_t pid, int *op_errno)
986 {
987 uuid_t uuid_req;
988 uuid_t uuid_curr;
989 int ret = 0;
990 ssize_t size = 0;
991 struct stat stat = {
992 0,
993 };
994
995 *op_errno = 0;
996
997 if (!xattr_req) {
998 if (pid != GF_SERVER_PID_TRASH) {
999 gf_msg(this->name, GF_LOG_ERROR, EINVAL, P_MSG_INVALID_ARGUMENT,
1000 "xattr_req is null");
1001 *op_errno = EINVAL;
1002 ret = -1;
1003 }
1004 goto out;
1005 }
1006
1007 if (sys_lstat(path, &stat) != 0) {
1008 ret = -1;
1009 *op_errno = errno;
1010 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LSTAT_FAILED,
1011 "lstat on %s failed", path);
1012 goto out;
1013 }
1014
1015 size = sys_lgetxattr(path, GFID_XATTR_KEY, uuid_curr, 16);
1016 if (size == 16) {
1017 ret = 0;
1018 goto verify_handle;
1019 }
1020
1021 ret = dict_get_gfuuid(xattr_req, "gfid-req", &uuid_req);
1022 if (ret) {
1023 gf_msg_debug(this->name, 0, "failed to get the gfid from dict for %s",
1024 loc->path);
1025 *op_errno = -ret;
1026 ret = -1;
1027 goto out;
1028 }
1029 if (gf_uuid_is_null(uuid_req)) {
1030 gf_msg(this->name, GF_LOG_ERROR, EINVAL, P_MSG_NULL_GFID,
1031 "gfid is null for %s", loc ? loc->path : "");
1032 ret = -1;
1033 *op_errno = EINVAL;
1034 goto out;
1035 }
1036
1037 ret = sys_lsetxattr(path, GFID_XATTR_KEY, uuid_req, 16, XATTR_CREATE);
1038 if (ret == -1) {
1039 gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_GFID_FAILED,
1040 "setting GFID on %s failed ", path);
1041 goto out;
1042 }
1043 gf_uuid_copy(uuid_curr, uuid_req);
1044
1045 verify_handle:
1046 if (!S_ISDIR(stat.st_mode))
1047 ret = posix_handle_hard(this, path, uuid_curr, &stat);
1048 else
1049 ret = posix_handle_soft(this, path, loc, uuid_curr, &stat);
1050
1051 out:
1052 if (ret && !(*op_errno))
1053 *op_errno = errno;
1054 return ret;
1055 }
1056
1057 #ifdef HAVE_SYS_ACL_H
1058 static int
1059 posix_pacl_set(const char *path, int fdnum, const char *key, const char *acl_s)
1060 {
1061 int ret = -1;
1062 acl_t acl = NULL;
1063 acl_type_t type = 0;
1064
1065 if ((!path) && (fdnum < 0)) {
1066 errno = -EINVAL;
1067 return -1;
1068 }
1069
1070 type = gf_posix_acl_get_type(key);
1071 if (!type)
1072 return -1;
1073
1074 acl = acl_from_text(acl_s);
1075 if (!acl)
1076 return -1;
1077
1078 if (path)
1079 ret = acl_set_file(path, type, acl);
1080 else if (type == ACL_TYPE_ACCESS)
1081 ret = acl_set_fd(fdnum, acl);
1082 else {
1083 errno = -EINVAL;
1084 return -1;
1085 }
1086
1087 if (ret)
1088 /* posix_handle_pair expects ret to be the errno */
1089 ret = -errno;
1090
1091 acl_free(acl);
1092
1093 return ret;
1094 }
1095
1096 int
1097 posix_pacl_get(const char *path, int fdnum, const char *key, char **acl_s)
1098 {
1099 int ret = -1;
1100 acl_t acl = NULL;
1101 acl_type_t type = 0;
1102 char *acl_tmp = NULL;
1103
1104 if ((!path) && (fdnum < 0)) {
1105 errno = -EINVAL;
1106 return -1;
1107 }
1108
1109 type = gf_posix_acl_get_type(key);
1110 if (!type)
1111 return -1;
1112
1113 if (path)
1114 acl = acl_get_file(path, type);
1115 else if (type == ACL_TYPE_ACCESS)
1116 acl = acl_get_fd(fdnum);
1117 else {
1118 errno = -EINVAL;
1119 return -1;
1120 }
1121
1122 if (!acl)
1123 return -1;
1124
1125 #ifdef HAVE_ACL_LIBACL_H
1126 acl_tmp = acl_to_any_text(acl, NULL, ',',
1127 TEXT_ABBREVIATE | TEXT_NUMERIC_IDS);
1128 #else /* FreeBSD and the like */
1129 acl_tmp = acl_to_text_np(acl, NULL, ACL_TEXT_NUMERIC_IDS);
1130 #endif
1131 if (!acl_tmp)
1132 goto free_acl;
1133
1134 *acl_s = gf_strdup(acl_tmp);
1135 if (*acl_s)
1136 ret = 0;
1137
1138 acl_free(acl_tmp);
1139 free_acl:
1140 acl_free(acl);
1141
1142 return ret;
1143 }
1144 #else /* !HAVE_SYS_ACL_H (NetBSD) */
1145 int
1146 posix_pacl_set(const char *path, int fdnum, const char *key, const char *acl_s)
1147 {
1148 errno = ENOTSUP;
1149 return -1;
1150 }
1151
1152 int
1153 posix_pacl_get(const char *path, int fdnum, const char *key, char **acl_s)
1154 {
1155 errno = ENOTSUP;
1156 return -1;
1157 }
1158 #endif
1159
1160 #ifdef GF_DARWIN_HOST_OS
1161 static void
1162 posix_dump_buffer(xlator_t *this, const char *real_path, const char *key,
1163 data_t *value, int flags)
1164 {
1165 char buffer[3 * value->len + 1];
1166 int index = 0;
1167 buffer[0] = 0;
1168 gf_loglevel_t log_level = gf_log_get_loglevel();
1169 if (log_level == GF_LOG_TRACE) {
1170 char *data = (char *)value->data;
1171 for (index = 0; index < value->len; index++)
1172 sprintf(buffer + 3 * index, " %02x", data[index]);
1173 }
1174 gf_msg_debug(this->name, 0, "Dump %s: key:%s flags: %u length:%u data:%s ",
1175 real_path, key, flags, value->len,
1176 (log_level == GF_LOG_TRACE ? buffer : "<skipped in DEBUG>"));
1177 }
1178 #endif
1179
1180 int
1181 posix_handle_pair(xlator_t *this, loc_t *loc, const char *real_path, char *key,
1182 data_t *value, int flags, struct iatt *stbuf)
1183 {
1184 int sys_ret = -1;
1185 int ret = 0;
1186 int op_errno = 0;
1187 struct mdata_iatt mdata_iatt = {
1188 0,
1189 };
1190 #ifdef GF_DARWIN_HOST_OS
1191 const int error_code = EINVAL;
1192 #else
1193 const int error_code = EEXIST;
1194 #endif
1195
1196 if (XATTR_IS_PATHINFO(key)) {
1197 ret = -EACCES;
1198 goto out;
1199 } else if (posix_is_gfid2path_xattr(key)) {
1200 ret = -ENOTSUP;
1201 goto out;
1202 } else if (GF_POSIX_ACL_REQUEST(key)) {
1203 if (stbuf && IS_DHT_LINKFILE_MODE(stbuf))
1204 goto out;
1205 ret = posix_pacl_set(real_path, -1, key, value->data);
1206 } else if (!strncmp(key, POSIX_ACL_ACCESS_XATTR,
1207 SLEN(POSIX_ACL_ACCESS_XATTR)) &&
1208 stbuf && IS_DHT_LINKFILE_MODE(stbuf)) {
1209 goto out;
1210 } else if (!strncmp(key, GF_INTERNAL_CTX_KEY, SLEN(GF_INTERNAL_CTX_KEY)) ||
1211 !strncmp(key, GF_FORCE_REPLACE_KEY,
1212 SLEN(GF_FORCE_REPLACE_KEY))) {
1213 /* ignore this key value pair */
1214 ret = 0;
1215 goto out;
1216 } else if (!strncmp(key, GF_XATTR_MDATA_KEY, strlen(key))) {
1217 /* This is either by rebalance or self heal. Create the xattr if it's
1218 * not present. Compare and update the larger value if the xattr is
1219 * already present.
1220 */
1221 if (loc == NULL) {
1222 ret = -EINVAL;
1223 goto out;
1224 }
1225 posix_mdata_iatt_from_disk(&mdata_iatt,
1226 (posix_mdata_disk_t *)value->data);
1227 ret = posix_set_mdata_xattr_legacy_files(this, loc->inode, real_path,
1228 &mdata_iatt, &op_errno);
1229 if (ret != 0) {
1230 ret = -op_errno;
1231 }
1232 goto out;
1233 } else {
1234 sys_ret = sys_lsetxattr(real_path, key, value->data, value->len, flags);
1235 #ifdef GF_DARWIN_HOST_OS
1236 posix_dump_buffer(this, real_path, key, value, flags);
1237 #endif
1238 if (sys_ret < 0) {
1239 ret = -errno;
1240 if (errno == ENOENT) {
1241 if (!posix_special_xattr(marker_xattrs, key)) {
1242 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_XATTR_FAILED,
1243 "setxattr on %s failed", real_path);
1244 }
1245 } else {
1246 if (errno == error_code) {
1247 gf_msg_debug(this->name, 0,
1248 "%s: key:%s"
1249 "flags: %u length:%d",
1250 real_path, key, flags, value->len);
1251 } else {
1252 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_XATTR_FAILED,
1253 "%s: key:%s"
1254 "flags: %u length:%d",
1255 real_path, key, flags, value->len);
1256 }
1257 }
1258
1259 goto out;
1260 }
1261 }
1262 out:
1263 return ret;
1264 }
1265
1266 int
1267 posix_fhandle_pair(call_frame_t *frame, xlator_t *this, int fd, char *key,
1268 data_t *value, int flags, struct iatt *stbuf, fd_t *_fd)
1269 {
1270 int sys_ret = -1;
1271 int ret = 0;
1272
1273 if (XATTR_IS_PATHINFO(key)) {
1274 ret = -EACCES;
1275 goto out;
1276 } else if (posix_is_gfid2path_xattr(key)) {
1277 ret = -ENOTSUP;
1278 goto out;
1279 } else if (!strncmp(key, POSIX_ACL_ACCESS_XATTR,
1280 SLEN(POSIX_ACL_ACCESS_XATTR)) &&
1281 stbuf && IS_DHT_LINKFILE_MODE(stbuf)) {
1282 goto out;
1283 }
1284
1285 sys_ret = sys_fsetxattr(fd, key, value->data, value->len, flags);
1286
1287 if (sys_ret < 0) {
1288 ret = -errno;
1289 if (errno == ENOENT) {
1290 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_XATTR_FAILED,
1291 "fsetxattr on fd=%d"
1292 " failed",
1293 fd);
1294 } else {
1295 #ifdef GF_DARWIN_HOST_OS
1296 if (errno == EINVAL) {
1297 gf_msg_debug(this->name, errno, "fd=%d: key:%s error", fd, key);
1298 } else {
1299 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_XATTR_FAILED,
1300 "fd=%d: key:%s", fd, key);
1301 }
1302
1303 #else /* ! DARWIN */
1304 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_XATTR_FAILED,
1305 "fd=%d: key:%s", fd, key);
1306 #endif /* DARWIN */
1307 }
1308
1309 goto out;
1310 } else if (_fd) {
1311 posix_set_ctime(frame, this, NULL, fd, _fd->inode, NULL);
1312 }
1313
1314 out:
1315 return ret;
1316 }
1317
1318 static void
1319 del_stale_dir_handle(xlator_t *this, uuid_t gfid)
1320 {
1321 char newpath[PATH_MAX] = {
1322 0,
1323 };
1324 uuid_t gfid_curr = {
1325 0,
1326 };
1327 ssize_t size = -1;
1328 gf_boolean_t stale = _gf_false;
1329 char *hpath = NULL;
1330 struct stat stbuf = {
1331 0,
1332 };
1333 struct iatt iabuf = {
1334 0,
1335 };
1336
1337 MAKE_HANDLE_GFID_PATH(hpath, this, gfid);
1338
1339 /* check that it is valid directory handle */
1340 size = sys_lstat(hpath, &stbuf);
1341 if (size < 0) {
1342 gf_msg_debug(this->name, errno, "%s: Handle stat failed", hpath);
1343 goto out;
1344 }
1345
1346 iatt_from_stat(&iabuf, &stbuf);
1347 if (iabuf.ia_nlink != 1 || !IA_ISLNK(iabuf.ia_type)) {
1348 gf_msg_debug(this->name, 0, "%s: Handle nlink %d %d", hpath,
1349 iabuf.ia_nlink, IA_ISLNK(iabuf.ia_type));
1350 goto out;
1351 }
1352
1353 size = posix_handle_path(this, gfid, NULL, newpath, sizeof(newpath));
1354 if (size <= 0) {
1355 if (errno == ENOENT) {
1356 gf_msg_debug(this->name, ENOENT, "Failed for path: %s", newpath);
1357 stale = _gf_true;
1358 }
1359 goto out;
1360 }
1361
1362 size = sys_lgetxattr(newpath, GFID_XATTR_KEY, gfid_curr, 16);
1363 if (size < 0 && errno == ENOENT) {
1364 gf_msg_debug(this->name, ENOENT, "Failed for path: %s", newpath);
1365 stale = _gf_true;
1366 } else if (size == 16 && gf_uuid_compare(gfid, gfid_curr)) {
1367 gf_msg_debug(this->name, 0,
1368 "%s: mismatching gfid: %s, "
1369 "at %s",
1370 hpath, uuid_utoa(gfid_curr), newpath);
1371 stale = _gf_true;
1372 }
1373
1374 out:
1375 if (stale) {
1376 size = sys_unlink(hpath);
1377 if (size < 0 && errno != ENOENT)
1378 gf_msg(this->name, GF_LOG_ERROR, errno,
1379 P_MSG_STALE_HANDLE_REMOVE_FAILED,
1380 "%s: Failed"
1381 "to remove handle to %s",
1382 hpath, newpath);
1383 } else if (size == 16) {
1384 gf_msg_debug(this->name, 0,
1385 "%s: Fresh handle for "
1386 "%s with gfid %s",
1387 hpath, newpath, uuid_utoa(gfid_curr));
1388 }
1389 return;
1390 }
1391
1392 static int
1393 janitor_walker(const char *fpath, const struct stat *sb, int typeflag,
1394 struct FTW *ftwbuf)
1395 {
1396 struct iatt stbuf = {
1397 0,
1398 };
1399 xlator_t *this = NULL;
1400
1401 this = THIS;
1402 /* posix_mdata_t is not filled, no time or size attributes
1403 * are being used, so fine.
1404 */
1405 posix_pstat(this, NULL, NULL, fpath, &stbuf, _gf_false, _gf_false);
1406 switch (sb->st_mode & S_IFMT) {
1407 case S_IFREG:
1408 case S_IFBLK:
1409 case S_IFLNK:
1410 case S_IFCHR:
1411 case S_IFIFO:
1412 case S_IFSOCK:
1413 gf_msg_trace(THIS->name, 0, "unlinking %s", fpath);
1414 sys_unlink(fpath);
1415 if (stbuf.ia_nlink == 1)
1416 posix_handle_unset_gfid(this, stbuf.ia_gfid);
1417 break;
1418
1419 case S_IFDIR:
1420 if (ftwbuf->level) { /* don't remove top level dir */
1421 gf_msg_debug(THIS->name, 0, "removing directory %s", fpath);
1422
1423 sys_rmdir(fpath);
1424 del_stale_dir_handle(this, stbuf.ia_gfid);
1425 }
1426 break;
1427 }
1428
1429 return 0; /* 0 = FTW_CONTINUE */
1430 }
1431
1432 static void
1433 __posix_janitor_timer_start(xlator_t *this);
1434
1435 static int
1436 posix_janitor_task_done(int ret, call_frame_t *frame, void *data)
1437 {
1438 xlator_t *this = NULL;
1439 struct posix_private *priv = NULL;
1440
1441 this = data;
1442 priv = this->private;
1443
1444 pthread_mutex_lock(&priv->janitor_mutex);
1445 {
1446 if (priv->janitor_task_stop) {
1447 priv->janitor_task_stop = _gf_false;
1448 pthread_cond_signal(&priv->janitor_cond);
1449 pthread_mutex_unlock(&priv->janitor_mutex);
1450 goto out;
1451 }
1452 }
1453 pthread_mutex_unlock(&priv->janitor_mutex);
1454
1455 LOCK(&priv->lock);
1456 {
1457 __posix_janitor_timer_start(this);
1458 }
1459 UNLOCK(&priv->lock);
1460
1461 out:
1462 return 0;
1463 }
1464
1465 static int
1466 posix_janitor_task(void *data)
1467 {
1468 xlator_t *this = NULL;
1469 struct posix_private *priv = NULL;
1470 xlator_t *old_this = NULL;
1471
1472 time_t now;
1473
1474 this = data;
1475 priv = this->private;
1476 /* We need THIS to be set for janitor_walker */
1477 old_this = THIS;
1478 THIS = this;
1479
1480 if (!priv)
1481 goto out;
1482
1483 now = gf_time();
1484 if ((now - priv->last_landfill_check) > priv->janitor_sleep_duration) {
1485 if (priv->disable_landfill_purge) {
1486 gf_msg_debug(this->name, 0,
1487 "Janitor would have "
1488 "cleaned out %s, but purge"
1489 "is disabled.",
1490 priv->trash_path);
1491 } else {
1492 gf_msg_trace(this->name, 0, "janitor cleaning out %s",
1493 priv->trash_path);
1494
1495 nftw(priv->trash_path, janitor_walker, 32, FTW_DEPTH | FTW_PHYS);
1496 }
1497 priv->last_landfill_check = now;
1498 }
1499
1500 THIS = old_this;
1501
1502 out:
1503 return 0;
1504 }
1505
1506 static void
1507 posix_janitor_task_initator(struct gf_tw_timer_list *timer, void *data,
1508 unsigned long calltime)
1509 {
1510 xlator_t *this = NULL;
1511 int ret = 0;
1512
1513 this = data;
1514
1515 ret = synctask_new(this->ctx->env, posix_janitor_task,
1516 posix_janitor_task_done, NULL, this);
1517 if (ret < 0) {
1518 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_THREAD_FAILED,
1519 "spawning janitor "
1520 "thread failed");
1521 }
1522
1523 return;
1524 }
1525
1526 static void
1527 __posix_janitor_timer_start(xlator_t *this)
1528 {
1529 struct posix_private *priv = NULL;
1530 struct gf_tw_timer_list *timer = NULL;
1531
1532 priv = this->private;
1533 timer = priv->janitor;
1534
1535 INIT_LIST_HEAD(&timer->entry);
1536 timer->expires = priv->janitor_sleep_duration;
1537 timer->function = posix_janitor_task_initator;
1538 timer->data = this;
1539 gf_tw_add_timer(glusterfs_ctx_tw_get(this->ctx), timer);
1540
1541 return;
1542 }
1543
1544 void
1545 posix_janitor_timer_start(xlator_t *this)
1546 {
1547 struct posix_private *priv = NULL;
1548 struct gf_tw_timer_list *timer = NULL;
1549
1550 priv = this->private;
1551
1552 LOCK(&priv->lock);
1553 {
1554 if (!priv->janitor) {
1555 timer = GF_CALLOC(1, sizeof(struct gf_tw_timer_list),
1556 gf_common_mt_tw_timer_list);
1557 if (!timer) {
1558 goto unlock;
1559 }
1560 priv->janitor = timer;
1561 __posix_janitor_timer_start(this);
1562 }
1563 }
1564 unlock:
1565 UNLOCK(&priv->lock);
1566
1567 return;
1568 }
1569
1570 static struct posix_fd *
1571 janitor_get_next_fd(glusterfs_ctx_t *ctx)
1572 {
1573 struct posix_fd *pfd = NULL;
1574
1575 while (list_empty(&ctx->janitor_fds)) {
1576 if (ctx->pxl_count == 0) {
1577 return NULL;
1578 }
1579
1580 pthread_cond_wait(&ctx->fd_cond, &ctx->fd_lock);
1581 }
1582
1583 pfd = list_first_entry(&ctx->janitor_fds, struct posix_fd, list);
1584 list_del_init(&pfd->list);
1585
1586 return pfd;
1587 }
1588
1589 static void
1590 posix_close_pfd(xlator_t *xl, struct posix_fd *pfd)
1591 {
1592 THIS = xl;
1593
1594 if (pfd->dir == NULL) {
1595 gf_msg_trace(xl->name, 0, "janitor: closing file fd=%d", pfd->fd);
1596 sys_close(pfd->fd);
1597 } else {
1598 gf_msg_debug(xl->name, 0, "janitor: closing dir fd=%p", pfd->dir);
1599 sys_closedir(pfd->dir);
1600 }
1601
1602 GF_FREE(pfd);
1603 }
1604
1605 static void *
1606 posix_ctx_janitor_thread_proc(void *data)
1607 {
1608 xlator_t *xl;
1609 struct posix_fd *pfd;
1610 glusterfs_ctx_t *ctx = NULL;
1611 struct posix_private *priv_fd;
1612
1613 ctx = data;
1614
1615 pthread_mutex_lock(&ctx->fd_lock);
1616
1617 while ((pfd = janitor_get_next_fd(ctx)) != NULL) {
1618 pthread_mutex_unlock(&ctx->fd_lock);
1619
1620 xl = pfd->xl;
1621 posix_close_pfd(xl, pfd);
1622
1623 pthread_mutex_lock(&ctx->fd_lock);
1624
1625 priv_fd = xl->private;
1626 priv_fd->rel_fdcount--;
1627 if (!priv_fd->rel_fdcount)
1628 pthread_cond_signal(&priv_fd->fd_cond);
1629 }
1630
1631 pthread_mutex_unlock(&ctx->fd_lock);
1632
1633 return NULL;
1634 }
1635
1636 int
1637 posix_spawn_ctx_janitor_thread(xlator_t *this)
1638 {
1639 int ret = 0;
1640 glusterfs_ctx_t *ctx = NULL;
1641
1642 ctx = this->ctx;
1643
1644 pthread_mutex_lock(&ctx->fd_lock);
1645 {
1646 if (ctx->pxl_count++ == 0) {
1647 ret = gf_thread_create(&ctx->janitor, NULL,
1648 posix_ctx_janitor_thread_proc, ctx,
1649 "posixctxjan");
1650
1651 if (ret) {
1652 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_THREAD_FAILED,
1653 "spawning janitor thread failed");
1654 ctx->pxl_count--;
1655 }
1656 }
1657 }
1658 pthread_mutex_unlock(&ctx->fd_lock);
1659
1660 return ret;
1661 }
1662
1663 static int
1664 is_fresh_file(struct timespec *ts)
1665 {
1666 struct timespec now;
1667 int64_t elapsed;
1668
1669 timespec_now_realtime(&now);
1670 elapsed = gf_tsdiff(ts, &now);
1671
1672 if (elapsed < 0) {
1673 /* The file has been modified in the future !!!
1674 * Is it fresh ? previous implementation considered this as a
1675 * non-fresh file, so maintaining the same behavior. */
1676 return 0;
1677 }
1678
1679 /* If the file is newer than a second, we consider it fresh. */
1680 return elapsed < 1000000000;
1681 }
1682
1683 int
1684 posix_gfid_heal(xlator_t *this, const char *path, loc_t *loc, dict_t *xattr_req)
1685 {
1686 /* The purpose of this function is to prevent a race
1687 where an inode creation FOP (like mkdir/mknod/create etc)
1688 races with lookup in the following way:
1689
1690 {create thread} | {lookup thread}
1691 |
1692 t0
1693 mkdir ("name") |
1694 t1
1695 | posix_gfid_set ("name", 2);
1696 t2
1697 posix_gfid_set ("name", 1); |
1698 t3
1699 lstat ("name"); | lstat ("name");
1700
1701 In the above case mkdir FOP would have resulted with GFID 2 while
1702 it should have been GFID 1. It matters in the case where GFID would
1703 have gotten set to 1 on other subvolumes of replciate/distribute
1704
1705 The "solution" here is that, if we detect lookup is attempting to
1706 set a GFID on a file which is created very recently, but does not
1707 yet have a GFID (i.e, between t1 and t2), then "fake" it as though
1708 posix_gfid_heal was called at t0 instead.
1709 */
1710
1711 uuid_t uuid_curr;
1712 int ret = 0;
1713 struct stat stat = {
1714 0,
1715 };
1716 struct iatt stbuf = {
1717 0,
1718 };
1719 struct posix_private *priv = NULL;
1720
1721 priv = this->private;
1722
1723 if (!xattr_req)
1724 return 0;
1725
1726 if (loc->inode && priv->ctime) {
1727 if (sys_lstat(path, &stat) != 0) {
1728 return -errno;
1729 }
1730 /* stbuf is only to compare ctime, don't use it to access
1731 * other fields as they are zero. */
1732 ret = posix_get_mdata_xattr(this, path, -1, loc->inode, &stbuf);
1733 if (ret) {
1734 gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_GETMDATA_FAILED,
1735 "posix get mdata failed on gfid: %s",
1736 uuid_utoa(loc->inode->gfid));
1737 return -ENOENT;
1738 }
1739 ret = sys_lgetxattr(path, GFID_XATTR_KEY, uuid_curr, 16);
1740 if (ret != 16) {
1741 /* TODO: This is a very hacky way of doing this, and very prone to
1742 * errors and unexpected behavior. This should be changed. */
1743 struct timespec ts = {.tv_sec = stbuf.ia_ctime,
1744 .tv_nsec = stbuf.ia_ctime_nsec};
1745 if (is_fresh_file(&ts)) {
1746 gf_msg(this->name, GF_LOG_ERROR, ENOENT, P_MSG_FRESHFILE,
1747 "Fresh file: %s", path);
1748 return -ENOENT;
1749 }
1750 }
1751 } else {
1752 if (sys_lstat(path, &stat) != 0) {
1753 return -errno;
1754 }
1755 ret = sys_lgetxattr(path, GFID_XATTR_KEY, uuid_curr, 16);
1756 if (ret != 16) {
1757 /* TODO: This is a very hacky way of doing this, and very prone to
1758 * errors and unexpected behavior. This should be changed. */
1759 if (is_fresh_file(&stat.st_ctim)) {
1760 gf_msg(this->name, GF_LOG_ERROR, ENOENT, P_MSG_FRESHFILE,
1761 "Fresh file: %s", path);
1762 return -ENOENT;
1763 }
1764 }
1765 }
1766
1767 (void)posix_gfid_set(this, path, loc, xattr_req, GF_CLIENT_PID_MAX, &ret);
1768 return 0;
1769 }
1770
1771 static int
1772 _handle_entry_create_keyvalue_pair(dict_t *d, char *k, data_t *v, void *tmp)
1773 {
1774 int ret = -1;
1775 posix_xattr_filler_t *filler = NULL;
1776
1777 filler = tmp;
1778
1779 if (!strcmp(GFID_XATTR_KEY, k) || !strcmp("gfid-req", k) ||
1780 !strcmp(POSIX_ACL_DEFAULT_XATTR, k) ||
1781 !strcmp(POSIX_ACL_ACCESS_XATTR, k) || posix_xattr_ignorable(k)) {
1782 return 0;
1783 }
1784
1785 ret = posix_handle_pair(filler->this, filler->loc, filler->real_path, k, v,
1786 XATTR_CREATE, filler->stbuf);
1787 if (ret < 0) {
1788 errno = -ret;
1789 return -1;
1790 }
1791 return 0;
1792 }
1793
1794 int
1795 posix_entry_create_xattr_set(xlator_t *this, loc_t *loc, const char *path,
1796 dict_t *dict)
1797 {
1798 int ret = -1;
1799
1800 posix_xattr_filler_t filler = {
1801 0,
1802 };
1803
1804 if (!dict)
1805 goto out;
1806
1807 filler.this = this;
1808 filler.real_path = path;
1809 filler.stbuf = NULL;
1810 filler.loc = loc;
1811
1812 ret = dict_foreach(dict, _handle_entry_create_keyvalue_pair, &filler);
1813
1814 out:
1815 return ret;
1816 }
1817
1818 static int
1819 __posix_fd_ctx_get(fd_t *fd, xlator_t *this, struct posix_fd **pfd_p,
1820 int *op_errno_p)
1821 {
1822 struct posix_fd *pfd = NULL;
1823 int ret = -1;
1824 char *real_path = NULL;
1825 char *unlink_path = NULL;
1826 int _fd = -1;
1827 int op_errno = 0;
1828 DIR *dir = NULL;
1829
1830 struct posix_private *priv = NULL;
1831
1832 pfd = __fd_ctx_get_ptr(fd, this);
1833 if (pfd) {
1834 ret = 0;
1835 goto out;
1836 }
1837 if (!fd_is_anonymous(fd)) {
1838 gf_msg(this->name, GF_LOG_ERROR, 0, P_MSG_READ_FAILED,
1839 "Failed to get fd context for a non-anonymous fd, "
1840 "gfid: %s",
1841 uuid_utoa(fd->inode->gfid));
1842 op_errno = EINVAL;
1843 goto out;
1844 }
1845
1846 MAKE_HANDLE_PATH(real_path, this, fd->inode->gfid, NULL);
1847 if (!real_path) {
1848 gf_msg(this->name, GF_LOG_ERROR, 0, P_MSG_READ_FAILED,
1849 "Failed to create handle path (%s)", uuid_utoa(fd->inode->gfid));
1850 ret = -1;
1851 op_errno = EINVAL;
1852 goto out;
1853 }
1854 pfd = GF_CALLOC(1, sizeof(*pfd), gf_posix_mt_posix_fd);
1855 if (!pfd) {
1856 op_errno = ENOMEM;
1857 goto out;
1858 }
1859 pfd->fd = -1;
1860
1861 if (fd->inode->ia_type == IA_IFDIR) {
1862 dir = sys_opendir(real_path);
1863 if (!dir) {
1864 op_errno = errno;
1865 gf_msg(this->name, GF_LOG_ERROR, op_errno, P_MSG_READ_FAILED,
1866 "Failed to get anonymous fd for "
1867 "real_path: %s.",
1868 real_path);
1869 GF_FREE(pfd);
1870 pfd = NULL;
1871 goto out;
1872 }
1873 _fd = dirfd(dir);
1874 }
1875
1876 /* Using fd->flags in case we choose to have anonymous
1877 * fds with different flags some day. As of today it
1878 * would be GF_ANON_FD_FLAGS and nothing else.
1879 */
1880 if (fd->inode->ia_type == IA_IFREG) {
1881 _fd = open(real_path, fd->flags);
1882 if ((_fd == -1) && (errno == ENOENT)) {
1883 priv = this->private;
1884 POSIX_GET_FILE_UNLINK_PATH(priv->base_path, fd->inode->gfid,
1885 unlink_path);
1886 _fd = open(unlink_path, fd->flags);
1887 }
1888 if (_fd == -1) {
1889 op_errno = errno;
1890 gf_msg(this->name, GF_LOG_ERROR, op_errno, P_MSG_READ_FAILED,
1891 "Failed to get anonymous fd for "
1892 "real_path: %s.",
1893 real_path);
1894 GF_FREE(pfd);
1895 pfd = NULL;
1896 goto out;
1897 }
1898 }
1899
1900 pfd->fd = _fd;
1901 pfd->dir = dir;
1902 pfd->flags = fd->flags;
1903
1904 ret = __fd_ctx_set(fd, this, (uint64_t)(long)pfd);
1905 if (ret != 0) {
1906 op_errno = ENOMEM;
1907 if (_fd != -1)
1908 sys_close(_fd);
1909 if (dir)
1910 sys_closedir(dir);
1911 GF_FREE(pfd);
1912 pfd = NULL;
1913 goto out;
1914 }
1915
1916 ret = 0;
1917 out:
1918 if (ret < 0 && op_errno_p)
1919 *op_errno_p = op_errno;
1920
1921 if (pfd_p)
1922 *pfd_p = pfd;
1923 return ret;
1924 }
1925
1926 int
1927 posix_fd_ctx_get(fd_t *fd, xlator_t *this, struct posix_fd **pfd, int *op_errno)
1928 {
1929 int ret;
1930
1931 LOCK(&fd->inode->lock);
1932 {
1933 ret = __posix_fd_ctx_get(fd, this, pfd, op_errno);
1934 }
1935 UNLOCK(&fd->inode->lock);
1936
1937 return ret;
1938 }
1939
1940 static int
1941 posix_fs_health_check(xlator_t *this, char *file_path)
1942 {
1943 struct posix_private *priv = NULL;
1944 int ret = -1;
1945 char timestamp[GF_TIMESTR_SIZE] = {
1946 0,
1947 };
1948 int fd = -1;
1949 int timelen = -1;
1950 time_t time_sec = {
1951 0,
1952 };
1953 char buff[256] = {0};
1954 char *op = NULL;
1955 int op_errno = 0;
1956 int cnt;
1957 time_t timeout = 0;
1958 struct aiocb aiocb;
1959
1960 priv = this->private;
1961
1962 timeout = priv->health_check_timeout;
1963
1964 fd = open(file_path, O_CREAT | O_WRONLY | O_TRUNC, 0644);
1965 if (fd == -1) {
1966 op_errno = errno;
1967 op = "open_for_write";
1968 goto out;
1969 }
1970
1971 time_sec = gf_time();
1972 timelen = gf_time_fmt_FT(timestamp, sizeof timestamp, time_sec);
1973
1974 memset(&aiocb, 0, sizeof(struct aiocb));
1975 aiocb.aio_fildes = fd;
1976 aiocb.aio_buf = timestamp;
1977 aiocb.aio_nbytes = timelen;
1978 aiocb.aio_sigevent.sigev_notify = SIGEV_NONE;
1979 if (aio_write(&aiocb) == -1) {
1980 op_errno = errno;
1981 op = "aio_write";
1982 goto out;
1983 }
1984
1985 cnt = 0;
1986 /* Wait until write completion */
1987 while ((aio_error(&aiocb) == EINPROGRESS) && (++cnt <= timeout))
1988 sleep(1);
1989
1990 ret = aio_error(&aiocb);
1991 if (ret != 0) {
1992 op_errno = errno;
1993 op = "aio_write_error";
1994 goto out;
1995 }
1996
1997 ret = aio_return(&aiocb);
1998 if (ret != timelen) {
1999 op_errno = errno;
2000 op = "aio_write_buf";
2001 ret = -1;
2002 goto out;
2003 }
2004
2005 sys_close(fd);
2006
2007 fd = open(file_path, O_RDONLY);
2008 if (fd == -1) {
2009 op_errno = errno;
2010 op = "open_for_read";
2011 goto out;
2012 }
2013
2014 memset(&aiocb, 0, sizeof(struct aiocb));
2015 aiocb.aio_fildes = fd;
2016 aiocb.aio_buf = buff;
2017 aiocb.aio_nbytes = sizeof(buff);
2018 if (aio_read(&aiocb) == -1) {
2019 op_errno = errno;
2020 op = "aio_read";
2021 goto out;
2022 }
2023 cnt = 0;
2024 /* Wait until read completion */
2025 while ((aio_error(&aiocb) == EINPROGRESS) && (++cnt <= timeout))
2026 sleep(1);
2027
2028 ret = aio_error(&aiocb);
2029 if (ret != 0) {
2030 op_errno = errno;
2031 op = "aio_read_error";
2032 goto out;
2033 }
2034
2035 ret = aio_return(&aiocb);
2036 if (ret != timelen) {
2037 op_errno = errno;
2038 op = "aio_read_buf";
2039 ret = -1;
2040 goto out;
2041 }
2042
2043 if (memcmp(timestamp, buff, ret)) {
2044 op_errno = EUCLEAN;
2045 op = "aio_read_cmp_buf";
2046 ret = -1;
2047 goto out;
2048 }
2049 ret = 0;
2050 out:
2051 if (fd != -1) {
2052 sys_close(fd);
2053 }
2054
2055 if (ret && file_path[0]) {
2056 gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_HEALTHCHECK_FAILED,
2057 "%s() on %s returned ret is %d error is %s", op, file_path, ret,
2058 ret != -1 ? strerror(ret) : strerror(op_errno));
2059
2060 if ((op_errno == EAGAIN) || (ret == EAGAIN)) {
2061 ret = 0;
2062 } else {
2063 gf_event(EVENT_POSIX_HEALTH_CHECK_FAILED,
2064 "op=%s;path=%s;error=%s;brick=%s:%s timeout is %ld", op,
2065 file_path, strerror(op_errno), priv->hostname,
2066 priv->base_path, timeout);
2067 }
2068 }
2069 return ret;
2070 }
2071
2072 static void *
2073 posix_health_check_thread_proc(void *data)
2074 {
2075 xlator_t *this = data;
2076 struct posix_private *priv = this->private;
2077 uint32_t interval = priv->health_check_interval;
2078 int ret = -1;
2079 xlator_t *top = NULL;
2080 xlator_t *victim = NULL;
2081 xlator_list_t **trav_p = NULL;
2082 int count = 0;
2083 gf_boolean_t victim_found = _gf_false;
2084 glusterfs_ctx_t *ctx = THIS->ctx;
2085 char file_path[PATH_MAX];
2086
2087 /* prevent races when the interval is updated */
2088 if (interval == 0)
2089 goto out;
2090
2091 snprintf(file_path, sizeof(file_path) - 1, "%s/%s/health_check",
2092 priv->base_path, GF_HIDDEN_PATH);
2093
2094 gf_msg_debug(this->name, 0,
2095 "health-check thread started, "
2096 "on path %s, "
2097 "interval = %d seconds",
2098 file_path, interval);
2099 while (1) {
2100 /* aborting sleep() is a request to exit this thread, sleep()
2101 * will normally not return when cancelled */
2102 ret = sleep(interval);
2103 if (ret > 0)
2104 break;
2105 /* prevent thread errors while doing the health-check(s) */
2106 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
2107
2108 /* Do the health-check.*/
2109 ret = posix_fs_health_check(this, file_path);
2110 if (ret < 0 && priv->health_check_active)
2111 goto abort;
2112 if (!priv->health_check_active)
2113 goto out;
2114 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
2115 }
2116
2117 out:
2118 gf_msg_debug(this->name, 0, "health-check thread exiting");
2119
2120 LOCK(&priv->lock);
2121 {
2122 priv->health_check_active = _gf_false;
2123 }
2124 UNLOCK(&priv->lock);
2125
2126 return NULL;
2127
2128 abort:
2129 LOCK(&priv->lock);
2130 {
2131 priv->health_check_active = _gf_false;
2132 }
2133 UNLOCK(&priv->lock);
2134
2135 /* health-check failed */
2136 gf_msg(this->name, GF_LOG_EMERG, 0, P_MSG_HEALTHCHECK_FAILED,
2137 "health-check failed, going down");
2138
2139 xlator_notify(this->parents->xlator, GF_EVENT_CHILD_DOWN, this);
2140
2141 /* Below code is use to ensure if brick multiplexing is enabled if
2142 count is more than 1 it means brick mux has enabled
2143 */
2144 if (this->ctx->active) {
2145 top = this->ctx->active->first;
2146 LOCK(&ctx->volfile_lock);
2147 for (trav_p = &top->children; *trav_p; trav_p = &(*trav_p)->next) {
2148 count++;
2149 }
2150 UNLOCK(&ctx->volfile_lock);
2151 }
2152
2153 if (count == 1) {
2154 gf_msg(this->name, GF_LOG_EMERG, 0, P_MSG_HEALTHCHECK_FAILED,
2155 "still alive! -> SIGTERM");
2156 ret = sleep(30);
2157
2158 /* Need to kill the process only while brick mux has not enabled
2159 */
2160 if (ret == 0)
2161 kill(getpid(), SIGTERM);
2162
2163 ret = sleep(30);
2164 gf_msg(this->name, GF_LOG_EMERG, 0, P_MSG_HEALTHCHECK_FAILED,
2165 "still alive! -> SIGKILL");
2166 if (ret == 0)
2167 kill(getpid(), SIGKILL);
2168
2169 } else if (top) {
2170 LOCK(&ctx->volfile_lock);
2171 for (trav_p = &top->children; *trav_p; trav_p = &(*trav_p)->next) {
2172 victim = (*trav_p)->xlator;
2173 if (!victim->call_cleanup &&
2174 strcmp(victim->name, priv->base_path) == 0) {
2175 victim_found = _gf_true;
2176 break;
2177 }
2178 }
2179 UNLOCK(&ctx->volfile_lock);
2180 if (victim_found && !victim->cleanup_starting) {
2181 gf_log(THIS->name, GF_LOG_INFO,
2182 "detaching not-only "
2183 " child %s",
2184 priv->base_path);
2185 victim->cleanup_starting = 1;
2186 top->notify(top, GF_EVENT_CLEANUP, victim);
2187 }
2188 }
2189
2190 return NULL;
2191 }
2192
2193 int
2194 posix_spawn_health_check_thread(xlator_t *xl)
2195 {
2196 struct posix_private *priv = NULL;
2197 int ret = -1;
2198
2199 priv = xl->private;
2200
2201 LOCK(&priv->lock);
2202 {
2203 /* cancel the running thread */
2204 if (priv->health_check_active == _gf_true) {
2205 ret = gf_thread_cleanup_xint(priv->health_check);
2206 if (ret != 0) {
2207 gf_msg(xl->name, GF_LOG_ERROR, ret, P_MSG_HEALTHCHECK_FAILED,
2208 "failed to terminate health-check thread");
2209 ret = -1;
2210 goto unlock;
2211 }
2212 priv->health_check_active = _gf_false;
2213 }
2214
2215 /* prevent scheduling a check in a tight loop */
2216 if (priv->health_check_interval == 0)
2217 goto unlock;
2218
2219 ret = gf_thread_create(&priv->health_check, NULL,
2220 posix_health_check_thread_proc, xl, "posixhc");
2221 if (ret) {
2222 priv->health_check_interval = 0;
2223 priv->health_check_active = _gf_false;
2224 gf_msg(xl->name, GF_LOG_ERROR, errno, P_MSG_HEALTHCHECK_FAILED,
2225 "unable to setup health-check thread");
2226 goto unlock;
2227 }
2228
2229 priv->health_check_active = _gf_true;
2230 }
2231 unlock:
2232 UNLOCK(&priv->lock);
2233 return ret;
2234 }
2235
2236 void
2237 posix_disk_space_check(struct posix_private *priv)
2238 {
2239 char *subvol_path = NULL;
2240 int op_ret = 0;
2241 double size = 0;
2242 struct statvfs buf = {0};
2243 double freesz = 0;
2244
2245 GF_VALIDATE_OR_GOTO("posix-helpers", priv, out);
2246
2247 subvol_path = priv->base_path;
2248
2249 op_ret = sys_statvfs(subvol_path, &buf);
2250
2251 if (op_ret != 0) {
2252 gf_msg("posix-disk", GF_LOG_ERROR, errno, P_MSG_STATVFS_FAILED,
2253 "statvfs failed on %s", subvol_path);
2254 goto out;
2255 }
2256
2257 if (priv->disk_unit_percent) {
2258 size = ((buf.f_blocks * priv->disk_reserve) / 100);
2259 freesz = buf.f_bfree;
2260 } else {
2261 size = priv->disk_reserve;
2262 freesz = (buf.f_bfree * buf.f_bsize);
2263 }
2264
2265 if (freesz <= size) {
2266 priv->disk_space_full = 1;
2267 } else {
2268 priv->disk_space_full = 0;
2269 }
2270 out:
2271 return;
2272 }
2273
2274 static void *
2275 posix_ctx_disk_thread_proc(void *data)
2276 {
2277 struct posix_private *priv = NULL;
2278 glusterfs_ctx_t *ctx = NULL;
2279 uint32_t interval = 0;
2280 struct posix_diskxl *pthis = NULL;
2281 xlator_t *this = NULL;
2282 struct timespec sleep_till = {
2283 0,
2284 };
2285
2286 ctx = data;
2287 interval = 5;
2288
2289 gf_msg_debug("glusterfs_ctx", 0,
2290 "Ctx disk-space thread started, "
2291 "interval = %d seconds",
2292 interval);
2293
2294 pthread_mutex_lock(&ctx->xl_lock);
2295 {
2296 while (ctx->diskxl_count > 0) {
2297 list_for_each_entry(pthis, &ctx->diskth_xl, list)
2298 {
2299 pthis->is_use = _gf_true;
2300 pthread_mutex_unlock(&ctx->xl_lock);
2301
2302 THIS = this = pthis->xl;
2303 priv = this->private;
2304
2305 posix_disk_space_check(priv);
2306
2307 pthread_mutex_lock(&ctx->xl_lock);
2308 pthis->is_use = _gf_false;
2309 /* Send a signal to posix_notify function */
2310 if (pthis->detach_notify)
2311 pthread_cond_signal(&pthis->cond);
2312 }
2313
2314 timespec_now_realtime(&sleep_till);
2315 sleep_till.tv_sec += 5;
2316 (void)pthread_cond_timedwait(&ctx->xl_cond, &ctx->xl_lock,
2317 &sleep_till);
2318 }
2319 }
2320 pthread_mutex_unlock(&ctx->xl_lock);
2321
2322 return NULL;
2323 }
2324
2325 int
2326 posix_spawn_disk_space_check_thread(xlator_t *this)
2327 {
2328 int ret = 0;
2329 glusterfs_ctx_t *ctx = this->ctx;
2330 struct posix_diskxl *pxl = NULL;
2331 struct posix_private *priv = this->private;
2332
2333 pxl = GF_CALLOC(1, sizeof(struct posix_diskxl), gf_posix_mt_diskxl_t);
2334 if (!pxl) {
2335 ret = -ENOMEM;
2336 gf_log(this->name, GF_LOG_ERROR,
2337 "Calloc is failed to allocate "
2338 "memory for diskxl object");
2339 goto out;
2340 }
2341 pthread_cond_init(&pxl->cond, NULL);
2342
2343 pthread_mutex_lock(&ctx->xl_lock);
2344 {
2345 if (ctx->diskxl_count++ == 0) {
2346 ret = gf_thread_create(&ctx->disk_space_check, NULL,
2347 posix_ctx_disk_thread_proc, ctx,
2348 "posixctxres");
2349
2350 if (ret) {
2351 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_THREAD_FAILED,
2352 "spawning disk space check thread failed");
2353 ctx->diskxl_count--;
2354 pthread_mutex_unlock(&ctx->xl_lock);
2355 goto out;
2356 }
2357 }
2358 pxl->xl = this;
2359 priv->pxl = (void *)pxl;
2360 list_add_tail(&pxl->list, &ctx->diskth_xl);
2361 }
2362 pthread_mutex_unlock(&ctx->xl_lock);
2363
2364 out:
2365 if (ret) {
2366 if (pxl) {
2367 pthread_cond_destroy(&pxl->cond);
2368 GF_FREE(pxl);
2369 }
2370 }
2371 return ret;
2372 }
2373
2374 static int
2375 posix_fsyncer_pick(struct posix_private *priv, struct list_head *head)
2376 {
2377 int count = 0;
2378
2379 pthread_mutex_lock(&priv->fsync_mutex);
2380 {
2381 while (list_empty(&priv->fsyncs))
2382 pthread_cond_wait(&priv->fsync_cond, &priv->fsync_mutex);
2383
2384 count = priv->fsync_queue_count;
2385 priv->fsync_queue_count = 0;
2386 list_splice_init(&priv->fsyncs, head);
2387 }
2388 pthread_mutex_unlock(&priv->fsync_mutex);
2389
2390 return count;
2391 }
2392
2393 static void
2394 posix_fsyncer_process(xlator_t *this, call_stub_t *stub, gf_boolean_t do_fsync)
2395 {
2396 struct posix_fd *pfd = NULL;
2397 int ret = -1;
2398 int op_errno = 0;
2399
2400 ret = posix_fd_ctx_get(stub->args.fd, this, &pfd, &op_errno);
2401 if (ret < 0) {
2402 gf_msg(this->name, GF_LOG_ERROR, op_errno, P_MSG_GET_FDCTX_FAILED,
2403 "could not get fdctx for fd(%s)",
2404 uuid_utoa(stub->args.fd->inode->gfid));
2405 call_unwind_error(stub, -1, op_errno);
2406 return;
2407 }
2408
2409 if (do_fsync && pfd) {
2410 if (stub->args.datasync)
2411 ret = sys_fdatasync(pfd->fd);
2412 else
2413 ret = sys_fsync(pfd->fd);
2414 } else {
2415 ret = 0;
2416 }
2417
2418 if (ret) {
2419 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_FSTAT_FAILED,
2420 "could not fstat fd(%s)", uuid_utoa(stub->args.fd->inode->gfid));
2421 call_unwind_error(stub, -1, errno);
2422 return;
2423 }
2424
2425 call_unwind_error(stub, 0, 0);
2426 }
2427
2428 static int
2429 gf_syncfs(int fd)
2430 {
2431 int ret = 0;
2432 #if defined(HAVE_SYNCFS)
2433 /* Linux with glibc recent enough. */
2434 ret = syncfs(fd);
2435 #elif defined(HAVE_SYNCFS_SYS)
2436 /* Linux with no library function. */
2437 ret = syscall(SYS_syncfs, fd);
2438 #else
2439 /* Fallback to generic UNIX stuff. */
2440 sync();
2441 #endif
2442 return ret;
2443 }
2444
2445 static void
2446 posix_fsyncer_syncfs(xlator_t *this, struct list_head *head)
2447 {
2448 call_stub_t *stub = NULL;
2449 struct posix_fd *pfd = NULL;
2450 int ret = -1;
2451
2452 stub = list_entry(head->prev, call_stub_t, list);
2453 ret = posix_fd_ctx_get(stub->args.fd, this, &pfd, NULL);
2454 if (!ret)
2455 (void)gf_syncfs(pfd->fd);
2456 }
2457
2458 void *
2459 posix_fsyncer(void *d)
2460 {
2461 xlator_t *this = d;
2462 struct posix_private *priv = NULL;
2463 call_stub_t *stub = NULL;
2464 call_stub_t *tmp = NULL;
2465 struct list_head list;
2466 int count = 0;
2467 gf_boolean_t do_fsync = _gf_true;
2468
2469 priv = this->private;
2470
2471 for (;;) {
2472 INIT_LIST_HEAD(&list);
2473
2474 count = posix_fsyncer_pick(priv, &list);
2475
2476 gf_nanosleep(priv->batch_fsync_delay_usec * GF_US_IN_NS);
2477
2478 gf_msg_debug(this->name, 0, "picked %d fsyncs", count);
2479
2480 switch (priv->batch_fsync_mode) {
2481 case BATCH_NONE:
2482 case BATCH_REVERSE_FSYNC:
2483 break;
2484 case BATCH_SYNCFS:
2485 case BATCH_SYNCFS_SINGLE_FSYNC:
2486 case BATCH_SYNCFS_REVERSE_FSYNC:
2487 posix_fsyncer_syncfs(this, &list);
2488 break;
2489 }
2490
2491 if (priv->batch_fsync_mode == BATCH_SYNCFS)
2492 do_fsync = _gf_false;
2493 else
2494 do_fsync = _gf_true;
2495
2496 list_for_each_entry_safe_reverse(stub, tmp, &list, list)
2497 {
2498 list_del_init(&stub->list);
2499
2500 posix_fsyncer_process(this, stub, do_fsync);
2501
2502 if (priv->batch_fsync_mode == BATCH_SYNCFS_SINGLE_FSYNC)
2503 do_fsync = _gf_false;
2504 }
2505 }
2506 }
2507
2508 /**
2509 * TODO: move fd/inode interfaces into a single routine..
2510 */
2511 static int32_t
2512 posix_fetch_signature_xattr(char *real_path, const char *key, dict_t *xattr,
2513 size_t *xsize)
2514 {
2515 int32_t ret = 0;
2516 char *memptr = NULL;
2517 ssize_t xattrsize = 0;
2518 char val_buf[2048] = {
2519 0,
2520 };
2521 gf_boolean_t have_val = _gf_false;
2522
2523 xattrsize = sys_lgetxattr(real_path, key, val_buf, sizeof(val_buf) - 1);
2524 if (xattrsize >= 0) {
2525 have_val = _gf_true;
2526 } else {
2527 if (errno == ERANGE)
2528 xattrsize = sys_lgetxattr(real_path, key, NULL, 0);
2529 if ((errno == ENOATTR) || (errno == ENODATA))
2530 return 0;
2531 if (xattrsize == -1)
2532 goto error_return;
2533 }
2534 memptr = GF_MALLOC(xattrsize + 1, gf_posix_mt_char);
2535 if (!memptr)
2536 goto error_return;
2537 if (have_val) {
2538 memcpy(memptr, val_buf, xattrsize);
2539 memptr[xattrsize] = '\0';
2540 } else {
2541 bzero(memptr, xattrsize + 1);
2542 ret = sys_lgetxattr(real_path, key, memptr, xattrsize);
2543 if (ret == -1)
2544 goto freemem;
2545 }
2546 ret = dict_set_dynptr(xattr, (char *)key, memptr, xattrsize);
2547 if (ret)
2548 goto freemem;
2549
2550 if (xsize)
2551 *xsize = xattrsize;
2552
2553 return 0;
2554
2555 freemem:
2556 GF_FREE(memptr);
2557 error_return:
2558 return -1;
2559 }
2560
2561 static int32_t
2562 posix_fd_fetch_signature_xattr(int fd, const char *key, dict_t *xattr,
2563 size_t *xsize)
2564 {
2565 int32_t ret = 0;
2566 char *memptr = NULL;
2567 ssize_t xattrsize = 0;
2568
2569 xattrsize = sys_fgetxattr(fd, key, NULL, 0);
2570 if ((xattrsize == -1) && ((errno == ENOATTR) || (errno == ENODATA)))
2571 return 0;
2572 if (xattrsize == -1)
2573 goto error_return;
2574
2575 memptr = GF_CALLOC(xattrsize + 1, sizeof(char), gf_posix_mt_char);
2576 if (!memptr)
2577 goto error_return;
2578 ret = sys_fgetxattr(fd, key, memptr, xattrsize);
2579 if (ret == -1)
2580 goto freemem;
2581
2582 ret = dict_set_dynptr(xattr, (char *)key, memptr, xattrsize);
2583 if (ret)
2584 goto freemem;
2585
2586 if (xsize)
2587 *xsize = xattrsize;
2588
2589 return 0;
2590
2591 freemem:
2592 GF_FREE(memptr);
2593 error_return:
2594 return -1;
2595 }
2596
2597 /**
2598 * Fetch on-disk ongoing version and object signature extended attribute.
2599 * Be generous to absence of xattrs (just *absence*, other errors are
2600 * propagated up to the invoker), higher layer (br-stub) takes care of
2601 * interpreting the xattrs for anomalies.
2602 */
2603 int32_t
2604 posix_get_objectsignature(char *real_path, dict_t *xattr)
2605 {
2606 int32_t ret = 0;
2607 size_t signsize = 0;
2608
2609 ret = posix_fetch_signature_xattr(real_path, BITROT_CURRENT_VERSION_KEY,
2610 xattr, NULL);
2611 if (ret)
2612 goto error_return;
2613
2614 ret = posix_fetch_signature_xattr(real_path, BITROT_SIGNING_VERSION_KEY,
2615 xattr, &signsize);
2616 if (ret)
2617 goto delkey1;
2618
2619 ret = dict_set_uint32(xattr, BITROT_SIGNING_XATTR_SIZE_KEY,
2620 (uint32_t)signsize);
2621 if (ret)
2622 goto delkey2;
2623
2624 return 0;
2625
2626 delkey2:
2627 dict_del(xattr, BITROT_SIGNING_VERSION_KEY);
2628 delkey1:
2629 dict_del(xattr, BITROT_CURRENT_VERSION_KEY);
2630 error_return:
2631 return -EINVAL;
2632 }
2633
2634 int32_t
2635 posix_fdget_objectsignature(int fd, dict_t *xattr)
2636 {
2637 int32_t ret = 0;
2638 size_t signsize = 0;
2639
2640 ret = posix_fd_fetch_signature_xattr(fd, BITROT_CURRENT_VERSION_KEY, xattr,
2641 NULL);
2642 if (ret)
2643 goto error_return;
2644
2645 ret = posix_fd_fetch_signature_xattr(fd, BITROT_SIGNING_VERSION_KEY, xattr,
2646 &signsize);
2647 if (ret)
2648 goto delkey1;
2649
2650 ret = dict_set_uint32(xattr, BITROT_SIGNING_XATTR_SIZE_KEY,
2651 (uint32_t)signsize);
2652 if (ret)
2653 goto delkey2;
2654
2655 return 0;
2656
2657 delkey2:
2658 dict_del(xattr, BITROT_SIGNING_VERSION_KEY);
2659 delkey1:
2660 dict_del(xattr, BITROT_CURRENT_VERSION_KEY);
2661 error_return:
2662 return -EINVAL;
2663 }
2664
2665 /*
2666 * posix_resolve_dirgfid_to_path:
2667 * It converts given dirgfid to path by doing recursive readlinks at the
2668 * backend. If bname is given, it suffixes bname to dir path to form the
2669 * complete path else it doesn't. It allocates memory for the path and is
2670 * caller's responsibility to free the same. If bname is NULL and pargfid
2671 * is ROOT, then it returns "/"
2672 **/
2673
2674 int32_t
2675 posix_resolve_dirgfid_to_path(const uuid_t dirgfid, const char *brick_path,
2676 const char *bname, char **path)
2677 {
2678 char *linkname = NULL;
2679 char *dir_handle = NULL;
2680 char *pgfidstr = NULL;
2681 char *saveptr = NULL;
2682 ssize_t len = 0;
2683 int ret = 0;
2684 uuid_t tmp_gfid = {
2685 0,
2686 };
2687 uuid_t pargfid = {
2688 0,
2689 };
2690 char gpath[PATH_MAX] = {
2691 0,
2692 };
2693 char result[PATH_MAX] = {
2694 0,
2695 };
2696 char result1[PATH_MAX] = {
2697 0,
2698 };
2699 char *dir_name = NULL;
2700 char pre_dir_name[PATH_MAX] = {
2701 0,
2702 };
2703 xlator_t *this = NULL;
2704
2705 this = THIS;
2706 GF_ASSERT(this);
2707
2708 gf_uuid_copy(pargfid, dirgfid);
2709 if (!path || gf_uuid_is_null(pargfid)) {
2710 ret = -1;
2711 goto out;
2712 }
2713
2714 if (__is_root_gfid(pargfid)) {
2715 if (bname) {
2716 snprintf(result, PATH_MAX, "/%s", bname);
2717 *path = gf_strdup(result);
2718 } else {
2719 *path = gf_strdup("/");
2720 }
2721 return ret;
2722 }
2723
2724 dir_handle = alloca(PATH_MAX);
2725 linkname = alloca(PATH_MAX);
2726 (void)snprintf(gpath, PATH_MAX, "%s/.glusterfs/", brick_path);
2727
2728 while (!(__is_root_gfid(pargfid))) {
2729 len = snprintf(dir_handle, PATH_MAX, "%s/%02x/%02x/%s", gpath,
2730 pargfid[0], pargfid[1], uuid_utoa(pargfid));
2731 if ((len < 0) || (len >= PATH_MAX)) {
2732 ret = -1;
2733 goto out;
2734 }
2735
2736 len = sys_readlink(dir_handle, linkname, PATH_MAX);
2737 if (len < 0) {
2738 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_READLINK_FAILED,
2739 "could not read the "
2740 "link from the gfid handle %s",
2741 dir_handle);
2742 ret = -1;
2743 goto out;
2744 }
2745
2746 linkname[len] = '\0';
2747
2748 pgfidstr = strtok_r(linkname + SLEN("../../00/00/"), "/", &saveptr);
2749 dir_name = strtok_r(NULL, "/", &saveptr);
2750
2751 if (pre_dir_name[0] != '\0') { /* Remove '/' at the end */
2752 len = snprintf(result, PATH_MAX, "%s/%s", dir_name, pre_dir_name);
2753 } else {
2754 len = snprintf(result, PATH_MAX, "%s", dir_name);
2755 }
2756 if ((len < 0) || (len >= PATH_MAX)) {
2757 ret = -1;
2758 goto out;
2759 }
2760
2761 snprintf(pre_dir_name, sizeof(pre_dir_name), "%s", result);
2762
2763 gf_uuid_parse(pgfidstr, tmp_gfid);
2764 gf_uuid_copy(pargfid, tmp_gfid);
2765 }
2766
2767 if (bname) {
2768 len = snprintf(result1, PATH_MAX, "/%s/%s", result, bname);
2769 } else {
2770 len = snprintf(result1, PATH_MAX, "/%s", result);
2771 }
2772 if ((len < 0) || (len >= PATH_MAX)) {
2773 ret = -1;
2774 goto out;
2775 }
2776
2777 *path = gf_strdup(result1);
2778 if (*path == NULL) {
2779 ret = -1;
2780 goto out;
2781 }
2782
2783 out:
2784 return ret;
2785 }
2786
2787 posix_inode_ctx_t *
2788 __posix_inode_ctx_get(inode_t *inode, xlator_t *this)
2789 {
2790 int ret = -1;
2791 uint64_t ctx_uint = 0;
2792 posix_inode_ctx_t *ctx_p = NULL;
2793
2794 ret = __inode_ctx_get(inode, this, &ctx_uint);
2795 if (ret == 0) {
2796 return (posix_inode_ctx_t *)(uintptr_t)ctx_uint;
2797 }
2798
2799 ctx_p = GF_CALLOC(1, sizeof(*ctx_p), gf_posix_mt_inode_ctx_t);
2800 if (!ctx_p)
2801 return NULL;
2802
2803 pthread_mutex_init(&ctx_p->xattrop_lock, NULL);
2804 pthread_mutex_init(&ctx_p->write_atomic_lock, NULL);
2805 pthread_mutex_init(&ctx_p->pgfid_lock, NULL);
2806
2807 ctx_uint = (uint64_t)(uintptr_t)ctx_p;
2808 ret = __inode_ctx_set(inode, this, &ctx_uint);
2809 if (ret < 0) {
2810 pthread_mutex_destroy(&ctx_p->xattrop_lock);
2811 pthread_mutex_destroy(&ctx_p->write_atomic_lock);
2812 pthread_mutex_destroy(&ctx_p->pgfid_lock);
2813 GF_FREE(ctx_p);
2814 return NULL;
2815 }
2816
2817 return ctx_p;
2818 }
2819
2820 int
2821 __posix_inode_ctx_set_unlink_flag(inode_t *inode, xlator_t *this, uint64_t ctx)
2822 {
2823 posix_inode_ctx_t *ctx_p = NULL;
2824
2825 ctx_p = __posix_inode_ctx_get(inode, this);
2826 if (ctx_p == NULL)
2827 return -1;
2828
2829 ctx_p->unlink_flag = ctx;
2830
2831 return 0;
2832 }
2833
2834 int
2835 posix_inode_ctx_set_unlink_flag(inode_t *inode, xlator_t *this, uint64_t ctx)
2836 {
2837 int ret = -1;
2838
2839 LOCK(&inode->lock);
2840 {
2841 ret = __posix_inode_ctx_set_unlink_flag(inode, this, ctx);
2842 }
2843 UNLOCK(&inode->lock);
2844
2845 return ret;
2846 }
2847
2848 int
2849 __posix_inode_ctx_get_all(inode_t *inode, xlator_t *this,
2850 posix_inode_ctx_t **ctx)
2851 {
2852 posix_inode_ctx_t *ctx_p = NULL;
2853
2854 ctx_p = __posix_inode_ctx_get(inode, this);
2855 if (ctx_p == NULL)
2856 return -1;
2857
2858 *ctx = ctx_p;
2859
2860 return 0;
2861 }
2862
2863 int
2864 posix_inode_ctx_get_all(inode_t *inode, xlator_t *this, posix_inode_ctx_t **ctx)
2865 {
2866 int ret = 0;
2867
2868 LOCK(&inode->lock);
2869 {
2870 ret = __posix_inode_ctx_get_all(inode, this, ctx);
2871 }
2872 UNLOCK(&inode->lock);
2873
2874 return ret;
2875 }
2876
2877 gf_boolean_t
2878 posix_is_bulk_removexattr(char *name, dict_t *xdata)
2879 {
2880 if (name && (name[0] == '\0') && xdata)
2881 return _gf_true;
2882 return _gf_false;
2883 }
2884
2885 int32_t
2886 posix_set_iatt_in_dict(dict_t *dict, struct iatt *preop, struct iatt *postop)
2887 {
2888 int ret = -1;
2889 struct iatt *stbuf = NULL;
2890 int32_t len = sizeof(struct iatt);
2891 struct iatt *prebuf = NULL;
2892 struct iatt *postbuf = NULL;
2893
2894 if (!dict)
2895 return ret;
2896
2897 if (postop) {
2898 stbuf = GF_MALLOC(len, gf_common_mt_char);
2899 if (!stbuf)
2900 goto out;
2901 memcpy(stbuf, postop, len);
2902 ret = dict_set_iatt(dict, DHT_IATT_IN_XDATA_KEY, stbuf, false);
2903 if (ret < 0) {
2904 GF_FREE(stbuf);
2905 goto out;
2906 }
2907 }
2908
2909 if (preop) {
2910 prebuf = GF_MALLOC(len, gf_common_mt_char);
2911 if (!prebuf)
2912 goto out;
2913 memcpy(prebuf, preop, len);
2914 ret = dict_set_iatt(dict, GF_PRESTAT, prebuf, false);
2915 if (ret < 0) {
2916 GF_FREE(prebuf);
2917 goto out;
2918 }
2919 }
2920
2921 if (postop) {
2922 postbuf = GF_MALLOC(len, gf_common_mt_char);
2923 if (!postbuf)
2924 goto out;
2925 memcpy(postbuf, postop, len);
2926 ret = dict_set_iatt(dict, GF_POSTSTAT, postbuf, false);
2927 if (ret < 0) {
2928 GF_FREE(postbuf);
2929 goto out;
2930 }
2931 }
2932
2933 ret = 0;
2934 out:
2935 return ret;
2936 }
2937
2938 mode_t
2939 posix_override_umask(mode_t mode, mode_t mode_bit)
2940 {
2941 gf_msg_debug("posix", 0, "The value of mode is %u", mode);
2942 mode = mode >> 9; /* 3x3 (bits for each octal digit)*/
2943 mode = (mode << 9) | mode_bit;
2944 gf_msg_debug("posix", 0, "The value of mode is %u", mode);
2945 return mode;
2946 }
2947
2948 int
2949 posix_check_internal_writes(xlator_t *this, inode_t *fd_inode, int sysfd,
2950 dict_t *xdata)
2951 {
2952 int ret = 0;
2953 data_t *val = NULL;
2954
2955 val = dict_get_sizen(xdata, GF_PROTECT_FROM_EXTERNAL_WRITES);
2956 if (!val) {
2957 val = dict_get_sizen(xdata, GF_AVOID_OVERWRITE);
2958 if (!val) // none of the items were found in the dict, return
2959 goto out;
2960 else // the 2nd item, GF_AVOID_OVERWRITE is available, we don't need
2961 // val
2962 val = NULL;
2963 }
2964
2965 LOCK(&fd_inode->lock);
2966 {
2967 if (val) { // GF_PROTECT_FROM_EXTERNAL_WRITES was found
2968 ret = sys_fsetxattr(sysfd, GF_PROTECT_FROM_EXTERNAL_WRITES,
2969 val->data, val->len, 0);
2970 if (ret < 0) {
2971 gf_msg(this->name, GF_LOG_ERROR, P_MSG_XATTR_FAILED, errno,
2972 "setxattr failed key %s",
2973 GF_PROTECT_FROM_EXTERNAL_WRITES);
2974 }
2975 } else { // GF_AVOID_OVERWRITE was found, otherwise we would not be
2976 // here
2977 ret = sys_fgetxattr(sysfd, GF_PROTECT_FROM_EXTERNAL_WRITES, NULL,
2978 0);
2979 if ((ret < 0) && ((errno == ENOATTR) || (errno == ENODATA))) {
2980 ret = 0;
2981 } else {
2982 ret = -1;
2983 }
2984 }
2985 }
2986 UNLOCK(&fd_inode->lock);
2987
2988 out:
2989 return ret;
2990 }
2991
2992 gf_cs_obj_state
2993 posix_cs_heal_state(xlator_t *this, const char *realpath, int *fd,
2994 struct iatt *buf)
2995 {
2996 gf_boolean_t remote = _gf_false;
2997 gf_boolean_t downloading = _gf_false;
2998 int ret = 0;
2999 gf_cs_obj_state state = GF_CS_ERROR;
3000 ssize_t xattrsize = 0;
3001
3002 if (!buf) {
3003 ret = -1;
3004 goto out;
3005 }
3006
3007 if (fd) {
3008 xattrsize = sys_fgetxattr(*fd, GF_CS_OBJECT_REMOTE, NULL, 0);
3009 if ((xattrsize == -1) && ((errno == ENOATTR) || (errno == ENODATA))) {
3010 remote = _gf_false;
3011 } else if (xattrsize == -1) {
3012 ret = -1;
3013 gf_msg(this->name, GF_LOG_ERROR, 0, errno,
3014 "fgetxattr"
3015 " failed");
3016 state = GF_CS_ERROR;
3017 goto out;
3018 } else {
3019 remote = _gf_true;
3020 }
3021
3022 xattrsize = sys_fgetxattr(*fd, GF_CS_OBJECT_DOWNLOADING, NULL, 0);
3023 if ((xattrsize == -1) && ((errno == ENOATTR) || (errno == ENODATA))) {
3024 downloading = _gf_false;
3025 } else if (xattrsize == -1) {
3026 ret = -1;
3027 gf_msg(this->name, GF_LOG_ERROR, 0, errno,
3028 "fgetxattr"
3029 " failed");
3030 state = GF_CS_ERROR;
3031 goto out;
3032 } else {
3033 downloading = _gf_true;
3034 }
3035 } else {
3036 xattrsize = sys_lgetxattr(realpath, GF_CS_OBJECT_REMOTE, NULL, 0);
3037 if ((xattrsize == -1) && ((errno == ENOATTR) || (errno == ENODATA))) {
3038 remote = _gf_false;
3039 } else if (xattrsize == -1) {
3040 ret = -1;
3041 gf_msg(this->name, GF_LOG_ERROR, 0, errno,
3042 "getxattr"
3043 " failed");
3044 state = GF_CS_ERROR;
3045 goto out;
3046 } else {
3047 remote = _gf_true;
3048 }
3049
3050 xattrsize = sys_lgetxattr(realpath, GF_CS_OBJECT_DOWNLOADING, NULL, 0);
3051 if ((xattrsize == -1) && ((errno == ENOATTR) || (errno == ENODATA))) {
3052 downloading = _gf_false;
3053 } else if (xattrsize == -1) {
3054 ret = -1;
3055 gf_msg(this->name, GF_LOG_ERROR, 0, errno,
3056 "getxattr"
3057 " failed");
3058 state = GF_CS_ERROR;
3059 goto out;
3060 } else {
3061 downloading = _gf_true;
3062 }
3063 }
3064
3065 if (remote && downloading) {
3066 if (fd) {
3067 ret = sys_fremovexattr(*fd, GF_CS_OBJECT_DOWNLOADING);
3068 } else {
3069 ret = sys_lremovexattr(realpath, GF_CS_OBJECT_DOWNLOADING);
3070 }
3071
3072 if (ret) {
3073 gf_msg(this->name, GF_LOG_ERROR, 0, errno,
3074 "failed to remove xattr, repair failed");
3075 state = GF_CS_ERROR;
3076 goto out;
3077 }
3078
3079 if (buf->ia_size) {
3080 if (fd) {
3081 ret = sys_ftruncate(*fd, 0);
3082 } else {
3083 ret = sys_truncate(realpath, 0);
3084 }
3085
3086 if (ret) {
3087 gf_msg(this->name, GF_LOG_ERROR, 0, errno,
3088 "truncate failed. File is in inconsistent"
3089 " state");
3090 state = GF_CS_ERROR;
3091 goto out;
3092 }
3093 }
3094
3095 state = GF_CS_REMOTE;
3096 goto out;
3097
3098 } else if (remote) {
3099 if (buf->ia_size) {
3100 if (fd) {
3101 ret = sys_ftruncate(*fd, 0);
3102 } else {
3103 ret = sys_truncate(realpath, 0);
3104 }
3105 if (ret) {
3106 gf_msg(this->name, GF_LOG_ERROR, 0, errno,
3107 "truncate failed. File is in inconsistent"
3108 " state");
3109 state = GF_CS_ERROR;
3110 goto out;
3111 }
3112 }
3113
3114 state = GF_CS_REMOTE;
3115 goto out;
3116 } else if (downloading) {
3117 if (buf->ia_size) {
3118 if (fd) {
3119 ret = sys_fremovexattr(*fd, GF_CS_OBJECT_DOWNLOADING);
3120 } else {
3121 ret = sys_lremovexattr(realpath, GF_CS_OBJECT_DOWNLOADING);
3122 }
3123
3124 if (ret) {
3125 gf_msg(this->name, GF_LOG_ERROR, 0, errno,
3126 "failed to remove xattr, repair failed");
3127 state = GF_CS_ERROR;
3128 goto out;
3129 }
3130
3131 state = GF_CS_LOCAL;
3132 goto out;
3133 }
3134 }
3135
3136 state = GF_CS_LOCAL;
3137 out:
3138 gf_msg_debug(this->name, 0, "heal state returned %d", state);
3139 return state;
3140 }
3141
3142 gf_cs_obj_state
3143 posix_cs_check_status(xlator_t *this, const char *realpath, int *fd,
3144 struct iatt *buf)
3145 {
3146 gf_boolean_t remote = _gf_false;
3147 gf_boolean_t downloading = _gf_false;
3148 int ret = 0;
3149 gf_cs_obj_state state = GF_CS_LOCAL;
3150 ssize_t xattrsize = 0;
3151 int op_errno = 0;
3152
3153 if (fd) {
3154 xattrsize = sys_fgetxattr(*fd, GF_CS_OBJECT_REMOTE, NULL, 0);
3155 if ((xattrsize == -1) && ((errno == ENOATTR) || (errno == ENODATA))) {
3156 remote = _gf_false;
3157 } else if (xattrsize == -1) {
3158 ret = -1;
3159 op_errno = errno;
3160 gf_msg(this->name, GF_LOG_ERROR, 0, 0,
3161 "getxattr "
3162 "failed err %d",
3163 errno);
3164 goto out;
3165 } else {
3166 remote = _gf_true;
3167 }
3168
3169 xattrsize = sys_fgetxattr(*fd, GF_CS_OBJECT_DOWNLOADING, NULL, 0);
3170 if ((xattrsize == -1) && ((errno == ENOATTR) || (errno == ENODATA))) {
3171 downloading = _gf_false;
3172 } else if (xattrsize == -1) {
3173 ret = -1;
3174 op_errno = errno;
3175 gf_msg(this->name, GF_LOG_ERROR, 0, 0,
3176 "getxattr "
3177 "failed err : %d",
3178 errno);
3179
3180 goto out;
3181 } else {
3182 downloading = _gf_true;
3183 }
3184 }
3185
3186 if (realpath) {
3187 xattrsize = sys_lgetxattr(realpath, GF_CS_OBJECT_REMOTE, NULL, 0);
3188 if ((xattrsize == -1) && ((errno == ENOATTR) || (errno == ENODATA))) {
3189 remote = _gf_false;
3190 } else if (xattrsize == -1) {
3191 ret = -1;
3192 op_errno = errno;
3193 gf_msg(this->name, GF_LOG_ERROR, 0, 0,
3194 "getxattr "
3195 "failed err : %d",
3196 errno);
3197 goto out;
3198 } else {
3199 remote = _gf_true;
3200 }
3201
3202 xattrsize = sys_lgetxattr(realpath, GF_CS_OBJECT_DOWNLOADING, NULL, 0);
3203 if ((xattrsize == -1) && ((errno == ENOATTR) || (errno == ENODATA))) {
3204 downloading = _gf_false;
3205 } else if (xattrsize == -1) {
3206 ret = -1;
3207 op_errno = errno;
3208 gf_msg(this->name, GF_LOG_ERROR, 0, 0,
3209 "getxattr "
3210 "failed err : %d",
3211 errno);
3212 goto out;
3213 } else {
3214 downloading = _gf_true;
3215 }
3216 }
3217
3218 out:
3219 if (ret) {
3220 gf_msg("POSIX", GF_LOG_ERROR, 0, op_errno,
3221 "getxattr failed "
3222 "with %d",
3223 op_errno);
3224 state = GF_CS_ERROR;
3225 return state;
3226 }
3227
3228 if ((remote && downloading) || (remote && buf && buf->ia_size)) {
3229 state = GF_CS_REPAIR;
3230 gf_msg_debug(this->name, 0, "status is REPAIR");
3231 return state;
3232 }
3233
3234 if (remote)
3235 state = GF_CS_REMOTE;
3236 else if (downloading)
3237 state = GF_CS_DOWNLOADING;
3238 else
3239 state = GF_CS_LOCAL;
3240
3241 gf_msg_debug(this->name, 0, "state returned is %d", state);
3242 return state;
3243 }
3244
3245 int
3246 posix_cs_set_state(xlator_t *this, dict_t **rsp, gf_cs_obj_state state,
3247 char const *path, int *fd)
3248 {
3249 int ret = 0;
3250 char *value = NULL;
3251 ssize_t xattrsize = 0;
3252
3253 if (!rsp) {
3254 ret = -1;
3255 goto out;
3256 }
3257
3258 if (!(*rsp)) {
3259 *rsp = dict_new();
3260 if (!(*rsp)) {
3261 gf_msg(this->name, GF_LOG_ERROR, 0, ENOMEM,
3262 "failed to"
3263 " create dict");
3264 ret = -1;
3265 goto out;
3266 }
3267 }
3268
3269 ret = dict_set_uint64(*rsp, GF_CS_OBJECT_STATUS, state);
3270 if (ret) {
3271 gf_msg(this->name, GF_LOG_ERROR, 0, ENOMEM,
3272 "failed to set "
3273 "dict");
3274 ret = -1;
3275 goto out;
3276 }
3277
3278 if (fd) {
3279 xattrsize = sys_fgetxattr(*fd, GF_CS_OBJECT_REMOTE, NULL, 0);
3280 if (xattrsize != -1) {
3281 value = GF_CALLOC(1, xattrsize + 1, gf_posix_mt_char);
3282 if (!value) {
3283 gf_msg(this->name, GF_LOG_ERROR, 0, 0, "no memory for value");
3284 ret = -1;
3285 goto out;
3286 }
3287 /* TODO: Add check for ENODATA */
3288 xattrsize = sys_fgetxattr(*fd, GF_CS_OBJECT_REMOTE, value,
3289 xattrsize + 1);
3290 if (xattrsize == -1) {
3291 gf_msg(this->name, GF_LOG_ERROR, 0, errno,
3292 " getxattr failed for key %s", GF_CS_OBJECT_REMOTE);
3293 goto out;
3294 } else {
3295 value[xattrsize] = '\0';
3296 }
3297 } else {
3298 gf_msg(this->name, GF_LOG_ERROR, 0, errno,
3299 " getxattr failed for key %s", GF_CS_OBJECT_REMOTE);
3300 goto out;
3301 }
3302 } else {
3303 xattrsize = sys_lgetxattr(path, GF_CS_OBJECT_REMOTE, NULL, 0);
3304 if (xattrsize != -1) {
3305 value = GF_CALLOC(1, xattrsize + 1, gf_posix_mt_char);
3306 if (!value) {
3307 ret = -1;
3308 goto out;
3309 }
3310
3311 xattrsize = sys_lgetxattr(path, GF_CS_OBJECT_REMOTE, value,
3312 xattrsize + 1);
3313 if (xattrsize == -1) {
3314 gf_msg(this->name, GF_LOG_ERROR, 0, errno,
3315 " getxattr failed for key %s", GF_CS_OBJECT_REMOTE);
3316 goto out;
3317 } else {
3318 value[xattrsize] = '\0';
3319 }
3320 } else {
3321 gf_msg(this->name, GF_LOG_ERROR, 0, errno,
3322 " getxattr failed for key %s", GF_CS_OBJECT_REMOTE);
3323 goto out;
3324 }
3325 }
3326
3327 if (ret == 0) {
3328 ret = dict_set_str_sizen(*rsp, GF_CS_OBJECT_REMOTE, value);
3329 if (ret) {
3330 gf_msg(this->name, GF_LOG_ERROR, 0, 0,
3331 "failed to set"
3332 "value");
3333 }
3334 }
3335
3336 out:
3337 return ret;
3338 }
3339
3340 /* This function checks the status of the file and updates the xattr response.
3341 * Also it repairs the state of the file which could have been resulted from a
3342 * crash or transient failures.
3343 */
3344 int
3345 posix_cs_maintenance(xlator_t *this, fd_t *fd, loc_t *loc, int *pfd,
3346 struct iatt *buf, const char *realpath,
3347 gf_boolean_t is_cs_obj_status,
3348 gf_boolean_t is_cs_obj_repair, dict_t **xattr_rsp,
3349 gf_boolean_t ignore_failure)
3350 {
3351 gf_cs_obj_state state = GF_CS_ERROR;
3352 int ret = 0;
3353
3354 if (fd) {
3355 LOCK(&fd->inode->lock);
3356 if (is_cs_obj_status) {
3357 state = posix_cs_check_status(this, NULL, pfd, buf);
3358 gf_msg_debug(this->name, 0, "state : %d", state);
3359 ret = posix_cs_set_state(this, xattr_rsp, state, NULL, pfd);
3360 if (ret) {
3361 gf_msg(this->name, GF_LOG_ERROR, 0, 0,
3362 "posix_cs_set_state failed");
3363 }
3364
3365 if (ignore_failure) {
3366 ret = 0;
3367 goto unlock;
3368 } else {
3369 if (state != GF_CS_LOCAL || ret != 0) {
3370 ret = -1;
3371 goto unlock;
3372 }
3373 }
3374 }
3375
3376 if (is_cs_obj_repair) {
3377 state = posix_cs_check_status(this, NULL, pfd, buf);
3378 gf_msg_debug(this->name, 0, "state : %d", state);
3379
3380 if (state == GF_CS_REPAIR) {
3381 state = posix_cs_heal_state(this, NULL, pfd, buf);
3382
3383 if (state == GF_CS_ERROR) {
3384 gf_msg(this->name, GF_LOG_ERROR, 0, 0,
3385 "repair check failed");
3386 }
3387 }
3388
3389 ret = posix_cs_set_state(this, xattr_rsp, state, NULL, pfd);
3390 if (ret) {
3391 gf_msg(this->name, GF_LOG_ERROR, 0, 0,
3392 "posix_cs_set_state failed");
3393 if (ignore_failure)
3394 ret = 0;
3395 else
3396 ret = -1;
3397 goto unlock;
3398 }
3399 }
3400 } else {
3401 if (!loc->inode) {
3402 ret = 0;
3403 goto out;
3404 }
3405
3406 LOCK(&loc->inode->lock);
3407 if (is_cs_obj_status) {
3408 state = posix_cs_check_status(this, realpath, NULL, buf);
3409 gf_msg_debug(this->name, 0, "state : %d", state);
3410 ret = posix_cs_set_state(this, xattr_rsp, state, realpath, NULL);
3411 if (ret) {
3412 gf_msg(this->name, GF_LOG_ERROR, 0, 0,
3413 "posix_cs_set_state failed");
3414 }
3415
3416 if (ignore_failure) {
3417 ret = 0;
3418 goto unlock;
3419 } else {
3420 if (state != GF_CS_LOCAL || ret != 0) {
3421 ret = -1;
3422 goto unlock;
3423 }
3424 }
3425 }
3426
3427 if (is_cs_obj_repair) {
3428 state = posix_cs_check_status(this, realpath, NULL, buf);
3429 gf_msg_debug(this->name, 0, "state : %d", state);
3430
3431 if (state == GF_CS_REPAIR) {
3432 state = posix_cs_heal_state(this, realpath, NULL, buf);
3433
3434 if (state == GF_CS_ERROR) {
3435 gf_msg(this->name, GF_LOG_ERROR, 0, 0,
3436 "repair check failed");
3437 }
3438 }
3439
3440 ret = posix_cs_set_state(this, xattr_rsp, state, realpath, NULL);
3441 if (ret) {
3442 gf_msg(this->name, GF_LOG_ERROR, 0, 0,
3443 "posix_cs_set_state failed");
3444 if (ignore_failure)
3445 ret = 0;
3446 else
3447 ret = -1;
3448 goto unlock;
3449 }
3450 }
3451 }
3452
3453 unlock:
3454 if (fd)
3455 UNLOCK(&fd->inode->lock);
3456 else
3457 UNLOCK(&loc->inode->lock);
3458 out:
3459 return ret;
3460 }
3461
3462 int
3463 posix_check_dev_file(xlator_t *this, inode_t *inode, char *fop, int *op_errno)
3464 {
3465 int ret = -1;
3466
3467 if (inode->ia_type == IA_IFBLK || inode->ia_type == IA_IFCHR) {
3468 *op_errno = EINVAL;
3469 gf_msg(this->name, GF_LOG_ERROR, *op_errno, P_MSG_INVALID_ARGUMENT,
3470 "%s received on %s file (%s)", fop,
3471 (inode->ia_type == IA_IFBLK) ? "block" : "char",
3472 uuid_utoa(inode->gfid));
3473 goto out;
3474 }
3475
3476 ret = 0;
3477
3478 out:
3479 return ret;
3480 }
3481
3482 void
3483 posix_update_iatt_buf(struct iatt *buf, int fd, char *loc)
3484 {
3485 int ret = 0;
3486 char val[64] = {
3487 0,
3488 };
3489
3490 if (fd != -1) {
3491 ret = sys_fgetxattr(fd, GF_CS_OBJECT_SIZE, &val, sizeof(val));
3492 if (ret > 0) {
3493 buf->ia_size = atoll(val);
3494 } else {
3495 /* Safe to assume that the other 2 xattrs are also not set*/
3496 return;
3497 }
3498 ret = sys_fgetxattr(fd, GF_CS_BLOCK_SIZE, &val, sizeof(val));
3499 if (ret > 0) {
3500 buf->ia_blksize = atoll(val);
3501 }
3502 ret = sys_fgetxattr(fd, GF_CS_NUM_BLOCKS, &val, sizeof(val));
3503 if (ret > 0) {
3504 buf->ia_blocks = atoll(val);
3505 }
3506 } else {
3507 ret = sys_lgetxattr(loc, GF_CS_OBJECT_SIZE, &val, sizeof(val));
3508 if (ret > 0) {
3509 buf->ia_size = atoll(val);
3510 } else {
3511 /* Safe to assume that the other 2 xattrs are also not set*/
3512 return;
3513 }
3514 ret = sys_lgetxattr(loc, GF_CS_BLOCK_SIZE, &val, sizeof(val));
3515 if (ret > 0) {
3516 buf->ia_blksize = atoll(val);
3517 }
3518 ret = sys_lgetxattr(loc, GF_CS_NUM_BLOCKS, &val, sizeof(val));
3519 if (ret > 0) {
3520 buf->ia_blocks = atoll(val);
3521 }
3522 }
3523 }
3524
3525 gf_boolean_t
3526 posix_is_layout_stale(dict_t *xdata, char *par_path, xlator_t *this)
3527 {
3528 int op_ret = 0;
3529 ssize_t size = 0;
3530 char value_buf[4096] = {
3531 0,
3532 };
3533 gf_boolean_t have_val = _gf_false;
3534 data_t *arg_data = NULL;
3535 char *xattr_name = NULL;
3536 size_t xattr_len = 0;
3537 gf_boolean_t is_stale = _gf_false;
3538
3539 op_ret = dict_get_str_sizen(xdata, GF_PREOP_PARENT_KEY, &xattr_name);
3540 if (xattr_name == NULL) {
3541 op_ret = 0;
3542 return is_stale;
3543 }
3544
3545 xattr_len = strlen(xattr_name);
3546 arg_data = dict_getn(xdata, xattr_name, xattr_len);
3547 if (!arg_data) {
3548 op_ret = 0;
3549 dict_del_sizen(xdata, GF_PREOP_PARENT_KEY);
3550 return is_stale;
3551 }
3552
3553 size = sys_lgetxattr(par_path, xattr_name, value_buf,
3554 sizeof(value_buf) - 1);
3555
3556 if (size >= 0) {
3557 have_val = _gf_true;
3558 } else {
3559 if (errno == ERANGE) {
3560 gf_msg(this->name, GF_LOG_INFO, errno, P_MSG_PREOP_CHECK_FAILED,
3561 "getxattr on key (%s) path (%s) failed due to"
3562 " buffer overflow",
3563 xattr_name, par_path);
3564 size = sys_lgetxattr(par_path, xattr_name, NULL, 0);
3565 }
3566 if (size < 0) {
3567 op_ret = -1;
3568 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_PREOP_CHECK_FAILED,
3569 "getxattr on key (%s) failed, path : %s", xattr_name,
3570 par_path);
3571 goto out;
3572 }
3573 }
3574
3575 if (!have_val) {
3576 size = sys_lgetxattr(par_path, xattr_name, value_buf, size);
3577 if (size < 0) {
3578 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_PREOP_CHECK_FAILED,
3579 "getxattr on key (%s) failed (%s)", xattr_name,
3580 strerror(errno));
3581 goto out;
3582 }
3583 }
3584
3585 if ((arg_data->len != size) || (memcmp(arg_data->data, value_buf, size))) {
3586 gf_msg(this->name, GF_LOG_INFO, EIO, P_MSG_PREOP_CHECK_FAILED,
3587 "failing preop as on-disk xattr value differs from argument "
3588 "value for key %s",
3589 xattr_name);
3590 op_ret = -1;
3591 }
3592
3593 out:
3594 dict_deln(xdata, xattr_name, xattr_len);
3595 dict_del_sizen(xdata, GF_PREOP_PARENT_KEY);
3596
3597 if (op_ret == -1) {
3598 is_stale = _gf_true;
3599 }
3600
3601 return is_stale;
3602 }
3603
3604 /* Delete user xattr from the file at the file-path specified by data and from
3605 * dict */
3606 int
3607 posix_delete_user_xattr(dict_t *dict, char *k, data_t *v, void *data)
3608 {
3609 int ret;
3610 char *real_path = data;
3611
3612 ret = sys_lremovexattr(real_path, k);
3613 if (ret) {
3614 gf_msg("posix-helpers", GF_LOG_ERROR, P_MSG_XATTR_NOT_REMOVED, errno,
3615 "removexattr failed. key %s path %s", k, real_path);
3616 }
3617
3618 dict_del(dict, k);
3619
3620 return ret;
3621 }