Debian Codegraph @

xlators/storage/posix/src/posix-inode-fd-ops.c

raw symbols SCIP
1 /*
2 Copyright (c) 2006-2017 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 /* for SEEK_HOLE and SEEK_DATA */
13 #ifndef _GNU_SOURCE
14 #define _GNU_SOURCE
15 #endif
16
17 #include <openssl/md5.h>
18 #include <stdint.h>
19 #include <sys/time.h>
20 #include <errno.h>
21 #include <libgen.h>
22 #include <pthread.h>
23 #include <ftw.h>
24 #include <sys/stat.h>
25 #include <signal.h>
26 #include <sys/uio.h>
27 #include <unistd.h>
28 #include <regex.h>
29
30 #ifndef GF_BSD_HOST_OS
31 #include <alloca.h>
32 #endif /* GF_BSD_HOST_OS */
33
34 #ifdef HAVE_LINKAT
35 #include <fcntl.h>
36 #endif /* HAVE_LINKAT */
37
38 #include <glusterfs/checksum.h>
39 #include <glusterfs/dict.h>
40 #include <glusterfs/logging.h>
41 #include "posix-handle.h"
42 #include <glusterfs/compat-errno.h>
43 #include <glusterfs/compat.h>
44 #include <glusterfs/syscall.h>
45 #include <glusterfs/locking.h>
46 #include <glusterfs/timer.h>
47 #include "glusterfs4-xdr.h"
48 #include <glusterfs/glusterfs-acl.h>
49 #include "posix.h"
50 #include "posix-messages.h"
51 #include "posix-metadata.h"
52 #include <glusterfs/events.h>
53 #include "posix-gfid-path.h"
54 #include <glusterfs/compat-uuid.h>
55
56 extern char *marker_xattrs[];
57 #define ALIGN_SIZE 4096
58
59 #undef HAVE_SET_FSID
60 #ifdef HAVE_SET_FSID
61
62 #define DECLARE_OLD_FS_ID_VAR \
63 uid_t old_fsuid; \
64 gid_t old_fsgid;
65
66 #define SET_FS_ID(uid, gid) \
67 do { \
68 old_fsuid = setfsuid(uid); \
69 old_fsgid = setfsgid(gid); \
70 } while (0)
71
72 #define SET_TO_OLD_FS_ID() \
73 do { \
74 setfsuid(old_fsuid); \
75 setfsgid(old_fsgid); \
76 } while (0)
77
78 #else
79
80 #define DECLARE_OLD_FS_ID_VAR
81 #define SET_FS_ID(uid, gid)
82 #define SET_TO_OLD_FS_ID()
83
84 #endif
85
86 /* Setting microseconds or nanoseconds depending on what's supported:
87 The passed in `tv` can be
88 struct timespec
89 if supported (better, because it supports nanosecond resolution) or
90 struct timeval
91 otherwise. */
92 #if HAVE_UTIMENSAT
93 #define SET_TIMESPEC_NSEC_OR_TIMEVAL_USEC(tv, nanosecs) tv.tv_nsec = nanosecs
94 #else
95 #define SET_TIMESPEC_NSEC_OR_TIMEVAL_USEC(tv, nanosecs) \
96 tv.tv_usec = nanosecs / 1000
97 #endif
98
99 static char *disallow_removexattrs[] = {GF_XATTR_VOL_ID_KEY, GFID_XATTR_KEY,
100 NULL};
101
102 static void
103 posix_cs_build_xattr_rsp(xlator_t *this, dict_t **rsp, dict_t *req, int fd,
104 char *loc)
105 {
106 int ret = 0;
107 uuid_t uuid;
108
109 if (!dict_get_sizen(req, GF_CS_OBJECT_STATUS))
110 return;
111
112 if (!(*rsp)) {
113 *rsp = dict_new();
114 if (!(*rsp)) {
115 return;
116 }
117 }
118
119 if (fd != -1) {
120 if (dict_get_sizen(req, GF_CS_XATTR_ARCHIVE_UUID)) {
121 ret = sys_fgetxattr(fd, GF_CS_XATTR_ARCHIVE_UUID, uuid, 16);
122 if (ret > 0) {
123 ret = dict_set_gfuuid(*rsp, GF_CS_XATTR_ARCHIVE_UUID, uuid,
124 true);
125 if (ret) {
126 gf_msg(this->name, GF_LOG_WARNING, 0, P_MSG_DICT_SET_FAILED,
127 "%s: Failed to set "
128 "dictionary value for %s for fd %d",
129 uuid_utoa(uuid), GF_CS_XATTR_ARCHIVE_UUID, fd);
130 }
131 } else {
132 gf_msg_debug(this->name, 0, "getxattr failed on %s for fd %d",
133 GF_CS_XATTR_ARCHIVE_UUID, fd);
134 }
135 }
136 } else {
137 if (dict_get_sizen(req, GF_CS_XATTR_ARCHIVE_UUID)) {
138 ret = sys_lgetxattr(loc, GF_CS_XATTR_ARCHIVE_UUID, uuid, 16);
139 if (ret > 0) {
140 ret = dict_set_gfuuid(*rsp, GF_CS_XATTR_ARCHIVE_UUID, uuid,
141 true);
142 if (ret) {
143 gf_msg(this->name, GF_LOG_WARNING, 0, P_MSG_DICT_SET_FAILED,
144 "%s: Failed to set "
145 "dictionary value for %s for loc %s",
146 uuid_utoa(uuid), GF_CS_XATTR_ARCHIVE_UUID, loc);
147 }
148 } else {
149 gf_msg_debug(this->name, 0, "getxattr failed on %s for %s",
150 GF_CS_XATTR_ARCHIVE_UUID, loc);
151 }
152 }
153 }
154 return;
155 }
156
157 int32_t
158 posix_stat(call_frame_t *frame, xlator_t *this, loc_t *loc, dict_t *xdata)
159 {
160 struct iatt buf = {
161 0,
162 };
163 int32_t op_ret = -1;
164 int32_t op_errno = 0;
165 struct posix_private *priv = NULL;
166 char *real_path = NULL;
167 dict_t *xattr_rsp = NULL;
168 gf_boolean_t cs_obj_status, cs_obj_repair;
169
170 DECLARE_OLD_FS_ID_VAR;
171
172 VALIDATE_OR_GOTO(frame, out);
173 VALIDATE_OR_GOTO(this, out);
174 VALIDATE_OR_GOTO(loc, out);
175
176 priv = this->private;
177 VALIDATE_OR_GOTO(priv, out);
178
179 SET_FS_ID(frame->root->uid, frame->root->gid);
180
181 MAKE_INODE_HANDLE(real_path, this, loc, &buf);
182
183 if (op_ret == -1) {
184 op_errno = errno;
185 if (op_errno == ENOENT) {
186 gf_msg_debug(this->name, op_errno,
187 "lstat on gfid-handle %s (path: %s) failed",
188 real_path ? real_path : "<null>", loc->path);
189 } else {
190 gf_msg(this->name, GF_LOG_ERROR, op_errno, P_MSG_LSTAT_FAILED,
191 "lstat on gfid-handle %s (path: %s) failed",
192 real_path ? real_path : "<null>", loc->path);
193 }
194 goto out;
195 }
196 if (xdata) {
197 xattr_rsp = posix_xattr_fill(this, real_path, loc, NULL, -1, xdata,
198 &buf);
199
200 cs_obj_status = dict_get_sizen(xdata, GF_CS_OBJECT_STATUS);
201 cs_obj_repair = dict_get_sizen(xdata, GF_CS_OBJECT_REPAIR);
202
203 if (cs_obj_status || cs_obj_repair)
204 posix_cs_maintenance(this, NULL, loc, NULL, &buf, real_path,
205 cs_obj_status, cs_obj_repair, &xattr_rsp,
206 _gf_true);
207 if (cs_obj_status) {
208 posix_cs_build_xattr_rsp(this, &xattr_rsp, xdata, -1, real_path);
209 posix_update_iatt_buf(&buf, -1, real_path);
210 }
211 }
212
213 op_ret = 0;
214
215 out:
216 SET_TO_OLD_FS_ID();
217 STACK_UNWIND_STRICT(stat, frame, op_ret, op_errno, &buf, xattr_rsp);
218 if (xattr_rsp)
219 dict_unref(xattr_rsp);
220
221 return 0;
222 }
223
224 static int
225 posix_do_chmod(xlator_t *this, const char *path, struct iatt *stbuf)
226 {
227 int32_t ret = -1;
228 mode_t mode = 0;
229 mode_t mode_bit = 0;
230 struct posix_private *priv = NULL;
231 struct stat stat;
232 int is_symlink = 0;
233
234 priv = this->private;
235 VALIDATE_OR_GOTO(priv, out);
236 ret = sys_lstat(path, &stat);
237 if (ret != 0) {
238 gf_msg(this->name, GF_LOG_WARNING, 0, P_MSG_LSTAT_FAILED,
239 "lstat failed: %s", path);
240 goto out;
241 }
242
243 if (S_ISLNK(stat.st_mode))
244 is_symlink = 1;
245
246 if (S_ISDIR(stat.st_mode)) {
247 mode = st_mode_from_ia(stbuf->ia_prot, stbuf->ia_type);
248 mode_bit = (mode & priv->create_directory_mask) |
249 priv->force_directory_mode;
250 mode = posix_override_umask(mode, mode_bit);
251 } else {
252 mode = st_mode_from_ia(stbuf->ia_prot, stbuf->ia_type);
253 mode_bit = (mode & priv->create_mask) | priv->force_create_mode;
254 mode = posix_override_umask(mode, mode_bit);
255 }
256 ret = sys_lchmod(path, mode);
257 /* Before glibc 2.32, lchmod() was not implemented and calling it
258 * always returned ENOSYS. Starting with glibc 2.32 this request
259 * is using fchmodat() system call to implement it. However, linux
260 * doesn't support setting the mode for symlinks, so the system
261 * call returns EOPNOTSUPP (or ENOTSUP based on man page). We need
262 * to handle all cases. */
263 if ((ret < 0) &&
264 ((errno == ENOSYS) || (errno == EOPNOTSUPP) || (errno == ENOTSUP))) {
265 gf_msg_debug(this->name, errno, "Failed for path: %s", path);
266 if (is_symlink) {
267 ret = 0;
268 goto out;
269 }
270
271 ret = sys_chmod(path, mode);
272 }
273 out:
274 return ret;
275 }
276
277 static int
278 posix_do_chown(xlator_t *this, const char *path, struct iatt *stbuf,
279 int32_t valid)
280 {
281 int32_t ret = -1;
282 uid_t uid = -1;
283 gid_t gid = -1;
284
285 if (valid & GF_SET_ATTR_UID)
286 uid = stbuf->ia_uid;
287
288 if (valid & GF_SET_ATTR_GID)
289 gid = stbuf->ia_gid;
290
291 ret = sys_lchown(path, uid, gid);
292
293 return ret;
294 }
295
296 static int
297 posix_do_utimes(xlator_t *this, const char *path, struct iatt *stbuf, int valid)
298 {
299 int32_t ret = -1;
300 #if defined(HAVE_UTIMENSAT)
301 struct timespec tv[2] = {{
302 0,
303 },
304 {
305 0,
306 }};
307 #else
308 struct timeval tv[2] = {{
309 0,
310 },
311 {
312 0,
313 }};
314 #endif
315 struct stat stat;
316 int is_symlink = 0;
317
318 ret = sys_lstat(path, &stat);
319 if (ret != 0) {
320 gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_FILE_OP_FAILED, "%s",
321 path);
322 goto out;
323 }
324
325 if (S_ISLNK(stat.st_mode))
326 is_symlink = 1;
327
328 if ((valid & GF_SET_ATTR_ATIME) == GF_SET_ATTR_ATIME) {
329 tv[0].tv_sec = stbuf->ia_atime;
330 SET_TIMESPEC_NSEC_OR_TIMEVAL_USEC(tv[0], stbuf->ia_atime_nsec);
331 } else {
332 /* atime is not given, use current values */
333 tv[0].tv_sec = ST_ATIM_SEC(&stat);
334 SET_TIMESPEC_NSEC_OR_TIMEVAL_USEC(tv[0], ST_ATIM_NSEC(&stat));
335 }
336
337 if ((valid & GF_SET_ATTR_MTIME) == GF_SET_ATTR_MTIME) {
338 tv[1].tv_sec = stbuf->ia_mtime;
339 SET_TIMESPEC_NSEC_OR_TIMEVAL_USEC(tv[1], stbuf->ia_mtime_nsec);
340 } else {
341 /* mtime is not given, use current values */
342 tv[1].tv_sec = ST_MTIM_SEC(&stat);
343 SET_TIMESPEC_NSEC_OR_TIMEVAL_USEC(tv[1], ST_MTIM_NSEC(&stat));
344 }
345
346 ret = PATH_SET_TIMESPEC_OR_TIMEVAL(path, tv);
347 if ((ret == -1) && (errno == ENOSYS)) {
348 gf_msg_debug(this->name, errno, "Failed for path: %s", path);
349 if (is_symlink) {
350 ret = 0;
351 goto out;
352 }
353
354 ret = PATH_SET_TIMESPEC_OR_TIMEVAL(path, tv);
355 }
356
357 out:
358 return ret;
359 }
360
361 int
362 posix_setattr(call_frame_t *frame, xlator_t *this, loc_t *loc,
363 struct iatt *stbuf, int32_t valid, dict_t *xdata)
364 {
365 int32_t op_ret = -1;
366 int32_t op_errno = 0;
367 char *real_path = 0;
368 struct iatt statpre = {
369 0,
370 };
371 struct iatt statpost = {
372 0,
373 };
374 dict_t *xattr_rsp = NULL;
375 struct posix_private *priv = NULL;
376
377 priv = this->private;
378
379 DECLARE_OLD_FS_ID_VAR;
380
381 VALIDATE_OR_GOTO(frame, out);
382 VALIDATE_OR_GOTO(this, out);
383 VALIDATE_OR_GOTO(loc, out);
384
385 SET_FS_ID(frame->root->uid, frame->root->gid);
386 MAKE_INODE_HANDLE(real_path, this, loc, &statpre);
387
388 if (op_ret == -1) {
389 op_errno = errno;
390 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LSTAT_FAILED,
391 "setattr (lstat) on gfid-handle %s (path: %s) failed",
392 real_path ? real_path : "<null>", loc->path);
393 goto out;
394 }
395
396 if (valid & (GF_SET_ATTR_UID | GF_SET_ATTR_GID)) {
397 op_ret = posix_do_chown(this, real_path, stbuf, valid);
398 if (op_ret == -1) {
399 op_errno = errno;
400 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_CHOWN_FAILED,
401 "setattr (chown) on %s "
402 "failed",
403 real_path);
404 goto out;
405 }
406 }
407
408 if (valid & GF_SET_ATTR_MODE) {
409 op_ret = posix_do_chmod(this, real_path, stbuf);
410 if (op_ret == -1) {
411 op_errno = errno;
412 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_CHMOD_FAILED,
413 "setattr (chmod) on gfid-handle %s (path: %s) "
414 "failed",
415 real_path, loc->path);
416 goto out;
417 }
418 }
419
420 if (valid & (GF_SET_ATTR_ATIME | GF_SET_ATTR_MTIME)) {
421 op_ret = posix_do_utimes(this, real_path, stbuf, valid);
422 if (op_ret == -1) {
423 op_errno = errno;
424 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_UTIMES_FAILED,
425 "setattr (utimes) on gfid-handle %s (path: %s) "
426 "failed",
427 real_path, loc->path);
428 goto out;
429 }
430 posix_update_utime_in_mdata(this, real_path, -1, loc->inode,
431 &frame->root->ctime, stbuf, valid);
432 }
433
434 if ((valid & GF_SET_ATTR_CTIME) && priv->ctime) {
435 posix_update_ctime_in_mdata(this, real_path, -1, loc->inode,
436 &frame->root->ctime, stbuf, valid);
437 }
438
439 if (!valid) {
440 op_ret = sys_lchown(real_path, -1, -1);
441 if (op_ret == -1) {
442 op_errno = errno;
443 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LCHOWN_FAILED,
444 "lchown (gfid-handle: %s, path: %s, -1, -1) "
445 "failed",
446 real_path, loc->path);
447
448 goto out;
449 }
450 }
451
452 op_ret = posix_pstat(this, loc->inode, loc->gfid, real_path, &statpost,
453 _gf_false, _gf_true);
454 if (op_ret == -1) {
455 op_errno = errno;
456 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LSTAT_FAILED,
457 "setattr (lstat) on gfid-handle %s (path: %s) failed", real_path,
458 loc->path);
459 goto out;
460 }
461
462 posix_set_ctime(frame, this, real_path, -1, loc->inode, &statpost);
463
464 if (xdata) {
465 xattr_rsp = posix_xattr_fill(this, real_path, loc, NULL, -1, xdata,
466 &statpost);
467 if (dict_get_sizen(xdata, GF_CS_OBJECT_STATUS)) {
468 posix_update_iatt_buf(&statpre, -1, real_path);
469 posix_update_iatt_buf(&statpost, -1, real_path);
470 }
471 }
472 op_ret = 0;
473
474 out:
475 SET_TO_OLD_FS_ID();
476
477 STACK_UNWIND_STRICT(setattr, frame, op_ret, op_errno, &statpre, &statpost,
478 xattr_rsp);
479 if (xattr_rsp)
480 dict_unref(xattr_rsp);
481
482 return 0;
483 }
484
485 int32_t
486 posix_do_fchown(xlator_t *this, int fd, struct iatt *stbuf, int32_t valid)
487 {
488 int ret = -1;
489 uid_t uid = -1;
490 gid_t gid = -1;
491
492 if (valid & GF_SET_ATTR_UID)
493 uid = stbuf->ia_uid;
494
495 if (valid & GF_SET_ATTR_GID)
496 gid = stbuf->ia_gid;
497
498 ret = sys_fchown(fd, uid, gid);
499
500 return ret;
501 }
502
503 int32_t
504 posix_do_fchmod(xlator_t *this, int fd, struct iatt *stbuf)
505 {
506 int32_t ret = -1;
507 mode_t mode = 0;
508 mode_t mode_bit = 0;
509 struct posix_private *priv = NULL;
510
511 priv = this->private;
512 VALIDATE_OR_GOTO(priv, out);
513 mode = st_mode_from_ia(stbuf->ia_prot, stbuf->ia_type);
514 mode_bit = (mode & priv->create_mask) | priv->force_create_mode;
515 mode = posix_override_umask(mode, mode_bit);
516 ret = sys_fchmod(fd, mode);
517 out:
518 return ret;
519 }
520
521 static inline int
522 gf_futime(int fd, struct timespec ts[2])
523 {
524 #if defined(HAVE_FUTIMENS)
525 return sys_futimens(fd, ts);
526 #elif defined(HAVE_FUTIMES)
527 struct timeval tv[2] = {
528 {.tv_sec = ts[0].tv_sec, .tv_usec = ts[0].tv_nsec / 1000},
529 {.tv_sec = ts[1].tv_sec, .tv_usec = ts[1].tv_nsec / 1000}};
530 return sys_futimes(fd, tv);
531 #else
532 /* This should be catched by configure early. */
533 #error "This system doesn't support neither futimens() nor futimes()"
534 #endif /* HAVE_FUTIMENS */
535 }
536
537 static int
538 posix_do_futimes(xlator_t *this, int fd, struct iatt *stbuf, int valid)
539 {
540 int32_t ret = -1;
541 struct timespec ts[2] = {{
542 0,
543 },
544 {
545 0,
546 }};
547 struct stat stat = {
548 0,
549 };
550 gf_boolean_t fstat_executed = _gf_false;
551
552 if ((valid & GF_SET_ATTR_ATIME) == GF_SET_ATTR_ATIME) {
553 ts[0].tv_sec = stbuf->ia_atime;
554 ts[0].tv_nsec = stbuf->ia_atime_nsec;
555 } else {
556 ret = sys_fstat(fd, &stat);
557 if (ret != 0) {
558 gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_FILE_OP_FAILED,
559 "%d", fd);
560 goto out;
561 }
562 fstat_executed = _gf_true;
563 /* atime is not given, use current values */
564 ts[0].tv_sec = ST_ATIM_SEC(&stat);
565 ts[0].tv_nsec = ST_ATIM_NSEC(&stat);
566 }
567
568 if ((valid & GF_SET_ATTR_MTIME) == GF_SET_ATTR_MTIME) {
569 ts[1].tv_sec = stbuf->ia_mtime;
570 ts[1].tv_nsec = stbuf->ia_mtime_nsec;
571 } else {
572 if (!fstat_executed) {
573 ret = sys_fstat(fd, &stat);
574 if (ret != 0) {
575 gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_FILE_OP_FAILED,
576 "%d", fd);
577 goto out;
578 }
579 }
580 /* mtime is not given, use current values */
581 ts[1].tv_sec = ST_MTIM_SEC(&stat);
582 ts[1].tv_nsec = ST_MTIM_NSEC(&stat);
583 }
584
585 ret = gf_futime(fd, ts);
586 if (ret == -1)
587 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_FUTIMES_FAILED, "%d", fd);
588
589 out:
590 return ret;
591 }
592
593 int
594 posix_fsetattr(call_frame_t *frame, xlator_t *this, fd_t *fd,
595 struct iatt *stbuf, int32_t valid, dict_t *xdata)
596 {
597 int32_t op_ret = -1;
598 int32_t op_errno = 0;
599 struct iatt statpre = {
600 0,
601 };
602 struct iatt statpost = {
603 0,
604 };
605 struct posix_private *priv = NULL;
606 struct posix_fd *pfd = NULL;
607 dict_t *xattr_rsp = NULL;
608 int32_t ret = -1;
609
610 DECLARE_OLD_FS_ID_VAR;
611
612 SET_FS_ID(frame->root->uid, frame->root->gid);
613
614 VALIDATE_OR_GOTO(frame, out);
615 VALIDATE_OR_GOTO(this, out);
616 VALIDATE_OR_GOTO(fd, out);
617
618 priv = this->private;
619 VALIDATE_OR_GOTO(priv, out);
620
621 ret = posix_fd_ctx_get(fd, this, &pfd, &op_errno);
622 if (ret < 0) {
623 gf_msg_debug(this->name, 0, "pfd is NULL from fd=%p", fd);
624 goto out;
625 }
626
627 op_ret = posix_fdstat(this, fd->inode, pfd->fd, &statpre, _gf_true);
628 if (op_ret == -1) {
629 op_errno = errno;
630 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_FSTAT_FAILED,
631 "fsetattr (fstat) failed on fd=%p", fd);
632 goto out;
633 }
634
635 if (valid & (GF_SET_ATTR_UID | GF_SET_ATTR_GID)) {
636 op_ret = posix_do_fchown(this, pfd->fd, stbuf, valid);
637 if (op_ret == -1) {
638 op_errno = errno;
639 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_FCHOWN_FAILED,
640 "fsetattr (fchown) failed"
641 " on fd=%p",
642 fd);
643 goto out;
644 }
645 }
646
647 if (valid & GF_SET_ATTR_MODE) {
648 op_ret = posix_do_fchmod(this, pfd->fd, stbuf);
649 if (op_ret == -1) {
650 op_errno = errno;
651 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_FCHMOD_FAILED,
652 "fsetattr (fchmod) failed"
653 " on fd=%p",
654 fd);
655 goto out;
656 }
657 }
658
659 if (valid & (GF_SET_ATTR_ATIME | GF_SET_ATTR_MTIME)) {
660 op_ret = posix_do_futimes(this, pfd->fd, stbuf, valid);
661 if (op_ret == -1) {
662 op_errno = errno;
663 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_FUTIMES_FAILED,
664 "fsetattr (futimes) on "
665 "failed fd=%p",
666 fd);
667 goto out;
668 }
669 posix_update_utime_in_mdata(this, NULL, pfd->fd, fd->inode,
670 &frame->root->ctime, stbuf, valid);
671 }
672
673 if ((valid & GF_SET_ATTR_CTIME) && priv->ctime) {
674 posix_update_ctime_in_mdata(this, NULL, pfd->fd, fd->inode,
675 &frame->root->ctime, stbuf, valid);
676 }
677
678 if (!valid) {
679 op_ret = sys_fchown(pfd->fd, -1, -1);
680 if (op_ret == -1) {
681 op_errno = errno;
682 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_FCHOWN_FAILED,
683 "fchown (%d, -1, -1) failed", pfd->fd);
684
685 goto out;
686 }
687 }
688
689 op_ret = posix_fdstat(this, fd->inode, pfd->fd, &statpost, _gf_true);
690 if (op_ret == -1) {
691 op_errno = errno;
692 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_FSTAT_FAILED,
693 "fsetattr (fstat) failed on fd=%p", fd);
694 goto out;
695 }
696
697 posix_set_ctime(frame, this, NULL, pfd->fd, fd->inode, &statpost);
698
699 if (xdata)
700 xattr_rsp = posix_xattr_fill(this, NULL, NULL, fd, pfd->fd, xdata,
701 &statpost);
702 op_ret = 0;
703
704 out:
705 SET_TO_OLD_FS_ID();
706
707 STACK_UNWIND_STRICT(fsetattr, frame, op_ret, op_errno, &statpre, &statpost,
708 xattr_rsp);
709 if (xattr_rsp)
710 dict_unref(xattr_rsp);
711
712 return 0;
713 }
714
715 static int32_t
716 posix_do_fallocate(call_frame_t *frame, xlator_t *this, fd_t *fd, int32_t flags,
717 off_t offset, size_t len, struct iatt *statpre,
718 struct iatt *statpost, dict_t *xdata, dict_t **rsp_xdata)
719 {
720 int32_t ret = -1;
721 int32_t op_errno = 0;
722 struct posix_fd *pfd = NULL;
723 gf_boolean_t locked = _gf_false;
724 posix_inode_ctx_t *ctx = NULL;
725 struct posix_private *priv = NULL;
726 gf_boolean_t check_space_error = _gf_false;
727 struct stat statbuf = {
728 0,
729 };
730 gf_boolean_t cs_obj_status, cs_obj_repair;
731
732 DECLARE_OLD_FS_ID_VAR;
733
734 SET_FS_ID(frame->root->uid, frame->root->gid);
735
736 VALIDATE_OR_GOTO(frame, out);
737 VALIDATE_OR_GOTO(this, out);
738 VALIDATE_OR_GOTO(fd, out);
739
740 priv = this->private;
741
742 /* fallocate case is special so call posix_disk_space_check separately
743 for every fallocate fop instead of calling posix_disk_space with
744 thread after every 5 sec sleep to working correctly storage.reserve
745 option behaviour
746 */
747 if (priv->disk_reserve)
748 posix_disk_space_check(priv);
749
750 DISK_SPACE_CHECK_AND_GOTO(frame, priv, xdata, ret, ret, unlock);
751
752 overwrite:
753 check_space_error = _gf_true;
754
755 ret = posix_fd_ctx_get(fd, this, &pfd, &op_errno);
756 if (ret < 0) {
757 gf_msg_debug(this->name, 0, "pfd is NULL from fd=%p", fd);
758 goto out;
759 }
760
761 ret = posix_inode_ctx_get_all(fd->inode, this, &ctx);
762 if (ret < 0) {
763 ret = -ENOMEM;
764 goto out;
765 }
766
767 if (xdata && dict_get(xdata, GLUSTERFS_WRITE_UPDATE_ATOMIC)) {
768 locked = _gf_true;
769 pthread_mutex_lock(&ctx->write_atomic_lock);
770 }
771
772 if (xdata) {
773 cs_obj_status = dict_get_sizen(xdata, GF_CS_OBJECT_STATUS);
774 cs_obj_repair = dict_get_sizen(xdata, GF_CS_OBJECT_REPAIR);
775
776 if (cs_obj_status || cs_obj_repair) {
777 ret = posix_fdstat(this, fd->inode, pfd->fd, statpre, _gf_true);
778 if (ret == -1) {
779 ret = -errno;
780 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_FSTAT_FAILED,
781 "fallocate (fstat) failed on fd=%p", fd);
782 goto unlock;
783 }
784
785 ret = posix_cs_maintenance(this, fd, NULL, &pfd->fd, statpre, NULL,
786 cs_obj_status, cs_obj_repair, rsp_xdata,
787 _gf_false);
788 if (ret < 0) {
789 gf_msg(this->name, GF_LOG_ERROR, 0, 0,
790 "file state check failed, fd %p", fd);
791 ret = -EIO;
792 goto unlock;
793 }
794 }
795 }
796
797 ret = sys_fallocate(pfd->fd, flags, offset, len);
798 if (ret == -1) {
799 ret = -errno;
800 gf_msg(this->name, GF_LOG_ERROR, -ret, P_MSG_FALLOCATE_FAILED,
801 "fallocate failed on %s offset: %jd, "
802 "len:%zu, flags: %d",
803 uuid_utoa(fd->inode->gfid), offset, len, flags);
804 goto unlock;
805 }
806
807 ret = posix_fdstat(this, fd->inode, pfd->fd, statpost, _gf_true);
808 if (ret == -1) {
809 ret = -errno;
810 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_FSTAT_FAILED,
811 "fallocate (fstat) failed on fd=%p", fd);
812 goto unlock;
813 }
814
815 posix_set_ctime(frame, this, NULL, pfd->fd, fd->inode, statpost);
816
817 unlock:
818 if (locked) {
819 pthread_mutex_unlock(&ctx->write_atomic_lock);
820 locked = _gf_false;
821 }
822
823 if (op_errno == ENOSPC && priv->disk_space_full && !check_space_error) {
824 #ifdef FALLOC_FL_KEEP_SIZE
825 if (flags & FALLOC_FL_KEEP_SIZE) {
826 goto overwrite;
827 }
828 #endif
829 ret = posix_fd_ctx_get(fd, this, &pfd, &op_errno);
830 if (ret < 0) {
831 gf_msg(this->name, GF_LOG_WARNING, ret, P_MSG_PFD_NULL,
832 "pfd is NULL from fd=%p", fd);
833 goto out;
834 }
835
836 if (sys_fstat(pfd->fd, &statbuf) < 0) {
837 gf_msg(this->name, GF_LOG_WARNING, op_errno, P_MSG_FILE_OP_FAILED,
838 "%d", pfd->fd);
839 goto out;
840 }
841
842 if (offset + len <= statbuf.st_size) {
843 gf_msg_debug(this->name, 0,
844 "io vector size will not"
845 " change disk size so allow overwrite for"
846 " fd %d",
847 pfd->fd);
848 goto overwrite;
849 }
850 }
851
852 out:
853 SET_TO_OLD_FS_ID();
854 if (ret == ENOSPC)
855 ret = -ENOSPC;
856
857 return ret;
858 }
859
860 static char *
861 _page_aligned_alloc(size_t size, char **aligned_buf, gf_boolean_t calloc)
862 {
863 char *alloc_buf = NULL;
864 char *buf = NULL;
865
866 alloc_buf = GF_MALLOC((size + ALIGN_SIZE), gf_posix_mt_char);
867 if (!alloc_buf)
868 goto out;
869 /* page aligned buffer */
870 buf = GF_ALIGN_BUF(alloc_buf, ALIGN_SIZE);
871 if (calloc)
872 memset(buf, 0, size);
873 *aligned_buf = buf;
874 out:
875 return alloc_buf;
876 }
877
878 static int32_t
879 _posix_do_zerofill(int fd, off_t offset, off_t len, int o_direct)
880 {
881 off_t num_vect = 0;
882 off_t num_loop = 1;
883 off_t idx = 0;
884 int32_t op_ret = -1;
885 int32_t vect_size = VECTOR_SIZE;
886 off_t remain = 0;
887 off_t extra = 0;
888 struct iovec *vector = NULL;
889 char *iov_base = NULL;
890 char *alloc_buf = NULL;
891
892 if (len == 0)
893 return 0;
894 if (len < VECTOR_SIZE)
895 vect_size = len;
896
897 num_vect = len / (vect_size);
898 remain = len % vect_size;
899 if (num_vect > MAX_NO_VECT) {
900 extra = num_vect % MAX_NO_VECT;
901 num_loop = num_vect / MAX_NO_VECT;
902 num_vect = MAX_NO_VECT;
903 }
904
905 vector = GF_CALLOC(num_vect, sizeof(struct iovec), gf_common_mt_iovec);
906 if (!vector)
907 return -1;
908 if (o_direct) {
909 alloc_buf = _page_aligned_alloc(vect_size, &iov_base, _gf_true);
910 if (!alloc_buf) {
911 GF_FREE(vector);
912 return -1;
913 }
914 } else {
915 iov_base = GF_CALLOC(vect_size, sizeof(char), gf_common_mt_char);
916 if (!iov_base) {
917 GF_FREE(vector);
918 return -1;
919 }
920 }
921
922 for (idx = 0; idx < num_vect; idx++) {
923 vector[idx].iov_base = iov_base;
924 vector[idx].iov_len = vect_size;
925 }
926 if (sys_lseek(fd, offset, SEEK_SET) < 0) {
927 op_ret = -1;
928 goto err;
929 }
930
931 for (idx = 0; idx < num_loop; idx++) {
932 op_ret = sys_writev(fd, vector, num_vect);
933 if (op_ret < 0)
934 goto err;
935 if (op_ret != (vect_size * num_vect)) {
936 op_ret = -1;
937 errno = ENOSPC;
938 goto err;
939 }
940 }
941 if (extra) {
942 op_ret = sys_writev(fd, vector, extra);
943 if (op_ret < 0)
944 goto err;
945 if (op_ret != (vect_size * extra)) {
946 op_ret = -1;
947 errno = ENOSPC;
948 goto err;
949 }
950 }
951 if (remain) {
952 vector[0].iov_len = remain;
953 op_ret = sys_writev(fd, vector, 1);
954 if (op_ret < 0)
955 goto err;
956 if (op_ret != remain) {
957 op_ret = -1;
958 errno = ENOSPC;
959 goto err;
960 }
961 }
962 err:
963 if (o_direct)
964 GF_FREE(alloc_buf);
965 else
966 GF_FREE(iov_base);
967 GF_FREE(vector);
968 return op_ret;
969 }
970
971 static int32_t
972 posix_do_zerofill(call_frame_t *frame, xlator_t *this, fd_t *fd, off_t offset,
973 off_t len, struct iatt *statpre, struct iatt *statpost,
974 dict_t *xdata, dict_t **rsp_xdata)
975 {
976 int32_t ret = -1;
977 int32_t op_errno = 0;
978 int32_t flags = 0;
979 struct posix_fd *pfd = NULL;
980 gf_boolean_t locked = _gf_false;
981 posix_inode_ctx_t *ctx = NULL;
982 gf_boolean_t cs_obj_status, cs_obj_repair;
983
984 DECLARE_OLD_FS_ID_VAR;
985
986 SET_FS_ID(frame->root->uid, frame->root->gid);
987
988 VALIDATE_OR_GOTO(frame, out);
989 VALIDATE_OR_GOTO(this, out);
990 VALIDATE_OR_GOTO(fd, out);
991
992 ret = posix_fd_ctx_get(fd, this, &pfd, &op_errno);
993 if (ret < 0) {
994 gf_msg_debug(this->name, 0, "pfd is NULL from fd=%p", fd);
995 goto out;
996 }
997
998 ret = posix_inode_ctx_get_all(fd->inode, this, &ctx);
999 if (ret < 0) {
1000 ret = -ENOMEM;
1001 goto out;
1002 }
1003
1004 if (dict_get(xdata, GLUSTERFS_WRITE_UPDATE_ATOMIC)) {
1005 locked = _gf_true;
1006 pthread_mutex_lock(&ctx->write_atomic_lock);
1007 }
1008
1009 if (xdata) {
1010 cs_obj_status = dict_get_sizen(xdata, GF_CS_OBJECT_STATUS);
1011 cs_obj_repair = dict_get_sizen(xdata, GF_CS_OBJECT_REPAIR);
1012
1013 if (cs_obj_status || cs_obj_repair) {
1014 /* statpre is only needed for CS related functions below */
1015 ret = posix_fdstat(this, fd->inode, pfd->fd, statpre, _gf_true);
1016 if (ret == -1) {
1017 ret = -errno;
1018 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_FSTAT_FAILED,
1019 "pre-operation fstat failed on fd = %p", fd);
1020 goto out;
1021 }
1022
1023 ret = posix_cs_maintenance(this, fd, NULL, &pfd->fd, statpre, NULL,
1024 cs_obj_status, cs_obj_repair, rsp_xdata,
1025 _gf_false);
1026 if (ret < 0) {
1027 gf_msg(this->name, GF_LOG_ERROR, 0, 0,
1028 "file state "
1029 "check failed, fd %p",
1030 fd);
1031 ret = -EIO;
1032 goto out;
1033 }
1034 }
1035 if (cs_obj_status)
1036 posix_update_iatt_buf(statpre, pfd->fd, NULL);
1037 }
1038 /* See if we can use FALLOC_FL_ZERO_RANGE to perform the zero fill.
1039 * If it fails, fall back to _posix_do_zerofill() and an optional fsync.
1040 */
1041 flags = FALLOC_FL_ZERO_RANGE;
1042 ret = sys_fallocate(pfd->fd, flags, offset, len);
1043 if (ret == 0) {
1044 goto fsync;
1045 } else {
1046 ret = -errno;
1047 if ((ret != -ENOSYS) && (ret != -EOPNOTSUPP)) {
1048 goto out;
1049 }
1050 }
1051
1052 ret = _posix_do_zerofill(pfd->fd, offset, len, pfd->flags & O_DIRECT);
1053 if (ret < 0) {
1054 ret = -errno;
1055 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_ZEROFILL_FAILED,
1056 "zerofill failed on fd %d length %" PRId64, pfd->fd, len);
1057 goto out;
1058 }
1059
1060 fsync:
1061 if (pfd->flags & (O_SYNC | O_DSYNC)) {
1062 ret = sys_fsync(pfd->fd);
1063 if (ret) {
1064 gf_msg(this->name, GF_LOG_ERROR, 0, P_MSG_WRITEV_FAILED,
1065 "fsync() in writev on fd"
1066 "%d failed",
1067 pfd->fd);
1068 ret = -errno;
1069 goto out;
1070 }
1071 }
1072
1073 ret = posix_fdstat(this, fd->inode, pfd->fd, statpost, _gf_true);
1074 if (ret == -1) {
1075 ret = -errno;
1076 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_FSTAT_FAILED,
1077 "post operation fstat failed on fd=%p", fd);
1078 goto out;
1079 }
1080
1081 posix_set_ctime(frame, this, NULL, pfd->fd, fd->inode, statpost);
1082
1083 out:
1084 if (locked) {
1085 pthread_mutex_unlock(&ctx->write_atomic_lock);
1086 locked = _gf_false;
1087 }
1088 SET_TO_OLD_FS_ID();
1089
1090 return ret;
1091 }
1092
1093 int32_t
1094 posix_glfallocate(call_frame_t *frame, xlator_t *this, fd_t *fd,
1095 int32_t keep_size, off_t offset, size_t len, dict_t *xdata)
1096 {
1097 int32_t ret;
1098 int32_t flags = 0;
1099 struct iatt statpre = {
1100 0,
1101 };
1102 struct iatt statpost = {
1103 0,
1104 };
1105 dict_t *rsp_xdata = NULL;
1106
1107 #ifdef FALLOC_FL_KEEP_SIZE
1108 if (keep_size)
1109 flags = FALLOC_FL_KEEP_SIZE;
1110 #endif /* FALLOC_FL_KEEP_SIZE */
1111
1112 ret = posix_do_fallocate(frame, this, fd, flags, offset, len, &statpre,
1113 &statpost, xdata, &rsp_xdata);
1114 if (ret < 0)
1115 goto err;
1116
1117 STACK_UNWIND_STRICT(fallocate, frame, 0, 0, &statpre, &statpost, rsp_xdata);
1118 return 0;
1119
1120 err:
1121 STACK_UNWIND_STRICT(fallocate, frame, -1, -ret, NULL, NULL, rsp_xdata);
1122 return 0;
1123 }
1124
1125 int32_t
1126 posix_discard(call_frame_t *frame, xlator_t *this, fd_t *fd, off_t offset,
1127 size_t len, dict_t *xdata)
1128 {
1129 int32_t ret;
1130 dict_t *rsp_xdata = NULL;
1131 #ifndef FALLOC_FL_KEEP_SIZE
1132 ret = EOPNOTSUPP;
1133
1134 #else /* FALLOC_FL_KEEP_SIZE */
1135 int32_t flags = FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE;
1136 struct iatt statpre = {
1137 0,
1138 };
1139 struct iatt statpost = {
1140 0,
1141 };
1142
1143 ret = posix_do_fallocate(frame, this, fd, flags, offset, len, &statpre,
1144 &statpost, xdata, &rsp_xdata);
1145 if (ret < 0)
1146 goto err;
1147
1148 STACK_UNWIND_STRICT(discard, frame, 0, 0, &statpre, &statpost, rsp_xdata);
1149 return 0;
1150
1151 err:
1152 #endif /* FALLOC_FL_KEEP_SIZE */
1153 STACK_UNWIND_STRICT(discard, frame, -1, -ret, NULL, NULL, rsp_xdata);
1154 return 0;
1155 }
1156
1157 int32_t
1158 posix_zerofill(call_frame_t *frame, xlator_t *this, fd_t *fd, off_t offset,
1159 off_t len, dict_t *xdata)
1160 {
1161 int32_t ret = 0;
1162 struct iatt statpre = {
1163 0,
1164 };
1165 struct iatt statpost = {
1166 0,
1167 };
1168 struct posix_private *priv = NULL;
1169 int op_ret = -1;
1170 int op_errno = EINVAL;
1171 dict_t *rsp_xdata = NULL;
1172 gf_boolean_t check_space_error = _gf_false;
1173 struct posix_fd *pfd = NULL;
1174 struct stat statbuf = {
1175 0,
1176 };
1177
1178 VALIDATE_OR_GOTO(frame, unwind);
1179 VALIDATE_OR_GOTO(this, unwind);
1180
1181 priv = this->private;
1182 DISK_SPACE_CHECK_AND_GOTO(frame, priv, xdata, op_ret, op_errno, out);
1183
1184 overwrite:
1185 check_space_error = _gf_true;
1186 ret = posix_do_zerofill(frame, this, fd, offset, len, &statpre, &statpost,
1187 xdata, &rsp_xdata);
1188 if (ret < 0) {
1189 op_ret = -1;
1190 op_errno = -ret;
1191 goto unwind;
1192 }
1193
1194 STACK_UNWIND_STRICT(zerofill, frame, 0, 0, &statpre, &statpost, rsp_xdata);
1195 return 0;
1196
1197 out:
1198 if (op_errno == ENOSPC && priv->disk_space_full && !check_space_error) {
1199 ret = posix_fd_ctx_get(fd, this, &pfd, &op_errno);
1200 if (ret < 0) {
1201 gf_msg(this->name, GF_LOG_WARNING, ret, P_MSG_PFD_NULL,
1202 "pfd is NULL from fd=%p", fd);
1203 goto out;
1204 }
1205
1206 if (sys_fstat(pfd->fd, &statbuf) < 0) {
1207 gf_msg(this->name, GF_LOG_WARNING, op_errno, P_MSG_FILE_OP_FAILED,
1208 "%d", pfd->fd);
1209 goto out;
1210 }
1211
1212 if (offset + len <= statbuf.st_size) {
1213 gf_msg_debug(this->name, 0,
1214 "io vector size will not"
1215 " change disk size so allow overwrite for"
1216 " fd %d",
1217 pfd->fd);
1218 goto overwrite;
1219 }
1220 }
1221
1222 unwind:
1223 STACK_UNWIND_STRICT(zerofill, frame, op_ret, op_errno, NULL, NULL,
1224 rsp_xdata);
1225 return 0;
1226 }
1227
1228 int32_t
1229 posix_ipc(call_frame_t *frame, xlator_t *this, int32_t op, dict_t *xdata)
1230 {
1231 /*
1232 * IPC is for inter-translator communication. If one gets here, it
1233 * means somebody sent one that nobody else recognized, which is an
1234 * error much like an uncaught exception.
1235 */
1236 gf_msg(this->name, GF_LOG_ERROR, 0, P_MSG_IPC_NOT_HANDLE,
1237 "GF_LOG_IPC(%d) not handled", op);
1238 STACK_UNWIND_STRICT(ipc, frame, -1, EOPNOTSUPP, NULL);
1239 return 0;
1240 }
1241
1242 int32_t
1243 posix_seek(call_frame_t *frame, xlator_t *this, fd_t *fd, off_t offset,
1244 gf_seek_what_t what, dict_t *xdata)
1245 {
1246 #ifdef HAVE_SEEK_HOLE
1247 struct posix_fd *pfd = NULL;
1248 off_t ret = -1;
1249 int err = 0;
1250 int whence = 0;
1251 struct iatt preop = {
1252 0,
1253 };
1254 dict_t *rsp_xdata = NULL;
1255 gf_boolean_t cs_obj_status, cs_obj_repair;
1256
1257 DECLARE_OLD_FS_ID_VAR;
1258
1259 SET_FS_ID(frame->root->uid, frame->root->gid);
1260
1261 VALIDATE_OR_GOTO(frame, out);
1262 VALIDATE_OR_GOTO(this, out);
1263 VALIDATE_OR_GOTO(fd, out);
1264
1265 switch (what) {
1266 case GF_SEEK_DATA:
1267 whence = SEEK_DATA;
1268 break;
1269 case GF_SEEK_HOLE:
1270 whence = SEEK_HOLE;
1271 break;
1272 default:
1273 err = ENOTSUP;
1274 gf_msg(this->name, GF_LOG_ERROR, ENOTSUP, P_MSG_SEEK_UNKOWN,
1275 "don't know what to seek");
1276 goto out;
1277 }
1278
1279 ret = posix_fd_ctx_get(fd, this, &pfd, &err);
1280 if (ret < 0) {
1281 gf_msg_debug(this->name, 0, "pfd is NULL from fd=%p", fd);
1282 goto out;
1283 }
1284
1285 if (xdata) {
1286 cs_obj_status = dict_get_sizen(xdata, GF_CS_OBJECT_STATUS);
1287 cs_obj_repair = dict_get_sizen(xdata, GF_CS_OBJECT_REPAIR);
1288
1289 if (cs_obj_status || cs_obj_repair) {
1290 ret = posix_fdstat(this, fd->inode, pfd->fd, &preop, _gf_false);
1291 if (ret == -1) {
1292 ret = -errno;
1293 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_FSTAT_FAILED,
1294 "pre-operation fstat failed on fd=%p", fd);
1295 goto out;
1296 }
1297
1298 ret = posix_cs_maintenance(this, fd, NULL, &pfd->fd, &preop, NULL,
1299 cs_obj_status, cs_obj_repair, &rsp_xdata,
1300 _gf_false);
1301 if (ret < 0) {
1302 gf_msg(this->name, GF_LOG_ERROR, 0, 0,
1303 "file state check failed, fd %p", fd);
1304 ret = -EIO;
1305 goto out;
1306 }
1307 }
1308 }
1309
1310 ret = sys_lseek(pfd->fd, offset, whence);
1311 if (ret == -1) {
1312 err = errno;
1313 gf_msg(this->name, fop_log_level(GF_FOP_SEEK, err), err,
1314 P_MSG_SEEK_FAILED, "seek failed on fd %d length %" PRId64,
1315 pfd->fd, offset);
1316 goto out;
1317 }
1318
1319 out:
1320 SET_TO_OLD_FS_ID();
1321
1322 STACK_UNWIND_STRICT(seek, frame, (ret == -1 ? -1 : 0), err,
1323 (ret == -1 ? -1 : ret), rsp_xdata);
1324 #else
1325 STACK_UNWIND_STRICT(seek, frame, -1, EINVAL, 0, NULL);
1326 #endif
1327 return 0;
1328 }
1329
1330 int32_t
1331 posix_opendir(call_frame_t *frame, xlator_t *this, loc_t *loc, fd_t *fd,
1332 dict_t *xdata)
1333 {
1334 char *real_path = NULL;
1335 int32_t op_ret = -1;
1336 int32_t op_errno = EINVAL;
1337 DIR *dir = NULL;
1338 struct posix_fd *pfd = NULL;
1339
1340 DECLARE_OLD_FS_ID_VAR;
1341
1342 VALIDATE_OR_GOTO(frame, out);
1343 VALIDATE_OR_GOTO(this, out);
1344 VALIDATE_OR_GOTO(loc, out);
1345 VALIDATE_OR_GOTO(fd, out);
1346
1347 SET_FS_ID(frame->root->uid, frame->root->gid);
1348 MAKE_INODE_HANDLE(real_path, this, loc, NULL);
1349 if (!real_path) {
1350 op_errno = ESTALE;
1351 goto out;
1352 }
1353
1354 op_ret = -1;
1355 dir = sys_opendir(real_path);
1356
1357 if (dir == NULL) {
1358 op_errno = errno;
1359 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_OPENDIR_FAILED,
1360 "opendir failed on gfid-handle: %s (path: %s)", real_path,
1361 loc->path);
1362 goto out;
1363 }
1364
1365 op_ret = dirfd(dir);
1366 if (op_ret < 0) {
1367 op_errno = errno;
1368 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_DIRFD_FAILED,
1369 "dirfd() failed (path: %s, gfid-handle: %s", loc->path,
1370 real_path);
1371 goto out;
1372 }
1373
1374 pfd = GF_CALLOC(1, sizeof(*pfd), gf_posix_mt_posix_fd);
1375 if (!pfd) {
1376 op_errno = errno;
1377 goto out;
1378 }
1379
1380 pfd->dir = dir;
1381 pfd->dir_eof = -1;
1382 pfd->fd = op_ret;
1383
1384 op_ret = fd_ctx_set(fd, this, (uint64_t)(long)pfd);
1385 if (op_ret)
1386 gf_msg(this->name, GF_LOG_WARNING, 0, P_MSG_FD_PATH_SETTING_FAILED,
1387 "failed to set the fd"
1388 "context path=%s "
1389 "gfid-handle= %s,fd=%p",
1390 loc->path, real_path, fd);
1391
1392 posix_set_ctime(frame, this, NULL, pfd->fd, fd->inode, NULL);
1393
1394 op_ret = 0;
1395
1396 out:
1397 if (op_ret == -1) {
1398 if (dir) {
1399 (void)sys_closedir(dir);
1400 dir = NULL;
1401 }
1402 if (pfd) {
1403 GF_FREE(pfd);
1404 pfd = NULL;
1405 }
1406 }
1407
1408 SET_TO_OLD_FS_ID();
1409 STACK_UNWIND_STRICT(opendir, frame, op_ret, op_errno, fd, NULL);
1410 return 0;
1411 }
1412
1413 static void
1414 posix_add_fd_to_cleanup(xlator_t *this, struct posix_fd *pfd)
1415 {
1416 glusterfs_ctx_t *ctx = this->ctx;
1417 struct posix_private *priv = this->private;
1418
1419 pfd->xl = this;
1420 pthread_mutex_lock(&ctx->fd_lock);
1421 {
1422 list_add_tail(&pfd->list, &ctx->janitor_fds);
1423 priv->rel_fdcount++;
1424 pthread_cond_signal(&ctx->fd_cond);
1425 }
1426 pthread_mutex_unlock(&ctx->fd_lock);
1427 }
1428
1429 int32_t
1430 posix_releasedir(xlator_t *this, fd_t *fd)
1431 {
1432 struct posix_fd *pfd = NULL;
1433
1434 VALIDATE_OR_GOTO(this, out);
1435 VALIDATE_OR_GOTO(fd, out);
1436
1437 pfd = fd_ctx_del_ptr(fd, this);
1438 if (pfd == NULL) {
1439 gf_msg_debug(this->name, 0, "pfd from fd=%p is NULL", fd);
1440 goto out;
1441 }
1442
1443 if (!pfd->dir) {
1444 gf_msg(this->name, GF_LOG_WARNING, 0, P_MSG_PFD_NULL,
1445 "pfd->dir is NULL for fd=%p", fd);
1446 goto out;
1447 }
1448 posix_add_fd_to_cleanup(this, pfd);
1449
1450 out:
1451 return 0;
1452 }
1453
1454 int32_t
1455 posix_readlink(call_frame_t *frame, xlator_t *this, loc_t *loc, size_t size,
1456 dict_t *xdata)
1457 {
1458 char *dest = NULL;
1459 int32_t op_ret = -1;
1460 int32_t op_errno = EINVAL;
1461 char *real_path = NULL;
1462 struct iatt stbuf = {
1463 0,
1464 };
1465
1466 DECLARE_OLD_FS_ID_VAR;
1467
1468 VALIDATE_OR_GOTO(frame, out);
1469 VALIDATE_OR_GOTO(loc, out);
1470
1471 SET_FS_ID(frame->root->uid, frame->root->gid);
1472
1473 dest = alloca(size + 1);
1474
1475 MAKE_INODE_HANDLE(real_path, this, loc, &stbuf);
1476 if (op_ret == -1) {
1477 op_errno = errno;
1478 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LSTAT_FAILED,
1479 "lstat on %s failed", loc->path ? loc->path : "<null>");
1480 goto out;
1481 }
1482
1483 op_ret = sys_readlink(real_path, dest, size);
1484 if (op_ret == -1) {
1485 op_errno = errno;
1486 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_READYLINK_FAILED,
1487 "readlink on gfid-handle: %s (path: %s) failed", real_path,
1488 loc->path);
1489 goto out;
1490 }
1491
1492 dest[op_ret] = 0;
1493 out:
1494 SET_TO_OLD_FS_ID();
1495
1496 STACK_UNWIND_STRICT(readlink, frame, op_ret, op_errno, dest, &stbuf, NULL);
1497
1498 return 0;
1499 }
1500
1501 int32_t
1502 posix_truncate(call_frame_t *frame, xlator_t *this, loc_t *loc, off_t offset,
1503 dict_t *xdata)
1504 {
1505 int32_t op_ret = -1;
1506 int32_t op_errno = 0;
1507 char *real_path = 0;
1508 struct posix_private *priv = NULL;
1509 struct iatt prebuf = {
1510 0,
1511 };
1512 struct iatt postbuf = {
1513 0,
1514 };
1515 dict_t *rsp_xdata = NULL;
1516 gf_boolean_t cs_obj_status, cs_obj_repair;
1517
1518 DECLARE_OLD_FS_ID_VAR;
1519
1520 VALIDATE_OR_GOTO(frame, out);
1521 VALIDATE_OR_GOTO(this, out);
1522 VALIDATE_OR_GOTO(loc, out);
1523
1524 priv = this->private;
1525 VALIDATE_OR_GOTO(priv, out);
1526
1527 SET_FS_ID(frame->root->uid, frame->root->gid);
1528
1529 MAKE_INODE_HANDLE(real_path, this, loc, &prebuf);
1530 if (op_ret == -1) {
1531 op_errno = errno;
1532 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LSTAT_FAILED,
1533 "pre-operation lstat on (path: %s gfid-handle: %s) "
1534 "failed",
1535 loc->path, real_path ? real_path : "<null>");
1536 goto out;
1537 }
1538
1539 if (xdata) {
1540 cs_obj_status = dict_get_sizen(xdata, GF_CS_OBJECT_STATUS);
1541 cs_obj_repair = dict_get_sizen(xdata, GF_CS_OBJECT_REPAIR);
1542
1543 if (cs_obj_status || cs_obj_repair) {
1544 op_ret = posix_cs_maintenance(this, NULL, loc, NULL, &prebuf,
1545 real_path, cs_obj_status,
1546 cs_obj_repair, &rsp_xdata, _gf_false);
1547 if (op_ret == -1) {
1548 gf_msg(this->name, GF_LOG_ERROR, 0, 0,
1549 "file state check failed, path %s", loc->path);
1550 op_errno = EIO;
1551 goto out;
1552 }
1553 }
1554 if (cs_obj_status)
1555 posix_update_iatt_buf(&prebuf, -1, real_path);
1556 }
1557
1558 op_ret = sys_truncate(real_path, offset);
1559 if (op_ret == -1) {
1560 op_errno = errno;
1561 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_TRUNCATE_FAILED,
1562 "truncate on gfid-handle: %s (path: %s) failed", real_path,
1563 loc->path);
1564 goto out;
1565 }
1566
1567 op_ret = posix_pstat(this, loc->inode, loc->gfid, real_path, &postbuf,
1568 _gf_false, _gf_true);
1569 if (op_ret == -1) {
1570 op_errno = errno;
1571 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LSTAT_FAILED,
1572 "lstat on gfid-handle %s (path: %s) failed", real_path,
1573 loc->path);
1574 goto out;
1575 }
1576
1577 posix_set_ctime(frame, this, real_path, -1, loc->inode, &postbuf);
1578
1579 op_ret = 0;
1580 out:
1581 SET_TO_OLD_FS_ID();
1582
1583 STACK_UNWIND_STRICT(truncate, frame, op_ret, op_errno, &prebuf, &postbuf,
1584 NULL);
1585
1586 return 0;
1587 }
1588
1589 int32_t
1590 posix_open(call_frame_t *frame, xlator_t *this, loc_t *loc, int32_t flags,
1591 fd_t *fd, dict_t *xdata)
1592 {
1593 int32_t op_ret = -1;
1594 int32_t op_errno = 0;
1595 char *real_path = NULL;
1596 int32_t _fd = -1;
1597 struct posix_fd *pfd = NULL;
1598 struct posix_private *priv = NULL;
1599 struct iatt preop = {
1600 0,
1601 };
1602 dict_t *rsp_xdata = NULL;
1603 struct iatt stbuf = {
1604 0,
1605 };
1606 gf_boolean_t cs_obj_status, cs_obj_repair;
1607
1608 DECLARE_OLD_FS_ID_VAR;
1609
1610 VALIDATE_OR_GOTO(frame, out);
1611 VALIDATE_OR_GOTO(this, out);
1612 VALIDATE_OR_GOTO(this->private, out);
1613 VALIDATE_OR_GOTO(loc, out);
1614 VALIDATE_OR_GOTO(fd, out);
1615
1616 priv = this->private;
1617 VALIDATE_OR_GOTO(priv, out);
1618
1619 if (loc->inode && ((loc->inode->ia_type == IA_IFBLK) ||
1620 (loc->inode->ia_type == IA_IFCHR))) {
1621 gf_msg(this->name, GF_LOG_ERROR, EINVAL, P_MSG_INVALID_ARGUMENT,
1622 "open received on a block/char file (%s)",
1623 uuid_utoa(loc->inode->gfid));
1624 op_errno = EINVAL;
1625 goto out;
1626 }
1627
1628 if (flags & O_CREAT)
1629 DISK_SPACE_CHECK_AND_GOTO(frame, priv, xdata, op_ret, op_errno, out);
1630
1631 MAKE_INODE_HANDLE(real_path, this, loc, &stbuf);
1632 if (!real_path) {
1633 op_ret = -1;
1634 op_errno = ESTALE;
1635 goto out;
1636 }
1637
1638 if (IA_ISLNK(stbuf.ia_type)) {
1639 op_ret = -1;
1640 op_errno = ELOOP;
1641 goto out;
1642 }
1643
1644 op_ret = -1;
1645 SET_FS_ID(frame->root->uid, frame->root->gid);
1646
1647 if (priv->o_direct)
1648 flags |= O_DIRECT;
1649
1650 _fd = sys_open(real_path, flags, priv->force_create_mode);
1651 if (_fd == -1) {
1652 op_ret = -1;
1653 op_errno = errno;
1654 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_FILE_OP_FAILED,
1655 "open on gfid-handle %s (path: %s), flags: %d", real_path,
1656 loc->path, flags);
1657 goto out;
1658 }
1659
1660 posix_set_ctime(frame, this, real_path, -1, loc->inode, &stbuf);
1661
1662 pfd = GF_CALLOC(1, sizeof(*pfd), gf_posix_mt_posix_fd);
1663 if (!pfd) {
1664 op_errno = errno;
1665 goto out;
1666 }
1667
1668 pfd->flags = flags;
1669 pfd->fd = _fd;
1670
1671 if (xdata) {
1672 cs_obj_status = dict_get_sizen(xdata, GF_CS_OBJECT_STATUS);
1673 cs_obj_repair = dict_get_sizen(xdata, GF_CS_OBJECT_REPAIR);
1674
1675 if (cs_obj_status || cs_obj_repair) {
1676 op_ret = posix_fdstat(this, fd->inode, pfd->fd, &preop, _gf_false);
1677 if (op_ret == -1) {
1678 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_FSTAT_FAILED,
1679 "pre-operation fstat failed on fd=%p", fd);
1680 GF_FREE(pfd);
1681 goto out;
1682 }
1683
1684 posix_cs_maintenance(this, fd, NULL, &pfd->fd, &preop, NULL,
1685 cs_obj_status, cs_obj_repair, &rsp_xdata,
1686 _gf_true);
1687 }
1688 }
1689
1690 op_ret = fd_ctx_set(fd, this, (uint64_t)(long)pfd);
1691 if (op_ret)
1692 gf_msg(this->name, GF_LOG_WARNING, 0, P_MSG_FD_PATH_SETTING_FAILED,
1693 "failed to set the fd context gfid-handle=%s path=%s fd=%p",
1694 real_path, loc->path, fd);
1695
1696 op_ret = 0;
1697
1698 out:
1699 if (op_ret == -1) {
1700 if (_fd != -1) {
1701 sys_close(_fd);
1702 }
1703 }
1704
1705 SET_TO_OLD_FS_ID();
1706
1707 STACK_UNWIND_STRICT(open, frame, op_ret, op_errno, fd, rsp_xdata);
1708
1709 return 0;
1710 }
1711
1712 int
1713 posix_readv(call_frame_t *frame, xlator_t *this, fd_t *fd, size_t size,
1714 off_t offset, uint32_t flags, dict_t *xdata)
1715 {
1716 int32_t op_ret = -1;
1717 int32_t op_errno = 0;
1718 int _fd = -1;
1719 struct posix_private *priv = NULL;
1720 struct iobuf *iobuf = NULL;
1721 struct iobref *iobref = NULL;
1722 struct iovec vec = {
1723 0,
1724 };
1725 struct posix_fd *pfd = NULL;
1726 struct iatt stbuf = {
1727 0,
1728 };
1729 struct iatt preop = {
1730 0,
1731 };
1732 int ret = -1;
1733 dict_t *rsp_xdata = NULL;
1734 gf_boolean_t cs_obj_status, cs_obj_repair;
1735
1736 VALIDATE_OR_GOTO(frame, out);
1737 VALIDATE_OR_GOTO(this, out);
1738 VALIDATE_OR_GOTO(fd, out);
1739 VALIDATE_OR_GOTO(fd->inode, out);
1740 VALIDATE_OR_GOTO(this->private, out);
1741
1742 priv = this->private;
1743 VALIDATE_OR_GOTO(priv, out);
1744
1745 if ((fd->inode->ia_type == IA_IFBLK) || (fd->inode->ia_type == IA_IFCHR)) {
1746 gf_msg(this->name, GF_LOG_ERROR, EINVAL, P_MSG_INVALID_ARGUMENT,
1747 "readv received on a block/char file (%s)",
1748 uuid_utoa(fd->inode->gfid));
1749 op_errno = EINVAL;
1750 goto out;
1751 }
1752
1753 ret = posix_fd_ctx_get(fd, this, &pfd, &op_errno);
1754 if (ret < 0) {
1755 gf_msg(this->name, GF_LOG_WARNING, op_errno, P_MSG_PFD_NULL,
1756 "pfd is NULL from fd=%p", fd);
1757 goto out;
1758 }
1759
1760 if (!size) {
1761 op_errno = EINVAL;
1762 gf_msg(this->name, GF_LOG_WARNING, EINVAL, P_MSG_INVALID_ARGUMENT,
1763 "size=%" GF_PRI_SIZET, size);
1764 goto out;
1765 }
1766
1767 iobuf = iobuf_get_page_aligned(this->ctx->iobuf_pool, size, ALIGN_SIZE);
1768 if (!iobuf) {
1769 op_errno = ENOMEM;
1770 goto out;
1771 }
1772
1773 _fd = pfd->fd;
1774
1775 if (xdata) {
1776 cs_obj_status = dict_get_sizen(xdata, GF_CS_OBJECT_STATUS);
1777 cs_obj_repair = dict_get_sizen(xdata, GF_CS_OBJECT_REPAIR);
1778 if (cs_obj_status || cs_obj_repair) {
1779 op_ret = posix_fdstat(this, fd->inode, _fd, &preop, _gf_false);
1780 if (op_ret == -1) {
1781 op_errno = errno;
1782 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_FSTAT_FAILED,
1783 "pre-operation fstat failed on fd=%p", fd);
1784 goto out;
1785 }
1786 op_ret = posix_cs_maintenance(this, fd, NULL, &_fd, &preop, NULL,
1787 cs_obj_status, cs_obj_repair,
1788 &rsp_xdata, _gf_false);
1789 if (op_ret < 0) {
1790 gf_msg(this->name, GF_LOG_ERROR, 0, 0,
1791 "file state check failed, fd %p", fd);
1792 op_errno = EIO;
1793 goto out;
1794 }
1795 }
1796 if (cs_obj_status)
1797 posix_update_iatt_buf(&preop, _fd, NULL);
1798 }
1799
1800 op_ret = sys_pread(_fd, iobuf->ptr, size, offset);
1801 if (op_ret == -1) {
1802 op_errno = errno;
1803 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_READ_FAILED,
1804 "read failed on gfid=%s, "
1805 "fd=%p, offset=%" PRIu64 " size=%" GF_PRI_SIZET
1806 ", "
1807 "buf=%p",
1808 uuid_utoa(fd->inode->gfid), fd, offset, size, iobuf->ptr);
1809 goto out;
1810 }
1811
1812 GF_ATOMIC_ADD(priv->read_value, op_ret);
1813
1814 vec.iov_base = iobuf->ptr;
1815 vec.iov_len = op_ret;
1816
1817 iobref = iobref_new();
1818
1819 iobref_add(iobref, iobuf);
1820
1821 /*
1822 * readv successful, and we need to get the stat of the file
1823 * we read from
1824 */
1825
1826 op_ret = posix_fdstat(this, fd->inode, _fd, &stbuf, _gf_true);
1827 if (op_ret == -1) {
1828 op_errno = errno;
1829 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_FSTAT_FAILED,
1830 "fstat failed on fd=%p", fd);
1831 goto out;
1832 }
1833
1834 posix_set_ctime(frame, this, NULL, pfd->fd, fd->inode, &stbuf);
1835
1836 /* Hack to notify higher layers of EOF. */
1837 if (!stbuf.ia_size || (offset + vec.iov_len) >= stbuf.ia_size)
1838 op_errno = ENOENT;
1839
1840 op_ret = vec.iov_len;
1841
1842 out:
1843
1844 STACK_UNWIND_STRICT(readv, frame, op_ret, op_errno, &vec, 1, &stbuf, iobref,
1845 rsp_xdata);
1846
1847 if (iobref)
1848 iobref_unref(iobref);
1849 if (iobuf)
1850 iobuf_unref(iobuf);
1851
1852 return 0;
1853 }
1854
1855 int32_t
1856 __posix_pwritev(int fd, struct iovec *vector, int count, off_t offset)
1857 {
1858 int32_t op_ret = 0;
1859 int idx = 0;
1860 int retval = 0;
1861 off_t internal_off = 0;
1862
1863 if (!vector)
1864 return -EFAULT;
1865
1866 internal_off = offset;
1867 for (idx = 0; idx < count; idx++) {
1868 retval = sys_pwrite(fd, vector[idx].iov_base, vector[idx].iov_len,
1869 internal_off);
1870 if (retval == -1) {
1871 op_ret = -errno;
1872 goto err;
1873 }
1874 op_ret += retval;
1875 internal_off += retval;
1876 }
1877
1878 err:
1879 return op_ret;
1880 }
1881
1882 static int32_t
1883 __posix_writev(int fd, struct iovec *vector, int count, off_t startoff,
1884 int odirect)
1885 {
1886 int32_t op_ret = 0;
1887 int idx = 0;
1888 int max_buf_size = 0;
1889 int retval = 0;
1890 char *buf = NULL;
1891 char *alloc_buf = NULL;
1892 off_t internal_off = 0;
1893
1894 /* Check for the O_DIRECT flag during open() */
1895 if (!odirect)
1896 return __posix_pwritev(fd, vector, count, startoff);
1897
1898 for (idx = 0; idx < count; idx++) {
1899 if (max_buf_size < vector[idx].iov_len)
1900 max_buf_size = vector[idx].iov_len;
1901 }
1902
1903 alloc_buf = _page_aligned_alloc(max_buf_size, &buf, _gf_false);
1904 if (!alloc_buf) {
1905 op_ret = -errno;
1906 goto err;
1907 }
1908
1909 internal_off = startoff;
1910 for (idx = 0; idx < count; idx++) {
1911 memcpy(buf, vector[idx].iov_base, vector[idx].iov_len);
1912
1913 /* not sure whether writev works on O_DIRECT'd fd */
1914 retval = sys_pwrite(fd, buf, vector[idx].iov_len, internal_off);
1915 if (retval == -1) {
1916 op_ret = -errno;
1917 goto err;
1918 }
1919
1920 op_ret += retval;
1921 internal_off += retval;
1922 }
1923
1924 err:
1925 GF_FREE(alloc_buf);
1926
1927 return op_ret;
1928 }
1929
1930 dict_t *
1931 _fill_writev_xdata(fd_t *fd, dict_t *xdata, xlator_t *this, int is_append)
1932 {
1933 dict_t *rsp_xdata = NULL;
1934 int32_t ret = 0;
1935 inode_t *inode = NULL;
1936
1937 if (fd)
1938 inode = fd->inode;
1939
1940 if (!fd || !fd->inode || gf_uuid_is_null(fd->inode->gfid)) {
1941 gf_msg_callingfn(this->name, GF_LOG_ERROR, EINVAL, P_MSG_XATTR_FAILED,
1942 "fd: %p inode: %p"
1943 "gfid:%s",
1944 fd, inode ? inode : 0,
1945 inode ? uuid_utoa(inode->gfid) : "N/A");
1946 goto out;
1947 }
1948
1949 if (!xdata)
1950 goto out;
1951
1952 rsp_xdata = dict_new();
1953 if (!rsp_xdata)
1954 goto out;
1955
1956 if (dict_get(xdata, GLUSTERFS_OPEN_FD_COUNT)) {
1957 ret = dict_set_uint32(rsp_xdata, GLUSTERFS_OPEN_FD_COUNT,
1958 fd->inode->fd_count);
1959 if (ret < 0) {
1960 gf_msg(this->name, GF_LOG_WARNING, 0, P_MSG_DICT_SET_FAILED,
1961 "%s: Failed to set "
1962 "dictionary value for %s",
1963 uuid_utoa(fd->inode->gfid), GLUSTERFS_OPEN_FD_COUNT);
1964 }
1965 }
1966
1967 if (dict_get(xdata, GLUSTERFS_ACTIVE_FD_COUNT)) {
1968 ret = dict_set_uint32(rsp_xdata, GLUSTERFS_ACTIVE_FD_COUNT,
1969 fd->inode->active_fd_count);
1970 if (ret < 0) {
1971 gf_msg(this->name, GF_LOG_WARNING, 0, P_MSG_DICT_SET_FAILED,
1972 "%s: Failed to set "
1973 "dictionary value for %s",
1974 uuid_utoa(fd->inode->gfid), GLUSTERFS_ACTIVE_FD_COUNT);
1975 }
1976 }
1977
1978 if (dict_get(xdata, GLUSTERFS_WRITE_IS_APPEND)) {
1979 ret = dict_set_uint32(rsp_xdata, GLUSTERFS_WRITE_IS_APPEND, is_append);
1980 if (ret < 0) {
1981 gf_msg(this->name, GF_LOG_WARNING, 0, P_MSG_DICT_SET_FAILED,
1982 "%s: Failed to set "
1983 "dictionary value for %s",
1984 uuid_utoa(fd->inode->gfid), GLUSTERFS_WRITE_IS_APPEND);
1985 }
1986 }
1987 out:
1988 return rsp_xdata;
1989 }
1990
1991 int32_t
1992 posix_writev(call_frame_t *frame, xlator_t *this, fd_t *fd,
1993 struct iovec *vector, int32_t count, off_t offset, uint32_t flags,
1994 struct iobref *iobref, dict_t *xdata)
1995 {
1996 int32_t op_ret = -1;
1997 int32_t op_errno = 0;
1998 int _fd = -1;
1999 struct posix_private *priv = NULL;
2000 struct posix_fd *pfd = NULL;
2001 struct iatt preop = {
2002 0,
2003 };
2004 struct iatt postop = {
2005 0,
2006 };
2007 int ret = -1;
2008 dict_t *rsp_xdata = NULL;
2009 int is_append = 0;
2010 gf_boolean_t locked = _gf_false;
2011 gf_boolean_t write_append = _gf_false;
2012 gf_boolean_t update_atomic = _gf_false;
2013 posix_inode_ctx_t *ctx = NULL;
2014 gf_boolean_t check_space_error = _gf_false;
2015 struct stat statbuf = {
2016 0,
2017 };
2018 int totlen = 0;
2019 int idx = 0;
2020 gf_boolean_t cs_obj_status, cs_obj_repair;
2021
2022 VALIDATE_OR_GOTO(frame, unwind);
2023 VALIDATE_OR_GOTO(this, unwind);
2024 VALIDATE_OR_GOTO(fd, unwind);
2025 VALIDATE_OR_GOTO(fd->inode, unwind);
2026 VALIDATE_OR_GOTO(vector, unwind);
2027 VALIDATE_OR_GOTO(this->private, unwind);
2028
2029 priv = this->private;
2030
2031 VALIDATE_OR_GOTO(priv, unwind);
2032 DISK_SPACE_CHECK_AND_GOTO(frame, priv, xdata, op_ret, op_errno, out);
2033
2034 overwrite:
2035
2036 check_space_error = _gf_true;
2037 if ((fd->inode->ia_type == IA_IFBLK) || (fd->inode->ia_type == IA_IFCHR)) {
2038 gf_msg(this->name, GF_LOG_ERROR, EINVAL, P_MSG_INVALID_ARGUMENT,
2039 "writev received on a block/char file (%s)",
2040 uuid_utoa(fd->inode->gfid));
2041 op_errno = EINVAL;
2042 goto out;
2043 }
2044
2045 ret = posix_fd_ctx_get(fd, this, &pfd, &op_errno);
2046 if (ret < 0) {
2047 gf_msg(this->name, GF_LOG_WARNING, ret, P_MSG_PFD_NULL,
2048 "pfd is NULL from fd=%p", fd);
2049 goto out;
2050 }
2051
2052 _fd = pfd->fd;
2053
2054 if (xdata) {
2055 ret = posix_check_internal_writes(this, fd->inode, _fd, xdata);
2056 if (ret < 0) {
2057 gf_msg(this->name, GF_LOG_ERROR, 0, 0,
2058 "possible overwrite from internal client, fd=%p", fd);
2059 op_ret = -1;
2060 op_errno = EBUSY;
2061 goto out;
2062 }
2063 if (dict_get(xdata, GLUSTERFS_WRITE_IS_APPEND))
2064 write_append = _gf_true;
2065 if (dict_get(xdata, GLUSTERFS_WRITE_UPDATE_ATOMIC))
2066 update_atomic = _gf_true;
2067 }
2068
2069 /* The write_is_append check and write must happen
2070 atomically. Else another write can overtake this
2071 write after the check and get written earlier.
2072
2073 So lock before preop-stat and unlock after write.
2074 */
2075
2076 /*
2077 * The update_atomic option is to instruct posix to do prestat,
2078 * write and poststat atomically. This is to prevent any modification to
2079 * ia_size and ia_blocks until poststat and the diff in their values
2080 * between pre and poststat could be of use for some translators (shard
2081 * as of today).
2082 */
2083
2084 op_ret = posix_inode_ctx_get_all(fd->inode, this, &ctx);
2085 if (op_ret < 0) {
2086 op_errno = ENOMEM;
2087 goto out;
2088 }
2089
2090 if (write_append || update_atomic) {
2091 locked = _gf_true;
2092 pthread_mutex_lock(&ctx->write_atomic_lock);
2093 }
2094
2095 op_ret = posix_fdstat(this, fd->inode, _fd, &preop, _gf_true);
2096 if (op_ret == -1) {
2097 op_errno = errno;
2098 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_FSTAT_FAILED,
2099 "pre-operation fstat failed on fd=%p", fd);
2100 goto out;
2101 }
2102
2103 if (xdata) {
2104 cs_obj_status = dict_get_sizen(xdata, GF_CS_OBJECT_STATUS);
2105 cs_obj_repair = dict_get_sizen(xdata, GF_CS_OBJECT_REPAIR);
2106 if (cs_obj_status || cs_obj_repair) {
2107 op_ret = posix_cs_maintenance(this, fd, NULL, &_fd, &preop, NULL,
2108 cs_obj_status, cs_obj_repair,
2109 &rsp_xdata, _gf_false);
2110 if (op_ret < 0) {
2111 gf_msg(this->name, GF_LOG_ERROR, 0, 0,
2112 "file state check failed, fd %p", fd);
2113 op_errno = EIO;
2114 goto out;
2115 }
2116 }
2117 if (cs_obj_status)
2118 posix_update_iatt_buf(&preop, _fd, NULL);
2119 }
2120
2121 if (locked && write_append) {
2122 if (preop.ia_size == offset || (fd->flags & O_APPEND))
2123 is_append = 1;
2124 }
2125
2126 op_ret = __posix_writev(_fd, vector, count, offset,
2127 (pfd->flags & O_DIRECT));
2128
2129 if (locked && (!update_atomic)) {
2130 pthread_mutex_unlock(&ctx->write_atomic_lock);
2131 locked = _gf_false;
2132 }
2133
2134 if (op_ret < 0) {
2135 op_errno = -op_ret;
2136 op_ret = -1;
2137 gf_msg(this->name, GF_LOG_ERROR, op_errno, P_MSG_WRITE_FAILED,
2138 "write failed: offset %" PRIu64 ",", offset);
2139 goto out;
2140 }
2141
2142 rsp_xdata = _fill_writev_xdata(fd, xdata, this, is_append);
2143 /* writev successful, we also need to get the stat of
2144 * the file we wrote to
2145 */
2146
2147 ret = posix_fdstat(this, fd->inode, _fd, &postop, _gf_true);
2148 if (ret == -1) {
2149 op_ret = -1;
2150 op_errno = errno;
2151 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_FSTAT_FAILED,
2152 "post-operation fstat failed on fd=%p", fd);
2153 goto out;
2154 }
2155
2156 posix_set_ctime(frame, this, NULL, pfd->fd, fd->inode, &postop);
2157
2158 if (locked) {
2159 pthread_mutex_unlock(&ctx->write_atomic_lock);
2160 locked = _gf_false;
2161 }
2162
2163 if (flags & (O_SYNC | O_DSYNC)) {
2164 ret = sys_fsync(_fd);
2165 if (ret) {
2166 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_WRITEV_FAILED,
2167 "fsync() in writev on fd %d failed", _fd);
2168 op_ret = -1;
2169 op_errno = errno;
2170 goto out;
2171 }
2172 }
2173
2174 GF_ATOMIC_ADD(priv->write_value, op_ret);
2175
2176 out:
2177
2178 if (locked) {
2179 pthread_mutex_unlock(&ctx->write_atomic_lock);
2180 locked = _gf_false;
2181 }
2182 /* Check overwrite in case if errorno is ENOSPC, there could be
2183 a situation the disk_space_check thread is not set disk_space_flag
2184 because the thread is already waiting on sleep and other thread has
2185 already consumed free disk space, at the same time if another client try
2186 to overwrite the data it would get failed.
2187 */
2188 if (op_errno == ENOSPC && !check_space_error) {
2189 ret = posix_fd_ctx_get(fd, this, &pfd, &op_errno);
2190 if (ret < 0) {
2191 gf_msg(this->name, GF_LOG_WARNING, ret, P_MSG_PFD_NULL,
2192 "pfd is NULL from fd=%p", fd);
2193 goto unwind;
2194 }
2195
2196 if (sys_fstat(pfd->fd, &statbuf) < 0) {
2197 gf_msg(this->name, GF_LOG_WARNING, op_errno, P_MSG_FILE_OP_FAILED,
2198 "%d", pfd->fd);
2199 goto unwind;
2200 }
2201
2202 for (idx = 0; idx < count; idx++) {
2203 totlen = vector[idx].iov_len;
2204 }
2205
2206 if ((offset + totlen <= statbuf.st_size) &&
2207 !(statbuf.st_blocks * statbuf.st_blksize < statbuf.st_size)) {
2208 gf_msg_debug(this->name, 0,
2209 "io vector size will not"
2210 " change disk size so allow overwrite for"
2211 " fd %d",
2212 pfd->fd);
2213 goto overwrite;
2214 }
2215 }
2216
2217 unwind:
2218 STACK_UNWIND_STRICT(writev, frame, op_ret, op_errno, &preop, &postop,
2219 rsp_xdata);
2220
2221 if (rsp_xdata)
2222 dict_unref(rsp_xdata);
2223 return 0;
2224 }
2225
2226 int32_t
2227 posix_copy_file_range(call_frame_t *frame, xlator_t *this, fd_t *fd_in,
2228 off64_t off_in, fd_t *fd_out, off64_t off_out, size_t len,
2229 uint32_t flags, dict_t *xdata)
2230 {
2231 int32_t op_ret = -1;
2232 int32_t op_errno = 0;
2233 int _fd_in = -1;
2234 int _fd_out = -1;
2235 struct posix_private *priv = NULL;
2236 struct posix_fd *pfd_in = NULL;
2237 struct posix_fd *pfd_out = NULL;
2238 struct iatt preop_dst = {
2239 0,
2240 };
2241 struct iatt postop_dst = {
2242 0,
2243 };
2244 struct iatt stbuf = {
2245 0,
2246 };
2247 int ret = -1;
2248 dict_t *rsp_xdata = NULL;
2249 int is_append = 0;
2250 gf_boolean_t locked = _gf_false;
2251 gf_boolean_t update_atomic = _gf_false;
2252 posix_inode_ctx_t *ctx = NULL;
2253 char in_uuid_str[64] = {0}, out_uuid_str[64] = {0};
2254 gf_boolean_t cs_obj_status, cs_obj_repair;
2255
2256 VALIDATE_OR_GOTO(frame, out);
2257 VALIDATE_OR_GOTO(this, out);
2258 VALIDATE_OR_GOTO(fd_in, out);
2259 VALIDATE_OR_GOTO(fd_in->inode, out);
2260 VALIDATE_OR_GOTO(fd_out, out);
2261 VALIDATE_OR_GOTO(fd_out->inode, out);
2262 VALIDATE_OR_GOTO(this->private, out);
2263
2264 priv = this->private;
2265
2266 VALIDATE_OR_GOTO(priv, out);
2267 DISK_SPACE_CHECK_AND_GOTO(frame, priv, xdata, op_ret, op_errno, out);
2268
2269 if (posix_check_dev_file(this, fd_in->inode, "copy_file_range", &op_errno))
2270 goto out;
2271
2272 if (posix_check_dev_file(this, fd_out->inode, "copy_file_range", &op_errno))
2273 goto out;
2274
2275 ret = posix_fd_ctx_get(fd_in, this, &pfd_in, &op_errno);
2276 if (ret < 0) {
2277 gf_msg(this->name, GF_LOG_WARNING, ret, P_MSG_PFD_NULL,
2278 "pfd is NULL from fd=%p", fd_in);
2279 goto out;
2280 }
2281
2282 _fd_in = pfd_in->fd;
2283
2284 ret = posix_fd_ctx_get(fd_out, this, &pfd_out, &op_errno);
2285 if (ret < 0) {
2286 gf_msg(this->name, GF_LOG_WARNING, ret, P_MSG_PFD_NULL,
2287 "pfd is NULL from fd=%p", fd_out);
2288 goto out;
2289 }
2290
2291 _fd_out = pfd_out->fd;
2292
2293 /*
2294 * Currently, the internal write is checked via xdata which
2295 * is set by some xlator above. It could be due to several of
2296 * the reasons such as healing or a snapshot operation happening
2297 * using copy_file_range. As of now (i.e. writing the patch with
2298 * this change) none of the xlators above posix are using the
2299 * internal write with copy_file_range. In future it might
2300 * change. Atleast as of now the hope is that, when that happens
2301 * this functon or fop does not require additional changes for
2302 * handling internal writes.
2303 */
2304
2305 if (xdata) {
2306 ret = posix_check_internal_writes(this, fd_out->inode, _fd_out, xdata);
2307 if (ret < 0) {
2308 gf_msg(this->name, GF_LOG_ERROR, 0, 0,
2309 "possible overwrite from internal client, fd=%p", fd_out);
2310 op_ret = -1;
2311 op_errno = EBUSY;
2312 goto out;
2313 }
2314
2315 if (dict_get(xdata, GLUSTERFS_WRITE_UPDATE_ATOMIC))
2316 update_atomic = _gf_true;
2317 }
2318
2319 /*
2320 * The update_atomic option is to instruct posix to do prestat,
2321 * write and poststat atomically. This is to prevent any modification to
2322 * ia_size and ia_blocks until poststat and the diff in their values
2323 * between pre and poststat could be of use for some translators.
2324 * This is similar to the atomic write operation. atmoic write is
2325 * (i.e. prestat + write + poststat) used by shard as of now. In case,
2326 * some xlator needs copy_file_range to be atomic from prestat and postat
2327 * prespective (i.e. prestat + copy_file_range + poststat) then it has
2328 * to send "GLUSTERFS_WRITE_UPDATE_ATOMIC" key in xdata.
2329 */
2330
2331 op_ret = posix_inode_ctx_get_all(fd_out->inode, this, &ctx);
2332 if (op_ret < 0) {
2333 op_errno = ENOMEM;
2334 goto out;
2335 }
2336
2337 if (update_atomic) {
2338 ret = pthread_mutex_lock(&ctx->write_atomic_lock);
2339 if (!ret)
2340 locked = _gf_true;
2341 else {
2342 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_MUTEX_FAILED,
2343 "failed to hold write atomic lock on %s",
2344 uuid_utoa(fd_out->inode->gfid));
2345 goto out;
2346 }
2347 }
2348
2349 op_ret = posix_fdstat(this, fd_out->inode, _fd_out, &preop_dst, _gf_true);
2350 if (op_ret == -1) {
2351 op_errno = errno;
2352 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_FSTAT_FAILED,
2353 "pre-operation fstat failed on fd=%p", fd_out);
2354 goto out;
2355 }
2356
2357 /*
2358 * Since, only the destination file (fd_out) is undergoing
2359 * modification, the write related tests are done on that.
2360 * i.e. this is treater similar to as if the destination file
2361 * undergoing write fop from maintenance perspective.
2362 */
2363 if (xdata) {
2364 cs_obj_status = dict_get_sizen(xdata, GF_CS_OBJECT_STATUS);
2365 cs_obj_repair = dict_get_sizen(xdata, GF_CS_OBJECT_REPAIR);
2366 if (cs_obj_status || cs_obj_repair) {
2367 op_ret = posix_cs_maintenance(this, fd_out, NULL, &_fd_out,
2368 &preop_dst, NULL, cs_obj_status,
2369 cs_obj_repair, &rsp_xdata, _gf_false);
2370 if (op_ret < 0) {
2371 gf_msg(this->name, GF_LOG_ERROR, 0, 0,
2372 "file state check failed, fd %p", fd_out);
2373 op_errno = EIO;
2374 goto out;
2375 }
2376 }
2377 }
2378
2379 /*
2380 * NOTE: This is just doing a single execution of copy_file_range
2381 * system call. If the returned value of this system call is less
2382 * than len, then should we keep doing it in a for loop until the
2383 * copy_file_range of all the len bytes is done?
2384 * Check the example program provided in the man page of
2385 * copy_file_range.
2386 * If so, then a separate variables for both off_in and off_out
2387 * should be used which are initialized to off_in and off_out
2388 * that this function call receives, but then advanced by the
2389 * value returned by sys_copy_file_range and then use that as
2390 * off_in and off_out for next instance of copy_file_range execution.
2391 */
2392 op_ret = sys_copy_file_range(_fd_in, &off_in, _fd_out, &off_out, len,
2393 flags);
2394
2395 if (op_ret < 0) {
2396 op_errno = errno;
2397 gf_msg(this->name, GF_LOG_ERROR, op_errno, P_MSG_COPY_FILE_RANGE_FAILED,
2398 "copy_file_range failed: fd_in: %p (gfid: %s) ,"
2399 " fd_out %p (gfid:%s)",
2400 fd_in, uuid_utoa_r(fd_in->inode->gfid, in_uuid_str), fd_out,
2401 uuid_utoa_r(fd_out->inode->gfid, out_uuid_str));
2402 goto out;
2403 }
2404
2405 /*
2406 * Let this be as it is for now. This function collects
2407 * infomration such as open fd count etc. So, even though
2408 * is_append does not apply to copy_file_range, for now,
2409 * allowing it to be recorded in the dict as _gf_false.
2410 */
2411 rsp_xdata = _fill_writev_xdata(fd_out, xdata, this, is_append);
2412
2413 /* copy_file_range successful, we also need to get the stat of
2414 * the file we wrote to (i.e. destination file or fd_out).
2415 */
2416 ret = posix_fdstat(this, fd_out->inode, _fd_out, &postop_dst, _gf_true);
2417 if (ret == -1) {
2418 op_ret = -1;
2419 op_errno = errno;
2420 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_FSTAT_FAILED,
2421 "post-operation fstat failed on fd=%p", fd_out);
2422 goto out;
2423 }
2424
2425 /*
2426 * Also perform the stat on the source fd (i.e. fd_in). For now,
2427 * allowing it to be done within the locked region if the request
2428 * is for atomic operation (and update) of copy_file_range.
2429 */
2430 ret = posix_fdstat(this, fd_in->inode, _fd_in, &stbuf, _gf_true);
2431 if (ret == -1) {
2432 op_ret = -1;
2433 op_errno = errno;
2434 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_FSTAT_FAILED,
2435 "post-operation fstat failed on fd=%p", fd_in);
2436 goto out;
2437 }
2438
2439 /*
2440 * The core logic of what time attributes are to be updated
2441 * on a fop is decided at client side xlator utime.
2442 * All the remaining fops call posix_set_ctime function
2443 * to update the {a,m,c}time. But, for all the other fops,
2444 * the operation is happening on only one file (or inode).
2445 * But here, there are 2 fds (source and destination). Hence
2446 * the new function below to update the appropriate times for
2447 * both the source and the destination file.
2448 * For the source file, if at all anything has to be updated,
2449 * it would be atime (as that file is only read, not updated).
2450 * For the destination file, the attributes that require the
2451 * modification would be mtime and ctime.
2452 * What times have to be changed is actually determined by
2453 * utime xlator. But, all of them would be in frame->root->flags.
2454 * So, currently posix assumes that, the atime flag is for
2455 * the source file and the other 2 flags are for the destination
2456 * file. Since, the assumption is rigid (i.e. atime for source
2457 * and {m,c}time for destination), the below function is called
2458 * posix_set_ctime_cfr (cfr standing for copy_file_range).
2459 * FUTURE TODO:
2460 * In future, some other functionality or fop might operate
2461 * simultaneously on 2 files. Then, depending upon what that new
2462 * fop does or what are its requirements, the below function might
2463 * require changes to become generic for consumption in case of
2464 * simultaneous operations on 2 files.
2465 */
2466 posix_set_ctime_cfr(frame, this, NULL, pfd_in->fd, fd_in->inode, &stbuf,
2467 NULL, pfd_out->fd, fd_out->inode, &postop_dst);
2468
2469 if (locked) {
2470 pthread_mutex_unlock(&ctx->write_atomic_lock);
2471 locked = _gf_false;
2472 }
2473
2474 /*
2475 * Record copy_file_range in priv->write_value for now.
2476 * If not needed, remove below section of code along with
2477 * this comment (or add comment to explain why it is not
2478 * needed).
2479 */
2480 GF_ATOMIC_ADD(priv->write_value, op_ret);
2481
2482 out:
2483
2484 if (locked) {
2485 pthread_mutex_unlock(&ctx->write_atomic_lock);
2486 locked = _gf_false;
2487 }
2488
2489 STACK_UNWIND_STRICT(copy_file_range, frame, op_ret, op_errno, &stbuf,
2490 &preop_dst, &postop_dst, rsp_xdata);
2491
2492 if (rsp_xdata)
2493 dict_unref(rsp_xdata);
2494 return 0;
2495 }
2496
2497 int32_t
2498 posix_statfs(call_frame_t *frame, xlator_t *this, loc_t *loc, dict_t *xdata)
2499 {
2500 char *real_path = NULL;
2501 int32_t op_ret = -1;
2502 int32_t op_errno = 0;
2503 struct statvfs buf = {
2504 0,
2505 };
2506 struct posix_private *priv = NULL;
2507 int shared_by = 1;
2508 double percent = 0;
2509 uint64_t reserved_blocks = 0;
2510
2511 VALIDATE_OR_GOTO(frame, out);
2512 VALIDATE_OR_GOTO(this, out);
2513 VALIDATE_OR_GOTO(loc, out);
2514 VALIDATE_OR_GOTO(this->private, out);
2515
2516 MAKE_INODE_HANDLE(real_path, this, loc, NULL);
2517 if (!real_path) {
2518 op_ret = -1;
2519 op_errno = ESTALE;
2520 goto out;
2521 }
2522
2523 priv = this->private;
2524
2525 op_ret = sys_statvfs(real_path, &buf);
2526
2527 if (op_ret == -1) {
2528 op_errno = errno;
2529 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_STATVFS_FAILED,
2530 "statvfs failed on gfid-handle %s (path: %s)", real_path,
2531 loc->path);
2532 goto out;
2533 }
2534
2535 if (priv->disk_unit_percent) {
2536 percent = priv->disk_reserve;
2537 reserved_blocks = (((buf.f_blocks * percent) / 100) + 0.5);
2538 } else {
2539 if (buf.f_bsize) {
2540 reserved_blocks = (priv->disk_reserve + buf.f_bsize - 1) /
2541 buf.f_bsize;
2542 }
2543 }
2544
2545 if (buf.f_bfree > reserved_blocks) {
2546 buf.f_bfree = (buf.f_bfree - reserved_blocks);
2547 if (buf.f_bavail > buf.f_bfree) {
2548 buf.f_bavail = buf.f_bfree;
2549 }
2550 } else {
2551 buf.f_bfree = 0;
2552 buf.f_bavail = 0;
2553 }
2554
2555 shared_by = priv->shared_brick_count;
2556 if (shared_by > 1) {
2557 buf.f_blocks /= shared_by;
2558 buf.f_bfree /= shared_by;
2559 buf.f_bavail /= shared_by;
2560 buf.f_files /= shared_by;
2561 buf.f_ffree /= shared_by;
2562 buf.f_favail /= shared_by;
2563 }
2564
2565 if (!priv->export_statfs) {
2566 buf.f_blocks = 0;
2567 buf.f_bfree = 0;
2568 buf.f_bavail = 0;
2569 buf.f_files = 0;
2570 buf.f_ffree = 0;
2571 buf.f_favail = 0;
2572 }
2573
2574 op_ret = 0;
2575
2576 out:
2577 STACK_UNWIND_STRICT(statfs, frame, op_ret, op_errno, &buf, NULL);
2578 return 0;
2579 }
2580
2581 int32_t
2582 posix_flush(call_frame_t *frame, xlator_t *this, fd_t *fd, dict_t *xdata)
2583 {
2584 int32_t op_ret = -1;
2585 int32_t op_errno = 0;
2586 int ret = -1;
2587 struct posix_fd *pfd = NULL;
2588
2589 VALIDATE_OR_GOTO(frame, out);
2590 VALIDATE_OR_GOTO(this, out);
2591 VALIDATE_OR_GOTO(fd, out);
2592
2593 ret = posix_fd_ctx_get(fd, this, &pfd, &op_errno);
2594 if (ret < 0) {
2595 gf_msg(this->name, GF_LOG_WARNING, op_errno, P_MSG_PFD_NULL,
2596 "pfd is NULL on fd=%p", fd);
2597 goto out;
2598 }
2599
2600 op_ret = 0;
2601
2602 out:
2603 STACK_UNWIND_STRICT(flush, frame, op_ret, op_errno, NULL);
2604
2605 return 0;
2606 }
2607
2608 static int
2609 posix_unlink_renamed_file(xlator_t *this, inode_t *inode)
2610 {
2611 int ret = 0;
2612 char *unlink_path = NULL;
2613 uint64_t ctx_uint = 0;
2614 posix_inode_ctx_t *ctx = NULL;
2615 struct posix_private *priv = this->private;
2616
2617 ret = inode_ctx_get(inode, this, &ctx_uint);
2618
2619 if (ret < 0)
2620 goto out;
2621
2622 ctx = (posix_inode_ctx_t *)(uintptr_t)ctx_uint;
2623
2624 if (ctx->unlink_flag == GF_UNLINK_TRUE) {
2625 POSIX_GET_FILE_UNLINK_PATH(priv->base_path, inode->gfid, unlink_path);
2626 if (!unlink_path) {
2627 gf_msg(this->name, GF_LOG_ERROR, ENOMEM, P_MSG_UNLINK_FAILED,
2628 "Failed to remove gfid :%s", uuid_utoa(inode->gfid));
2629 ret = -1;
2630 } else {
2631 ret = sys_unlink(unlink_path);
2632 if (!ret)
2633 ctx->unlink_flag = GF_UNLINK_FALSE;
2634 }
2635 }
2636
2637 out:
2638 return ret;
2639 }
2640
2641 int32_t
2642 posix_release(xlator_t *this, fd_t *fd)
2643 {
2644 struct posix_fd *pfd = NULL;
2645
2646 VALIDATE_OR_GOTO(this, out);
2647 VALIDATE_OR_GOTO(fd, out);
2648
2649 if (fd->inode->active_fd_count == 0)
2650 posix_unlink_renamed_file(this, fd->inode);
2651
2652 pfd = fd_ctx_del_ptr(fd, this);
2653 if (pfd == NULL) {
2654 gf_msg(this->name, GF_LOG_WARNING, 0, P_MSG_PFD_NULL,
2655 "pfd is NULL from fd=%p", fd);
2656 goto out;
2657 }
2658
2659 if (pfd->dir) {
2660 gf_msg(this->name, GF_LOG_WARNING, 0, P_MSG_DIR_NOT_NULL,
2661 "pfd->dir is %p (not NULL) for file fd=%p", pfd->dir, fd);
2662 }
2663
2664 posix_add_fd_to_cleanup(this, pfd);
2665
2666 out:
2667 return 0;
2668 }
2669
2670 int
2671 posix_batch_fsync(call_frame_t *frame, xlator_t *this, fd_t *fd, int datasync,
2672 dict_t *xdata)
2673 {
2674 call_stub_t *stub = NULL;
2675 struct posix_private *priv = NULL;
2676
2677 priv = this->private;
2678
2679 stub = fop_fsync_stub(frame, default_fsync, fd, datasync, xdata);
2680 if (!stub) {
2681 STACK_UNWIND_STRICT(fsync, frame, -1, ENOMEM, 0, 0, 0);
2682 return 0;
2683 }
2684
2685 pthread_mutex_lock(&priv->fsync_mutex);
2686 {
2687 list_add_tail(&stub->list, &priv->fsyncs);
2688 priv->fsync_queue_count++;
2689 pthread_cond_signal(&priv->fsync_cond);
2690 }
2691 pthread_mutex_unlock(&priv->fsync_mutex);
2692
2693 return 0;
2694 }
2695
2696 int32_t
2697 posix_fsync(call_frame_t *frame, xlator_t *this, fd_t *fd, int32_t datasync,
2698 dict_t *xdata)
2699 {
2700 int32_t op_ret = -1;
2701 int32_t op_errno = 0;
2702 int _fd = -1;
2703 struct posix_fd *pfd = NULL;
2704 int ret = -1;
2705 struct iatt preop = {
2706 0,
2707 };
2708 struct iatt postop = {
2709 0,
2710 };
2711 struct posix_private *priv = NULL;
2712
2713 DECLARE_OLD_FS_ID_VAR;
2714
2715 VALIDATE_OR_GOTO(frame, out);
2716 VALIDATE_OR_GOTO(this, out);
2717 VALIDATE_OR_GOTO(fd, out);
2718
2719 SET_FS_ID(frame->root->uid, frame->root->gid);
2720
2721 #ifdef GF_DARWIN_HOST_OS
2722 /* Always return success in case of fsync in MAC OS X */
2723 op_ret = 0;
2724 goto out;
2725 #endif
2726
2727 priv = this->private;
2728
2729 if (priv->batch_fsync_mode && xdata && dict_get(xdata, "batch-fsync")) {
2730 posix_batch_fsync(frame, this, fd, datasync, xdata);
2731 return 0;
2732 }
2733
2734 ret = posix_fd_ctx_get(fd, this, &pfd, &op_errno);
2735 if (ret < 0) {
2736 gf_msg(this->name, GF_LOG_WARNING, op_errno, P_MSG_PFD_NULL,
2737 "pfd not found in fd's ctx");
2738 goto out;
2739 }
2740
2741 _fd = pfd->fd;
2742
2743 op_ret = posix_fdstat(this, fd->inode, _fd, &preop, _gf_true);
2744 if (op_ret == -1) {
2745 op_errno = errno;
2746 gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_FSTAT_FAILED,
2747 "pre-operation fstat failed on fd=%p", fd);
2748 goto out;
2749 }
2750
2751 if (datasync) {
2752 op_ret = sys_fdatasync(_fd);
2753 if (op_ret == -1) {
2754 op_errno = errno;
2755 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_FSYNC_FAILED,
2756 "fdatasync on fd=%p"
2757 "failed:",
2758 fd);
2759 goto out;
2760 }
2761 } else {
2762 op_ret = sys_fsync(_fd);
2763 if (op_ret == -1) {
2764 op_errno = errno;
2765 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_FSYNC_FAILED,
2766 "fsync on fd=%p "
2767 "failed",
2768 fd);
2769 goto out;
2770 }
2771 }
2772
2773 op_ret = posix_fdstat(this, fd->inode, _fd, &postop, _gf_true);
2774 if (op_ret == -1) {
2775 op_errno = errno;
2776 gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_FSTAT_FAILED,
2777 "post-operation fstat failed on fd=%p", fd);
2778 goto out;
2779 }
2780
2781 op_ret = 0;
2782
2783 out:
2784 SET_TO_OLD_FS_ID();
2785
2786 STACK_UNWIND_STRICT(fsync, frame, op_ret, op_errno, &preop, &postop, NULL);
2787
2788 return 0;
2789 }
2790
2791 static int gf_posix_xattr_enotsup_log;
2792 static int
2793 _handle_setxattr_keyvalue_pair(dict_t *d, char *k, data_t *v, void *tmp)
2794 {
2795 posix_xattr_filler_t *filler = NULL;
2796
2797 filler = tmp;
2798
2799 return posix_handle_pair(filler->this, filler->loc, filler->real_path, k, v,
2800 filler->flags, filler->stbuf);
2801 }
2802
2803 #ifdef GF_DARWIN_HOST_OS
2804 static int
2805 map_xattr_flags(int flags)
2806 {
2807 /* DARWIN has different defines on XATTR_ flags.
2808 There do not seem to be a POSIX standard
2809 Parse any other flags over.
2810 */
2811 int darwinflags = flags &
2812 ~(GF_XATTR_CREATE | GF_XATTR_REPLACE | XATTR_REPLACE);
2813 if (GF_XATTR_CREATE & flags)
2814 darwinflags |= XATTR_CREATE;
2815 if (GF_XATTR_REPLACE & flags)
2816 darwinflags |= XATTR_REPLACE;
2817 return darwinflags;
2818 }
2819 #endif
2820
2821 int32_t
2822 posix_setxattr(call_frame_t *frame, xlator_t *this, loc_t *loc, dict_t *dict,
2823 int flags, dict_t *xdata)
2824 {
2825 int32_t op_ret = -1;
2826 int32_t op_errno = 0;
2827 char *real_path = NULL;
2828 char *acl_xattr = NULL;
2829 struct iatt preop = {0};
2830 struct iatt postop = {0};
2831 int32_t ret = 0;
2832 ssize_t acl_size = 0;
2833 dict_t *xattr = NULL;
2834 dict_t *subvol_xattrs = NULL;
2835 posix_xattr_filler_t filler = {
2836 0,
2837 };
2838 struct posix_private *priv = NULL;
2839 struct iatt tmp_stbuf = {
2840 0,
2841 };
2842 data_t *tdata = NULL;
2843 char *cs_var = NULL;
2844 gf_cs_obj_state state = -1;
2845 int i = 0;
2846 int len;
2847 struct mdata_iatt mdata_iatt = {
2848 0,
2849 };
2850 int8_t sync_backend_xattrs = _gf_false;
2851 data_pair_t *custom_xattrs;
2852 data_t *keyval = NULL;
2853 char **xattrs_to_heal = get_xattrs_to_heal();
2854
2855 DECLARE_OLD_FS_ID_VAR;
2856 SET_FS_ID(frame->root->uid, frame->root->gid);
2857
2858 VALIDATE_OR_GOTO(frame, out);
2859 VALIDATE_OR_GOTO(this, out);
2860 VALIDATE_OR_GOTO(this->private, out);
2861 VALIDATE_OR_GOTO(loc, out);
2862 VALIDATE_OR_GOTO(dict, out);
2863
2864 priv = this->private;
2865 DISK_SPACE_CHECK_AND_GOTO(frame, priv, xdata, op_ret, op_errno, out);
2866
2867 MAKE_INODE_HANDLE(real_path, this, loc, NULL);
2868 if (!real_path) {
2869 op_ret = -1;
2870 op_errno = ESTALE;
2871 goto out;
2872 }
2873
2874 ret = dict_get_mdata(dict, CTIME_MDATA_XDATA_KEY, &mdata_iatt);
2875 if (ret == 0) {
2876 /* This is initiated by lookup when ctime feature is enabled to create
2877 * "trusted.glusterfs.mdata" xattr if not present. These are the files
2878 * which were created when ctime feature is disabled.
2879 */
2880 ret = posix_set_mdata_xattr_legacy_files(this, loc->inode, real_path,
2881 &mdata_iatt, &op_errno);
2882 if (ret != 0) {
2883 op_ret = -1;
2884 }
2885 goto out;
2886 }
2887
2888 posix_pstat(this, loc->inode, loc->gfid, real_path, &preop, _gf_false,
2889 _gf_true);
2890
2891 op_ret = -1;
2892
2893 dict_del(dict, GFID_XATTR_KEY);
2894 dict_del(dict, GF_XATTR_VOL_ID_KEY);
2895 /* the io-stats-dump key should not reach disk */
2896 dict_del(dict, GF_XATTR_IOSTATS_DUMP_KEY);
2897
2898 tdata = dict_get_sizen(dict, GF_CS_OBJECT_UPLOAD_COMPLETE);
2899 if (tdata) {
2900 /*TODO: move the following to a different function */
2901 LOCK(&loc->inode->lock);
2902 {
2903 state = posix_cs_check_status(this, real_path, NULL, &preop);
2904 if (state != GF_CS_LOCAL) {
2905 op_errno = EINVAL;
2906 ret = posix_cs_set_state(this, &xattr, state, real_path, NULL);
2907 if (ret) {
2908 gf_msg(this->name, GF_LOG_ERROR, 0, 0, "set state failed");
2909 }
2910 goto unlock;
2911 }
2912
2913 ret = posix_pstat(this, loc->inode, loc->gfid, real_path,
2914 &tmp_stbuf, _gf_true, _gf_true);
2915 if (ret) {
2916 op_errno = EINVAL;
2917 goto unlock;
2918 }
2919
2920 cs_var = alloca(4096);
2921 sprintf(cs_var, "%" PRId64, tmp_stbuf.ia_mtime);
2922
2923 /*TODO: may be should consider nano-second also */
2924 if (strncmp(cs_var, tdata->data, tdata->len) > 0) {
2925 gf_msg(this->name, GF_LOG_ERROR, 0, 0,
2926 "mtime "
2927 "passed is different from seen by file now."
2928 " Will skip truncating the file");
2929 ret = -1;
2930 op_errno = EINVAL;
2931 goto unlock;
2932 }
2933
2934 len = sprintf(cs_var, "%" PRIu64, tmp_stbuf.ia_size);
2935
2936 ret = sys_lsetxattr(real_path, GF_CS_OBJECT_SIZE, cs_var, len,
2937 flags);
2938 if (ret) {
2939 op_errno = errno;
2940 gf_msg(this->name, GF_LOG_ERROR, 0, 0,
2941 "setxattr failed. key %s err %d", GF_CS_OBJECT_SIZE,
2942 ret);
2943 goto unlock;
2944 }
2945
2946 len = sprintf(cs_var, "%" PRIu64, tmp_stbuf.ia_blocks);
2947
2948 ret = sys_lsetxattr(real_path, GF_CS_NUM_BLOCKS, cs_var, len,
2949 flags);
2950 if (ret) {
2951 op_errno = errno;
2952 gf_msg(this->name, GF_LOG_ERROR, 0, 0,
2953 "setxattr failed. key %s err %d", GF_CS_NUM_BLOCKS, ret);
2954 goto unlock;
2955 }
2956
2957 len = sprintf(cs_var, "%" PRIu32, tmp_stbuf.ia_blksize);
2958
2959 ret = sys_lsetxattr(real_path, GF_CS_BLOCK_SIZE, cs_var, len,
2960 flags);
2961 if (ret) {
2962 op_errno = errno;
2963 gf_msg(this->name, GF_LOG_ERROR, 0, 0,
2964 "setxattr failed. key %s err %d", GF_CS_BLOCK_SIZE, ret);
2965 goto unlock;
2966 }
2967
2968 memset(cs_var, 0, 4096);
2969 if (loc->path[0] == '/') {
2970 for (i = 1; i < strlen(loc->path); i++) {
2971 cs_var[i - 1] = loc->path[i];
2972 }
2973
2974 cs_var[i] = '\0';
2975 gf_msg_debug(this->name, GF_LOG_ERROR, "remotepath %s", cs_var);
2976 }
2977
2978 ret = sys_lsetxattr(real_path, GF_CS_OBJECT_REMOTE, cs_var,
2979 strlen(cs_var), flags);
2980 if (ret) {
2981 op_errno = errno;
2982 gf_log("POSIX", GF_LOG_ERROR,
2983 "setxattr failed - %s"
2984 " %d",
2985 GF_CS_OBJECT_SIZE, ret);
2986 goto unlock;
2987 }
2988
2989 ret = sys_truncate(real_path, 0);
2990 if (ret) {
2991 op_errno = errno;
2992 gf_log("POSIX", GF_LOG_ERROR,
2993 "truncate failed - %s"
2994 " %d",
2995 GF_CS_OBJECT_SIZE, ret);
2996 ret = sys_lremovexattr(real_path, GF_CS_OBJECT_REMOTE);
2997 if (ret) {
2998 op_errno = errno;
2999 gf_log("POSIX", GF_LOG_ERROR,
3000 "removexattr "
3001 "failed post processing- %s"
3002 " %d",
3003 GF_CS_OBJECT_SIZE, ret);
3004 }
3005 goto unlock;
3006 } else {
3007 state = GF_CS_REMOTE;
3008 ret = posix_cs_set_state(this, &xattr, state, real_path, NULL);
3009 if (ret) {
3010 gf_msg(this->name, GF_LOG_ERROR, 0, 0, "set state failed");
3011 }
3012 }
3013 }
3014 unlock:
3015 UNLOCK(&loc->inode->lock);
3016 op_ret = ret;
3017 goto out;
3018 }
3019
3020 filler.real_path = real_path;
3021 filler.this = this;
3022 filler.stbuf = &preop;
3023 filler.loc = loc;
3024
3025 #ifdef GF_DARWIN_HOST_OS
3026 filler.flags = map_xattr_flags(flags);
3027 #else
3028 filler.flags = flags;
3029 #endif
3030 op_ret = dict_foreach(dict, _handle_setxattr_keyvalue_pair, &filler);
3031 if (op_ret < 0) {
3032 op_errno = -op_ret;
3033 op_ret = -1;
3034 goto out;
3035 }
3036
3037 if (xdata) {
3038 ret = dict_get_int8(xdata, "sync_backend_xattrs", &sync_backend_xattrs);
3039 if (ret) {
3040 gf_msg_debug(this->name, -ret, "Unable to get sync_backend_xattrs");
3041 }
3042 }
3043
3044 if (sync_backend_xattrs) {
3045 /* List all custom xattrs */
3046 subvol_xattrs = dict_new();
3047 if (!subvol_xattrs)
3048 goto out;
3049
3050 ret = dict_set_int32_sizen(xdata, "list-xattr", 1);
3051 if (ret) {
3052 gf_msg(this->name, GF_LOG_ERROR, 0, ENOMEM,
3053 "Unable to set list-xattr in dict ");
3054 goto out;
3055 }
3056
3057 subvol_xattrs = posix_xattr_fill(this, real_path, loc, NULL, -1, xdata,
3058 NULL);
3059
3060 /* Remove all user xattrs from the file */
3061 dict_foreach_fnmatch(subvol_xattrs, "user.*", posix_delete_user_xattr,
3062 real_path);
3063
3064 /* Remove all custom xattrs from the file */
3065 for (i = 1; xattrs_to_heal[i]; i++) {
3066 keyval = dict_get(subvol_xattrs, xattrs_to_heal[i]);
3067 if (keyval) {
3068 ret = sys_lremovexattr(real_path, xattrs_to_heal[i]);
3069 if (ret) {
3070 gf_msg(this->name, GF_LOG_ERROR, P_MSG_XATTR_NOT_REMOVED,
3071 errno, "removexattr failed. key %s path %s",
3072 xattrs_to_heal[i], loc->path);
3073 goto out;
3074 }
3075
3076 dict_del(subvol_xattrs, xattrs_to_heal[i]);
3077 keyval = NULL;
3078 }
3079 }
3080
3081 /* Set custom xattrs based on info provided by DHT */
3082 custom_xattrs = dict->members_list;
3083
3084 while (custom_xattrs != NULL) {
3085 ret = sys_lsetxattr(real_path, custom_xattrs->key,
3086 custom_xattrs->value->data,
3087 custom_xattrs->value->len, flags);
3088 if (ret) {
3089 op_errno = errno;
3090 gf_log(this->name, GF_LOG_ERROR, "setxattr failed - %s %d",
3091 custom_xattrs->key, ret);
3092 goto out;
3093 }
3094
3095 custom_xattrs = custom_xattrs->next;
3096 }
3097 }
3098
3099 xattr = dict_new();
3100 if (!xattr)
3101 goto out;
3102
3103 /*
3104 * FIXFIX: Send the stbuf info in the xdata for now
3105 * This is used by DHT to redirect FOPs if the file is being migrated
3106 * Ignore errors for now
3107 */
3108 ret = posix_pstat(this, loc->inode, loc->gfid, real_path, &postop,
3109 _gf_false, _gf_true);
3110 if (ret)
3111 goto out;
3112
3113 ret = posix_set_iatt_in_dict(xattr, &preop, &postop);
3114
3115 /*
3116 * ACL can be set on a file/folder using GF_POSIX_ACL_*_KEY xattrs which
3117 * won't aware of access-control xlator. To update its context correctly,
3118 * POSIX_ACL_*_XATTR stored in xdata which is send in the call_back path.
3119 */
3120 if (dict_get(dict, GF_POSIX_ACL_ACCESS)) {
3121 /*
3122 * The size of buffer will be know after calling sys_lgetxattr,
3123 * so first we allocate buffer with large size(~4k), then we
3124 * reduced into required size using GF_REALLO().
3125 */
3126 acl_xattr = GF_CALLOC(1, ACL_BUFFER_MAX, gf_posix_mt_char);
3127 if (!acl_xattr)
3128 goto out;
3129
3130 acl_size = sys_lgetxattr(real_path, POSIX_ACL_ACCESS_XATTR, acl_xattr,
3131 ACL_BUFFER_MAX);
3132
3133 if (acl_size < 0) {
3134 gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_XATTR_FAILED,
3135 "Posix acl is not set "
3136 "properly at the backend");
3137 goto out;
3138 }
3139
3140 /* If acl_size is more than max buffer size, just ignore it */
3141 if (acl_size >= ACL_BUFFER_MAX) {
3142 gf_msg(this->name, GF_LOG_WARNING, ENOMEM, P_MSG_BUFFER_OVERFLOW,
3143 "size of acl is more"
3144 "than the buffer");
3145 goto out;
3146 }
3147
3148 acl_xattr = GF_REALLOC(acl_xattr, acl_size);
3149 if (!acl_xattr)
3150 goto out;
3151
3152 ret = dict_set_bin(xattr, POSIX_ACL_ACCESS_XATTR, acl_xattr, acl_size);
3153 if (ret) {
3154 gf_msg(this->name, GF_LOG_WARNING, 0, P_MSG_SET_XDATA_FAIL,
3155 "failed to set"
3156 "xdata for acl");
3157 GF_FREE(acl_xattr);
3158 goto out;
3159 }
3160 }
3161
3162 if (dict_get(dict, GF_POSIX_ACL_DEFAULT)) {
3163 acl_xattr = GF_CALLOC(1, ACL_BUFFER_MAX, gf_posix_mt_char);
3164 if (!acl_xattr)
3165 goto out;
3166
3167 acl_size = sys_lgetxattr(real_path, POSIX_ACL_DEFAULT_XATTR, acl_xattr,
3168 ACL_BUFFER_MAX);
3169
3170 if (acl_size < 0) {
3171 gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_XATTR_FAILED,
3172 "Posix acl is not set "
3173 "properly at the backend");
3174 goto out;
3175 }
3176
3177 if (acl_size >= ACL_BUFFER_MAX) {
3178 gf_msg(this->name, GF_LOG_WARNING, ENOMEM, P_MSG_BUFFER_OVERFLOW,
3179 "size of acl is more"
3180 "than the buffer");
3181 goto out;
3182 }
3183
3184 acl_xattr = GF_REALLOC(acl_xattr, acl_size);
3185 if (!acl_xattr)
3186 goto out;
3187
3188 ret = dict_set_bin(xattr, POSIX_ACL_DEFAULT_XATTR, acl_xattr, acl_size);
3189 if (ret) {
3190 gf_msg(this->name, GF_LOG_WARNING, 0, P_MSG_SET_XDATA_FAIL,
3191 "failed to set"
3192 "xdata for acl");
3193 GF_FREE(acl_xattr);
3194 goto out;
3195 }
3196 }
3197
3198 out:
3199 SET_TO_OLD_FS_ID();
3200
3201 STACK_UNWIND_STRICT(setxattr, frame, op_ret, op_errno, xattr);
3202
3203 if (xattr)
3204 dict_unref(xattr);
3205
3206 if (subvol_xattrs)
3207 dict_unref(subvol_xattrs);
3208
3209 return 0;
3210 }
3211
3212 int
3213 posix_xattr_get_real_filename(call_frame_t *frame, xlator_t *this, loc_t *loc,
3214 const char *key, dict_t *dict, dict_t *xdata)
3215 {
3216 int ret = -1;
3217 int op_ret = -1;
3218 const char *fname = NULL;
3219 char *real_path = NULL;
3220 char *found = NULL;
3221 DIR *fd = NULL;
3222 struct dirent *entry = NULL;
3223 struct dirent scratch[2] = {
3224 {
3225 0,
3226 },
3227 };
3228
3229 MAKE_INODE_HANDLE(real_path, this, loc, NULL);
3230 if (!real_path) {
3231 return -ESTALE;
3232 }
3233 if (op_ret == -1) {
3234 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LSTAT_FAILED,
3235 "posix_xattr_get_real_filename (lstat) on "
3236 "gfid-handle %s (path: %s) failed",
3237 real_path, loc->path);
3238 return -errno;
3239 }
3240
3241 fd = sys_opendir(real_path);
3242 if (!fd)
3243 return -errno;
3244
3245 fname = key + SLEN(GF_XATTR_GET_REAL_FILENAME_KEY);
3246
3247 for (;;) {
3248 errno = 0;
3249 entry = sys_readdir(fd, scratch);
3250 if (!entry || errno != 0)
3251 break;
3252
3253 if (strcasecmp(entry->d_name, fname) == 0) {
3254 found = gf_strdup(entry->d_name);
3255 if (!found) {
3256 (void)sys_closedir(fd);
3257 return -ENOMEM;
3258 }
3259 break;
3260 }
3261 }
3262
3263 (void)sys_closedir(fd);
3264
3265 if (!found)
3266 return -ENOATTR;
3267
3268 ret = dict_set_dynstr(dict, (char *)key, found);
3269 if (ret) {
3270 GF_FREE(found);
3271 return -ENOMEM;
3272 }
3273 ret = strlen(found) + 1;
3274
3275 return ret;
3276 }
3277
3278 int
3279 posix_get_ancestry_directory(xlator_t *this, inode_t *leaf_inode,
3280 gf_dirent_t *head, char **path, int type,
3281 int32_t *op_errno, dict_t *xdata)
3282 {
3283 ssize_t handle_size = 0;
3284 struct posix_private *priv = NULL;
3285 inode_t *inode = NULL;
3286 int ret = -1;
3287 char dirpath[PATH_MAX] = {
3288 0,
3289 };
3290
3291 priv = this->private;
3292
3293 handle_size = POSIX_GFID_HANDLE_SIZE(priv->base_path_length);
3294
3295 ret = posix_make_ancestryfromgfid(
3296 this, dirpath, PATH_MAX + 1, head, type | POSIX_ANCESTRY_PATH,
3297 leaf_inode->gfid, handle_size, priv->base_path, leaf_inode->table,
3298 &inode, xdata, op_errno);
3299 if (ret < 0)
3300 goto out;
3301
3302 /* there is already a reference in loc->inode */
3303 inode_unref(inode);
3304
3305 if ((type & POSIX_ANCESTRY_PATH) && (path != NULL)) {
3306 if (strcmp(dirpath, "/"))
3307 dirpath[strlen(dirpath) - 1] = '\0';
3308
3309 *path = gf_strdup(dirpath);
3310 }
3311
3312 out:
3313 return ret;
3314 }
3315
3316 int32_t
3317 posix_links_in_same_directory(char *dirpath, int count, inode_t *leaf_inode,
3318 inode_t *parent, struct stat *stbuf,
3319 gf_dirent_t *head, char **path, int type,
3320 dict_t *xdata, int32_t *op_errno)
3321 {
3322 int op_ret = -1;
3323 gf_dirent_t *gf_entry = NULL;
3324 xlator_t *this = NULL;
3325 struct posix_private *priv = NULL;
3326 DIR *dirp = NULL;
3327 struct dirent *entry = NULL;
3328 struct dirent scratch[2] = {
3329 {
3330 0,
3331 },
3332 };
3333 char temppath[PATH_MAX] = {
3334 0,
3335 };
3336 char scr[PATH_MAX * 4] = {
3337 0,
3338 };
3339
3340 this = THIS;
3341
3342 priv = this->private;
3343
3344 dirp = sys_opendir(dirpath);
3345 if (!dirp) {
3346 *op_errno = errno;
3347 gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_OPEN_FAILED,
3348 "could not opendir %s", dirpath);
3349 goto out;
3350 }
3351
3352 while (count > 0) {
3353 errno = 0;
3354 entry = sys_readdir(dirp, scratch);
3355 if (!entry || errno != 0)
3356 break;
3357
3358 if (entry->d_ino != stbuf->st_ino)
3359 continue;
3360
3361 /* Linking an inode here, can cause a race in posix_acl.
3362 Parent inode gets linked here, but before
3363 it reaches posix_acl_readdirp_cbk, create/lookup can
3364 come on a leaf-inode, as parent-inode-ctx not yet updated
3365 in posix_acl_readdirp_cbk, create and lookup can fail
3366 with EACCESS. So do the inode linking in the quota xlator
3367
3368 linked_inode = inode_link (leaf_inode, parent,
3369 entry->d_name, NULL);
3370
3371 GF_ASSERT (linked_inode == leaf_inode);
3372 inode_unref (linked_inode);*/
3373
3374 if (type & POSIX_ANCESTRY_DENTRY) {
3375 loc_t loc = {
3376 0,
3377 };
3378
3379 loc.inode = inode_ref(leaf_inode);
3380 gf_uuid_copy(loc.gfid, leaf_inode->gfid);
3381
3382 (void)snprintf(temppath, sizeof(temppath), "%s/%s", dirpath,
3383 entry->d_name);
3384
3385 gf_entry = gf_dirent_for_name(entry->d_name);
3386 if (!gf_entry) {
3387 gf_msg(this->name, GF_LOG_ERROR, ENOMEM, 0, "gf_entry is NULL");
3388 op_ret = -1;
3389 *op_errno = ENOMEM;
3390 inode_unref(loc.inode);
3391 goto out;
3392 }
3393 gf_entry->inode = inode_ref(leaf_inode);
3394 gf_entry->dict = posix_xattr_fill(this, temppath, &loc, NULL, -1,
3395 xdata, NULL);
3396 iatt_from_stat(&(gf_entry->d_stat), stbuf);
3397
3398 list_add_tail(&gf_entry->list, &head->list);
3399 loc_wipe(&loc);
3400 }
3401
3402 if (type & POSIX_ANCESTRY_PATH) {
3403 (void)snprintf(temppath, sizeof(temppath), "%s/%s",
3404 &dirpath[priv->base_path_length], entry->d_name);
3405 if (!*path) {
3406 *path = gf_strdup(temppath);
3407 } else {
3408 /* creating a colon separated */
3409 /* list of hard links */
3410 (void)snprintf(scr, sizeof(scr), "%s:%s", *path, temppath);
3411
3412 GF_FREE(*path);
3413 *path = gf_strdup(scr);
3414 }
3415 if (!*path) {
3416 op_ret = -1;
3417 *op_errno = ENOMEM;
3418 goto out;
3419 }
3420 }
3421
3422 count--;
3423 }
3424
3425 op_ret = 0;
3426 out:
3427 if (dirp) {
3428 op_ret = sys_closedir(dirp);
3429 if (op_ret == -1) {
3430 *op_errno = errno;
3431 gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_CLOSE_FAILED,
3432 "closedir failed");
3433 }
3434 }
3435
3436 return op_ret;
3437 }
3438
3439 int
3440 posix_get_ancestry_non_directory(xlator_t *this, inode_t *leaf_inode,
3441 gf_dirent_t *head, char **path, int type,
3442 int32_t *op_errno, dict_t *xdata)
3443 {
3444 size_t remaining_size = 0;
3445 int op_ret = -1, pathlen = -1;
3446 ssize_t handle_size = 0;
3447 uuid_t pgfid = {
3448 0,
3449 };
3450 int nlink_samepgfid = 0;
3451 struct stat stbuf = {
3452 0,
3453 };
3454 char *list = NULL;
3455 int32_t list_offset = 0;
3456 struct posix_private *priv = NULL;
3457 ssize_t size = 0;
3458 inode_t *parent = NULL;
3459 loc_t *loc = NULL;
3460 char *leaf_path = NULL;
3461 char key[4096] = {
3462 0,
3463 };
3464 char dirpath[PATH_MAX] = {
3465 0,
3466 };
3467 int len;
3468
3469 priv = this->private;
3470
3471 loc = GF_CALLOC(1, sizeof(*loc), gf_posix_mt_char);
3472 if (loc == NULL) {
3473 op_ret = -1;
3474 *op_errno = ENOMEM;
3475 goto out;
3476 }
3477
3478 gf_uuid_copy(loc->gfid, leaf_inode->gfid);
3479
3480 MAKE_INODE_HANDLE(leaf_path, this, loc, NULL);
3481 if (!leaf_path) {
3482 GF_FREE(loc);
3483 *op_errno = ESTALE;
3484 goto out;
3485 }
3486 GF_FREE(loc);
3487
3488 size = sys_llistxattr(leaf_path, NULL, 0);
3489 if (size == -1) {
3490 *op_errno = errno;
3491 if ((errno == ENOTSUP) || (errno == ENOSYS)) {
3492 GF_LOG_OCCASIONALLY(gf_posix_xattr_enotsup_log, this->name,
3493 GF_LOG_WARNING,
3494 "Extended attributes not "
3495 "supported (try remounting brick"
3496 " with 'user_xattr' flag)");
3497
3498 } else {
3499 gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_XATTR_FAILED,
3500 "listxattr failed on"
3501 "%s",
3502 leaf_path);
3503 }
3504
3505 goto out;
3506 }
3507
3508 if (size == 0) {
3509 op_ret = 0;
3510 goto out;
3511 }
3512
3513 list = GF_MALLOC(size, gf_posix_mt_char);
3514 if (!list) {
3515 *op_errno = errno;
3516 goto out;
3517 }
3518
3519 size = sys_llistxattr(leaf_path, list, size);
3520 if (size < 0) {
3521 op_ret = -1;
3522 *op_errno = errno;
3523 goto out;
3524 }
3525 remaining_size = size;
3526 list_offset = 0;
3527
3528 op_ret = sys_lstat(leaf_path, &stbuf);
3529 if (op_ret == -1) {
3530 *op_errno = errno;
3531 gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_LSTAT_FAILED,
3532 "lstat failed on %s", leaf_path);
3533 goto out;
3534 }
3535
3536 while (remaining_size > 0) {
3537 len = snprintf(key, sizeof(key), "%s", list + list_offset);
3538 if (strncmp(key, PGFID_XATTR_KEY_PREFIX,
3539 SLEN(PGFID_XATTR_KEY_PREFIX)) != 0)
3540 goto next;
3541
3542 op_ret = sys_lgetxattr(leaf_path, key, &nlink_samepgfid,
3543 sizeof(nlink_samepgfid));
3544 if (op_ret == -1) {
3545 *op_errno = errno;
3546 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_XATTR_FAILED,
3547 "getxattr failed on "
3548 "%s: key = %s ",
3549 leaf_path, key);
3550 goto out;
3551 }
3552
3553 nlink_samepgfid = be32toh(nlink_samepgfid);
3554
3555 gf_uuid_parse(key + SLEN(PGFID_XATTR_KEY_PREFIX), pgfid);
3556
3557 handle_size = POSIX_GFID_HANDLE_SIZE(priv->base_path_length);
3558
3559 /* constructing the absolute real path of parent dir */
3560 snprintf(dirpath, sizeof(dirpath), "%s", priv->base_path);
3561 pathlen = PATH_MAX + 1 - priv->base_path_length;
3562
3563 op_ret = posix_make_ancestryfromgfid(
3564 this, dirpath + priv->base_path_length, pathlen, head,
3565 type | POSIX_ANCESTRY_PATH, pgfid, handle_size, priv->base_path,
3566 leaf_inode->table, &parent, xdata, op_errno);
3567 if (op_ret < 0) {
3568 goto next;
3569 }
3570
3571 dirpath[strlen(dirpath) - 1] = '\0';
3572
3573 posix_links_in_same_directory(dirpath, nlink_samepgfid, leaf_inode,
3574 parent, &stbuf, head, path, type, xdata,
3575 op_errno);
3576
3577 if (parent != NULL) {
3578 inode_unref(parent);
3579 parent = NULL;
3580 }
3581
3582 next:
3583 remaining_size -= (len + 1);
3584 list_offset += (len + 1);
3585 } /* while (remaining_size > 0) */
3586
3587 op_ret = 0;
3588
3589 out:
3590 GF_FREE(list);
3591 return op_ret;
3592 }
3593
3594 int
3595 posix_get_ancestry(xlator_t *this, inode_t *leaf_inode, gf_dirent_t *head,
3596 char **path, int type, int32_t *op_errno, dict_t *xdata)
3597 {
3598 int ret = -1;
3599 struct posix_private *priv = NULL;
3600
3601 priv = this->private;
3602
3603 if (IA_ISDIR(leaf_inode->ia_type)) {
3604 ret = posix_get_ancestry_directory(this, leaf_inode, head, path, type,
3605 op_errno, xdata);
3606 } else {
3607 if (!priv->update_pgfid_nlinks)
3608 goto out;
3609 ret = posix_get_ancestry_non_directory(this, leaf_inode, head, path,
3610 type, op_errno, xdata);
3611 }
3612
3613 out:
3614 if (ret && path && *path) {
3615 GF_FREE(*path);
3616 *path = NULL;
3617 }
3618
3619 return ret;
3620 }
3621
3622 /**
3623 * posix_getxattr - this function returns a dictionary with all the
3624 * key:value pair present as xattr. used for
3625 * both 'listxattr' and 'getxattr'.
3626 */
3627 int32_t
3628 posix_getxattr(call_frame_t *frame, xlator_t *this, loc_t *loc,
3629 const char *name, dict_t *xdata)
3630 {
3631 struct posix_private *priv = NULL;
3632 int32_t op_ret = -1;
3633 int32_t op_errno = 0;
3634 char *value = NULL;
3635 char *real_path = NULL;
3636 dict_t *dict = NULL;
3637 int ret = -1;
3638 char *path = NULL;
3639 char *rpath = NULL;
3640 ssize_t size = 0;
3641 char *list = NULL;
3642 int32_t list_offset = 0;
3643 size_t remaining_size = 0;
3644 char *host_buf = NULL;
3645 char *keybuffer = NULL;
3646 int keybuff_len;
3647 char *value_buf = NULL;
3648 gf_boolean_t have_val = _gf_false;
3649 struct iatt buf = {
3650 0,
3651 };
3652 dict_t *xattr_rsp = NULL;
3653
3654 DECLARE_OLD_FS_ID_VAR;
3655
3656 VALIDATE_OR_GOTO(frame, out);
3657 VALIDATE_OR_GOTO(this, out);
3658 VALIDATE_OR_GOTO(loc, out);
3659 VALIDATE_OR_GOTO(this->private, out);
3660
3661 SET_FS_ID(frame->root->uid, frame->root->gid);
3662 MAKE_INODE_HANDLE(real_path, this, loc, NULL);
3663
3664 op_ret = -1;
3665 priv = this->private;
3666
3667 ret = posix_handle_georep_xattrs(frame, name, &op_errno, _gf_true);
3668 if (ret == -1) {
3669 op_ret = -1;
3670 /* errno should be set from the above function*/
3671 goto out;
3672 }
3673
3674 ret = posix_handle_mdata_xattr(frame, name, &op_errno);
3675 if (ret == -1) {
3676 op_ret = -1;
3677 /* errno should be set from the above function*/
3678 goto out;
3679 }
3680
3681 if (name && posix_is_gfid2path_xattr(name)) {
3682 op_ret = -1;
3683 op_errno = ENOATTR;
3684 goto out;
3685 }
3686
3687 dict = dict_new();
3688 if (!dict) {
3689 op_errno = ENOMEM;
3690 goto out;
3691 }
3692
3693 if (loc->inode && name && GF_POSIX_ACL_REQUEST(name)) {
3694 ret = posix_pacl_get(real_path, -1, name, &value);
3695 if (ret || !value) {
3696 op_errno = errno;
3697 gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_ACL_FAILED,
3698 "could not get acl (%s) for"
3699 "gfid-handle %s (path: %s)",
3700 name, real_path, loc->path);
3701 op_ret = -1;
3702 goto out;
3703 }
3704
3705 ret = dict_set_dynstr(dict, (char *)name, value);
3706 if (ret < 0) {
3707 GF_FREE(value);
3708 gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_ACL_FAILED,
3709 "could not set acl (%s) for %s "
3710 "(gfid-handle: %s) in dictionary",
3711 name, loc->path, real_path);
3712 op_ret = -1;
3713 op_errno = ENOMEM;
3714 goto out;
3715 }
3716
3717 size = ret;
3718 goto done;
3719 }
3720
3721 if (loc->inode && name &&
3722 (strncmp(name, GF_XATTR_GET_REAL_FILENAME_KEY,
3723 SLEN(GF_XATTR_GET_REAL_FILENAME_KEY)) == 0)) {
3724 ret = posix_xattr_get_real_filename(frame, this, loc, name, dict,
3725 xdata);
3726 if (ret < 0) {
3727 op_ret = -1;
3728 op_errno = -ret;
3729 if (op_errno == ENOATTR) {
3730 gf_msg_debug(this->name, 0,
3731 "Failed to get "
3732 "real filename (%s, %s)",
3733 loc->path, name);
3734 } else {
3735 gf_msg(this->name, GF_LOG_WARNING, op_errno,
3736 P_MSG_GETTING_FILENAME_FAILED,
3737 "Failed to get real filename (%s, %s):", loc->path,
3738 name);
3739 }
3740 goto out;
3741 }
3742
3743 size = ret;
3744 goto done;
3745 }
3746
3747 if (loc->inode && name && !strcmp(name, GLUSTERFS_OPEN_FD_COUNT)) {
3748 if (!fd_list_empty(loc->inode)) {
3749 ret = dict_set_uint32(dict, (char *)name, 1);
3750 if (ret < 0) {
3751 gf_msg(this->name, GF_LOG_WARNING, 0, P_MSG_DICT_SET_FAILED,
3752 "Failed to set "
3753 "dictionary value for %s",
3754 name);
3755 op_errno = ENOMEM;
3756 goto out;
3757 }
3758 } else {
3759 ret = dict_set_uint32(dict, (char *)name, 0);
3760 if (ret < 0) {
3761 gf_msg(this->name, GF_LOG_WARNING, 0, P_MSG_DICT_SET_FAILED,
3762 "Failed to set "
3763 "dictionary value for %s",
3764 name);
3765 op_errno = ENOMEM;
3766 goto out;
3767 }
3768 }
3769 goto done;
3770 }
3771 if (loc->inode && name && (XATTR_IS_PATHINFO(name))) {
3772 VALIDATE_OR_GOTO(this->private, out);
3773 if (LOC_HAS_ABSPATH(loc)) {
3774 MAKE_REAL_PATH(rpath, this, loc->path);
3775 } else {
3776 rpath = real_path;
3777 }
3778 size = gf_asprintf(
3779 &host_buf, "<POSIX(%s):%s:%s>", priv->base_path,
3780 ((priv->node_uuid_pathinfo && !gf_uuid_is_null(priv->glusterd_uuid))
3781 ? uuid_utoa(priv->glusterd_uuid)
3782 : priv->hostname),
3783 rpath);
3784 if (size < 0) {
3785 op_errno = ENOMEM;
3786 goto out;
3787 }
3788 ret = dict_set_dynstr(dict, (char *)name, host_buf);
3789 if (ret < 0) {
3790 gf_msg(this->name, GF_LOG_WARNING, 0, P_MSG_DICT_SET_FAILED,
3791 "could not set value"
3792 " (%s) in dictionary",
3793 host_buf);
3794 GF_FREE(host_buf);
3795 op_errno = ENOMEM;
3796 goto out;
3797 }
3798
3799 goto done;
3800 }
3801
3802 if (loc->inode && name && (strcmp(name, GF_XATTR_NODE_UUID_KEY) == 0) &&
3803 !gf_uuid_is_null(priv->glusterd_uuid)) {
3804 size = gf_asprintf(&host_buf, "%s", uuid_utoa(priv->glusterd_uuid));
3805 if (size == -1) {
3806 op_errno = ENOMEM;
3807 goto out;
3808 }
3809 ret = dict_set_dynstr(dict, GF_XATTR_NODE_UUID_KEY, host_buf);
3810 if (ret < 0) {
3811 gf_msg(this->name, GF_LOG_WARNING, -ret, P_MSG_DICT_SET_FAILED,
3812 "could not set value"
3813 "(%s) in dictionary",
3814 host_buf);
3815 GF_FREE(host_buf);
3816 op_errno = -ret;
3817 goto out;
3818 }
3819 goto done;
3820 }
3821
3822 if (loc->inode && name && (strcmp(name, GFID_TO_PATH_KEY) == 0)) {
3823 ret = inode_path(loc->inode, NULL, &path);
3824 if (ret < 0) {
3825 op_errno = -ret;
3826 gf_msg(this->name, GF_LOG_WARNING, op_errno,
3827 P_MSG_INODE_PATH_GET_FAILED,
3828 "%s: could not get "
3829 "inode path",
3830 uuid_utoa(loc->inode->gfid));
3831 goto out;
3832 }
3833
3834 size = ret;
3835 ret = dict_set_dynstr(dict, GFID_TO_PATH_KEY, path);
3836 if (ret < 0) {
3837 op_errno = ENOMEM;
3838 GF_FREE(path);
3839 goto out;
3840 }
3841 goto done;
3842 }
3843
3844 if (loc->inode && name && (strcmp(name, GFID2PATH_VIRT_XATTR_KEY) == 0)) {
3845 if (!priv->gfid2path) {
3846 op_errno = ENOATTR;
3847 op_ret = -1;
3848 goto out;
3849 }
3850 ret = posix_get_gfid2path(this, loc->inode, real_path, &op_errno, dict);
3851 if (ret < 0) {
3852 op_ret = -1;
3853 goto out;
3854 }
3855 size = ret;
3856 goto done;
3857 }
3858
3859 if (loc->inode && name && (strcmp(name, GET_ANCESTRY_PATH_KEY) == 0)) {
3860 int type = POSIX_ANCESTRY_PATH;
3861
3862 op_ret = posix_get_ancestry(this, loc->inode, NULL, &path, type,
3863 &op_errno, xdata);
3864 if (op_ret < 0) {
3865 op_ret = -1;
3866 op_errno = ENODATA;
3867 goto out;
3868 }
3869 size = op_ret;
3870 op_ret = dict_set_dynstr(dict, GET_ANCESTRY_PATH_KEY, path);
3871 if (op_ret < 0) {
3872 gf_msg(this->name, GF_LOG_WARNING, -op_ret,
3873 P_MSG_GET_KEY_VALUE_FAILED,
3874 "could not get "
3875 "value for key (%s)",
3876 GET_ANCESTRY_PATH_KEY);
3877 GF_FREE(path);
3878 op_errno = ENOMEM;
3879 goto out;
3880 }
3881
3882 goto done;
3883 }
3884
3885 if (loc->inode && name &&
3886 (strncmp(name, GLUSTERFS_GET_OBJECT_SIGNATURE,
3887 SLEN(GLUSTERFS_GET_OBJECT_SIGNATURE)) == 0)) {
3888 op_ret = posix_get_objectsignature(real_path, dict);
3889 if (op_ret < 0) {
3890 op_errno = -op_ret;
3891 goto out;
3892 }
3893
3894 goto done;
3895 }
3896
3897 /* here allocate value_buf of 8192 bytes to avoid one extra getxattr
3898 call,If buffer size is small to hold the xattr result then it will
3899 allocate a new buffer value of required size and call getxattr again
3900 */
3901
3902 value_buf = alloca(XATTR_VAL_BUF_SIZE);
3903 if (name) {
3904 char *key = (char *)name;
3905
3906 keybuffer = key;
3907 #if defined(GF_DARWIN_HOST_OS_DISABLED)
3908 if (priv->xattr_user_namespace == XATTR_STRIP) {
3909 if (strncmp(key, "user.", 5) == 0) {
3910 key += 5;
3911 gf_msg_debug(this->name, 0,
3912 "getxattr for file %s (gfid-handle: %s)"
3913 " stripping user key: %s -> %s",
3914 loc->path, real_path, keybuffer, key);
3915 }
3916 }
3917 #endif
3918 size = sys_lgetxattr(real_path, key, value_buf, XATTR_VAL_BUF_SIZE - 1);
3919 if (size >= 0) {
3920 have_val = _gf_true;
3921 } else {
3922 if (errno == ERANGE) {
3923 gf_msg(this->name, GF_LOG_INFO, errno, P_MSG_XATTR_FAILED,
3924 "getxattr failed due to overflow of buffer"
3925 " on gfid-handle %s (path: %s) : %s ",
3926 real_path, loc->path, key);
3927 size = sys_lgetxattr(real_path, key, NULL, 0);
3928 }
3929 if (size == -1) {
3930 op_errno = errno;
3931 if ((op_errno == ENOTSUP) || (op_errno == ENOSYS)) {
3932 GF_LOG_OCCASIONALLY(gf_posix_xattr_enotsup_log, this->name,
3933 GF_LOG_WARNING,
3934 "Extended attributes not "
3935 "supported (try remounting"
3936 " brick with 'user_xattr' "
3937 "flag)");
3938 }
3939 if ((op_errno == ENOATTR) || (op_errno == ENODATA)) {
3940 gf_msg_debug(this->name, 0,
3941 "No such attribute:%s for file %s (path: %s)",
3942 key, real_path, loc->path);
3943 } else {
3944 gf_msg(this->name, GF_LOG_ERROR, op_errno,
3945 P_MSG_XATTR_FAILED,
3946 "getxattr failed on "
3947 "%s (path: %s): %s ",
3948 real_path, loc->path, key);
3949 }
3950 goto out;
3951 }
3952 }
3953 value = GF_MALLOC(size + 1, gf_posix_mt_char);
3954 if (!value) {
3955 op_ret = -1;
3956 op_errno = ENOMEM;
3957 goto out;
3958 }
3959 if (have_val) {
3960 memcpy(value, value_buf, size);
3961 } else {
3962 bzero(value, size + 1);
3963 size = sys_lgetxattr(real_path, key, value, size);
3964 if (size == -1) {
3965 op_ret = -1;
3966 op_errno = errno;
3967 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_XATTR_FAILED,
3968 "getxattr failed on %s (path: %s): key = %s", real_path,
3969 loc->path, key);
3970 GF_FREE(value);
3971 goto out;
3972 }
3973 }
3974 value[size] = '\0';
3975 op_ret = dict_set_dynptr(dict, key, value, size);
3976 if (op_ret < 0) {
3977 op_errno = -op_ret;
3978 gf_msg(this->name, GF_LOG_ERROR, op_errno, P_MSG_DICT_SET_FAILED,
3979 "dict set operation "
3980 "on %s (gfid-handle: %s) for the key %s failed.",
3981 loc->path, real_path, key);
3982 GF_FREE(value);
3983 goto out;
3984 }
3985
3986 goto done;
3987 }
3988
3989 have_val = _gf_false;
3990 size = sys_llistxattr(real_path, value_buf, XATTR_VAL_BUF_SIZE - 1);
3991 if (size > 0) {
3992 have_val = _gf_true;
3993 } else {
3994 if (errno == ERANGE) {
3995 gf_msg(this->name, GF_LOG_INFO, errno, P_MSG_XATTR_FAILED,
3996 "listxattr failed due to overflow of buffer"
3997 " on %s (path: %s) ",
3998 real_path, loc->path);
3999 size = sys_llistxattr(real_path, NULL, 0);
4000 }
4001 if (size == -1) {
4002 op_errno = errno;
4003 if ((errno == ENOTSUP) || (errno == ENOSYS)) {
4004 GF_LOG_OCCASIONALLY(gf_posix_xattr_enotsup_log, this->name,
4005 GF_LOG_WARNING,
4006 "Extended attributes not "
4007 "supported (try remounting"
4008 " brick with 'user_xattr' "
4009 "flag)");
4010 } else {
4011 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_XATTR_FAILED,
4012 "listxattr failed on %s (path: %s)", real_path,
4013 loc->path);
4014 }
4015 goto out;
4016 }
4017 if (size == 0)
4018 goto done;
4019 }
4020
4021 list = GF_MALLOC(size, gf_posix_mt_char);
4022 if (!list) {
4023 op_errno = errno;
4024 goto out;
4025 }
4026 if (have_val) {
4027 memcpy(list, value_buf, size);
4028 } else {
4029 size = sys_llistxattr(real_path, list, size);
4030 if (size < 0) {
4031 op_ret = -1;
4032 op_errno = errno;
4033 goto out;
4034 }
4035 }
4036 remaining_size = size;
4037 list_offset = 0;
4038 keybuffer = alloca(XATTR_KEY_BUF_SIZE);
4039 while (remaining_size > 0) {
4040 keybuff_len = snprintf(keybuffer, XATTR_KEY_BUF_SIZE, "%s",
4041 list + list_offset);
4042
4043 ret = posix_handle_georep_xattrs(frame, keybuffer, NULL, _gf_false);
4044 if (ret == -1)
4045 goto ignore;
4046
4047 ret = posix_handle_mdata_xattr(frame, keybuffer, &op_errno);
4048 if (ret == -1) {
4049 goto ignore;
4050 }
4051
4052 if (posix_is_gfid2path_xattr(keybuffer)) {
4053 goto ignore;
4054 }
4055
4056 have_val = _gf_false;
4057 size = sys_lgetxattr(real_path, keybuffer, value_buf,
4058 XATTR_VAL_BUF_SIZE - 1);
4059 if (size >= 0) {
4060 have_val = _gf_true;
4061 } else {
4062 if (errno == ERANGE) {
4063 gf_msg(this->name, GF_LOG_INFO, op_errno, P_MSG_XATTR_FAILED,
4064 "getxattr failed due to overflow of"
4065 " buffer on %s (path: %s): %s ",
4066 real_path, loc->path, keybuffer);
4067 size = sys_lgetxattr(real_path, keybuffer, NULL, 0);
4068 }
4069 if (size == -1) {
4070 op_errno = errno;
4071 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_XATTR_FAILED,
4072 "getxattr failed on"
4073 " %s (path: %s): key = %s ",
4074 real_path, loc->path, keybuffer);
4075 goto out;
4076 }
4077 }
4078 value = GF_MALLOC(size + 1, gf_posix_mt_char);
4079 if (!value) {
4080 op_errno = errno;
4081 goto out;
4082 }
4083 if (have_val) {
4084 memcpy(value, value_buf, size);
4085 } else {
4086 bzero(value, size + 1);
4087 size = sys_lgetxattr(real_path, keybuffer, value, size);
4088 if (size == -1) {
4089 op_errno = errno;
4090 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_XATTR_FAILED,
4091 "getxattr failed on"
4092 " %s (path: %s): key = %s ",
4093 real_path, loc->path, keybuffer);
4094 GF_FREE(value);
4095 goto out;
4096 }
4097 }
4098 value[size] = '\0';
4099 #ifdef GF_DARWIN_HOST_OS
4100 /* The protocol expect namespace for now */
4101 char *newkey = NULL;
4102 gf_add_prefix(XATTR_USER_PREFIX, keybuffer, &newkey);
4103 keybuff_len = snprintf(keybuffer, sizeof(keybuffer), "%s", newkey);
4104 GF_FREE(newkey);
4105 #endif
4106 op_ret = dict_set_dynptr(dict, keybuffer, value, size);
4107 if (op_ret < 0) {
4108 op_errno = -op_ret;
4109 gf_msg(this->name, GF_LOG_ERROR, op_errno, P_MSG_DICT_SET_FAILED,
4110 "dict set operation "
4111 "on %s (gfid-handle: %s) for the key %s failed.",
4112 loc->path, real_path, keybuffer);
4113 GF_FREE(value);
4114 goto out;
4115 }
4116
4117 ignore:
4118 remaining_size -= keybuff_len + 1;
4119 list_offset += keybuff_len + 1;
4120
4121 } /* while (remaining_size > 0) */
4122
4123 done:
4124 op_ret = size;
4125
4126 if (xdata && (op_ret >= 0)) {
4127 xattr_rsp = posix_xattr_fill(this, real_path, loc, NULL, -1, xdata,
4128 &buf);
4129 }
4130
4131 if (dict) {
4132 dict_del(dict, GFID_XATTR_KEY);
4133 dict_del(dict, GF_XATTR_VOL_ID_KEY);
4134 }
4135
4136 out:
4137 SET_TO_OLD_FS_ID();
4138
4139 STACK_UNWIND_STRICT(getxattr, frame, op_ret, op_errno, dict, xattr_rsp);
4140
4141 if (xattr_rsp)
4142 dict_unref(xattr_rsp);
4143
4144 if (dict) {
4145 dict_unref(dict);
4146 }
4147
4148 GF_FREE(list);
4149 return 0;
4150 }
4151
4152 int32_t
4153 posix_fgetxattr(call_frame_t *frame, xlator_t *this, fd_t *fd, const char *name,
4154 dict_t *xdata)
4155 {
4156 int32_t op_ret = -1;
4157 int32_t op_errno = EINVAL;
4158 struct posix_fd *pfd = NULL;
4159 int _fd = -1;
4160 int32_t list_offset = 0;
4161 ssize_t size = 0;
4162 size_t remaining_size = 0;
4163 char *value = NULL;
4164 char *list = NULL;
4165 dict_t *dict = NULL;
4166 int ret = -1;
4167 char key[4096] = {
4168 0,
4169 };
4170 int key_len;
4171 char *value_buf = NULL;
4172 gf_boolean_t have_val = _gf_false;
4173 struct iatt buf = {
4174 0,
4175 };
4176 dict_t *xattr_rsp = NULL;
4177 char *path = NULL;
4178 loc_t loc = {
4179 0,
4180 };
4181
4182 DECLARE_OLD_FS_ID_VAR;
4183
4184 VALIDATE_OR_GOTO(frame, out);
4185 VALIDATE_OR_GOTO(this, out);
4186 VALIDATE_OR_GOTO(fd, out);
4187
4188 SET_FS_ID(frame->root->uid, frame->root->gid);
4189
4190 ret = posix_fd_ctx_get(fd, this, &pfd, &op_errno);
4191 if (ret < 0) {
4192 op_ret = -1;
4193 gf_msg(this->name, GF_LOG_WARNING, op_errno, P_MSG_PFD_NULL,
4194 "pfd is NULL from fd=%p", fd);
4195 goto out;
4196 }
4197
4198 _fd = pfd->fd;
4199
4200 /* Get the total size */
4201 dict = dict_new();
4202 if (!dict) {
4203 op_ret = -1;
4204 op_errno = ENOMEM;
4205 goto out;
4206 }
4207
4208 if (fd->inode && name &&
4209 (strncmp(name, GF_XATTR_GET_REAL_FILENAME_KEY,
4210 SLEN(GF_XATTR_GET_REAL_FILENAME_KEY)) == 0)) {
4211 ret = inode_path(fd->inode, NULL, &path);
4212 if (ret < 0) {
4213 op_ret = -1;
4214 op_errno = -ret;
4215 goto out;
4216 }
4217
4218 loc.path = path;
4219 loc.inode = inode_ref(fd->inode);
4220 gf_uuid_copy(loc.gfid, fd->inode->gfid);
4221
4222 ret = posix_xattr_get_real_filename(frame, this, &loc, name, dict,
4223 xdata);
4224 if (ret < 0) {
4225 op_ret = -1;
4226 op_errno = -ret;
4227 if (op_errno == ENOATTR) {
4228 gf_msg_debug(this->name, 0,
4229 "Failed to get "
4230 "real filename (%s, %s)",
4231 loc.path, name);
4232 } else {
4233 gf_msg(this->name, GF_LOG_WARNING, op_errno,
4234 P_MSG_GETTING_FILENAME_FAILED,
4235 "Failed to get real filename (%s, %s):", loc.path, name);
4236 }
4237 loc_wipe(&loc);
4238 goto out;
4239 }
4240
4241 size = ret;
4242 loc_wipe(&loc);
4243 goto done;
4244 }
4245
4246 if (name && !strcmp(name, GLUSTERFS_OPEN_FD_COUNT)) {
4247 ret = dict_set_uint32(dict, (char *)name, 1);
4248 if (ret < 0) {
4249 op_ret = -1;
4250 size = -1;
4251 op_errno = ENOMEM;
4252 gf_msg(this->name, GF_LOG_WARNING, 0, P_MSG_DICT_SET_FAILED,
4253 "Failed to set "
4254 "dictionary value for %s",
4255 name);
4256 goto out;
4257 }
4258 goto done;
4259 }
4260
4261 if (name && strncmp(name, GLUSTERFS_GET_OBJECT_SIGNATURE,
4262 SLEN(GLUSTERFS_GET_OBJECT_SIGNATURE)) == 0) {
4263 op_ret = posix_fdget_objectsignature(_fd, dict);
4264 if (op_ret < 0) {
4265 gf_msg(this->name, GF_LOG_ERROR, 0, 0,
4266 "posix_fdget_objectsignature failed");
4267 op_errno = -op_ret;
4268 op_ret = -1;
4269 size = -1;
4270 goto out;
4271 }
4272
4273 goto done;
4274 }
4275
4276 /* here allocate value_buf of 8192 bytes to avoid one extra getxattr
4277 call,If buffer size is small to hold the xattr result then it will
4278 allocate a new buffer value of required size and call getxattr again
4279 */
4280 value_buf = alloca(XATTR_VAL_BUF_SIZE);
4281
4282 if (name) {
4283 key_len = snprintf(key, sizeof(key), "%s", name);
4284 #ifdef GF_DARWIN_HOST_OS
4285 struct posix_private *priv = NULL;
4286 priv = this->private;
4287 if (priv->xattr_user_namespace == XATTR_STRIP) {
4288 char *newkey = NULL;
4289 gf_add_prefix(XATTR_USER_PREFIX, key, &newkey);
4290 key_len = snprintf(key, sizeof(key), "%s", newkey);
4291 GF_FREE(newkey);
4292 }
4293 #endif
4294 size = sys_fgetxattr(_fd, key, value_buf, XATTR_VAL_BUF_SIZE - 1);
4295 if (size >= 0) {
4296 have_val = _gf_true;
4297 } else {
4298 if (errno == ERANGE) {
4299 gf_msg(this->name, GF_LOG_INFO, errno, P_MSG_XATTR_FAILED,
4300 "fgetxattr failed due to overflow of"
4301 "buffer on %s ",
4302 key);
4303 size = sys_fgetxattr(_fd, key, NULL, 0);
4304 }
4305 if (size == -1) {
4306 op_errno = errno;
4307 if (errno == ENODATA || errno == ENOATTR) {
4308 gf_msg_debug(this->name, op_errno,
4309 "fgetxattr"
4310 " failed on key %s",
4311 key);
4312 } else {
4313 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_XATTR_FAILED,
4314 "fgetxattr"
4315 " failed on key %s",
4316 key);
4317 }
4318 goto done;
4319 }
4320 }
4321 value = GF_MALLOC(size + 1, gf_posix_mt_char);
4322 if (!value) {
4323 op_ret = -1;
4324 op_errno = ENOMEM;
4325 goto out;
4326 }
4327 if (have_val) {
4328 memcpy(value, value_buf, size);
4329 } else {
4330 bzero(value, size + 1);
4331 size = sys_fgetxattr(_fd, key, value, size);
4332 if (size == -1) {
4333 op_ret = -1;
4334 op_errno = errno;
4335 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_XATTR_FAILED,
4336 "fgetxattr"
4337 " failed on fd %p for the key %s ",
4338 fd, key);
4339 GF_FREE(value);
4340 goto out;
4341 }
4342 }
4343
4344 value[size] = '\0';
4345 op_ret = dict_set_dynptr(dict, key, value, size);
4346 if (op_ret < 0) {
4347 op_errno = -op_ret;
4348 op_ret = -1;
4349 gf_msg(this->name, GF_LOG_ERROR, 0, P_MSG_DICT_SET_FAILED,
4350 "dict set operation "
4351 "on key %s failed",
4352 key);
4353 GF_FREE(value);
4354 goto out;
4355 }
4356
4357 goto done;
4358 }
4359 size = sys_flistxattr(_fd, value_buf, XATTR_VAL_BUF_SIZE - 1);
4360 if (size > 0) {
4361 have_val = _gf_true;
4362 } else {
4363 if (errno == ERANGE) {
4364 gf_msg(this->name, GF_LOG_INFO, errno, P_MSG_XATTR_FAILED,
4365 "listxattr failed due to overflow of buffer"
4366 " on %p ",
4367 fd);
4368 size = sys_flistxattr(_fd, NULL, 0);
4369 }
4370 if (size == -1) {
4371 op_ret = -1;
4372 op_errno = errno;
4373 if ((errno == ENOTSUP) || (errno == ENOSYS)) {
4374 GF_LOG_OCCASIONALLY(gf_posix_xattr_enotsup_log, this->name,
4375 GF_LOG_WARNING,
4376 "Extended attributes not "
4377 "supported (try remounting "
4378 "brick with 'user_xattr' flag)");
4379 } else {
4380 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_XATTR_FAILED,
4381 "listxattr failed "
4382 "on %p:",
4383 fd);
4384 }
4385 goto out;
4386 }
4387 if (size == 0)
4388 goto done;
4389 }
4390
4391 list = GF_MALLOC(size, gf_posix_mt_char);
4392 if (!list) {
4393 op_ret = -1;
4394 op_errno = ENOMEM;
4395 goto out;
4396 }
4397 if (have_val)
4398 memcpy(list, value_buf, size);
4399 else
4400 size = sys_flistxattr(_fd, list, size);
4401
4402 remaining_size = size;
4403 list_offset = 0;
4404 while (remaining_size > 0) {
4405 if (*(list + list_offset) == '\0')
4406 break;
4407
4408 key_len = snprintf(key, sizeof(key), "%s", list + list_offset);
4409 have_val = _gf_false;
4410 size = sys_fgetxattr(_fd, key, value_buf, XATTR_VAL_BUF_SIZE - 1);
4411 if (size >= 0) {
4412 have_val = _gf_true;
4413 } else {
4414 if (errno == ERANGE) {
4415 gf_msg(this->name, GF_LOG_INFO, errno, P_MSG_XATTR_FAILED,
4416 "fgetxattr failed due to overflow of buffer"
4417 " on fd %p: for the key %s ",
4418 fd, key);
4419 size = sys_fgetxattr(_fd, key, NULL, 0);
4420 }
4421 if (size == -1) {
4422 op_errno = errno;
4423 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_XATTR_FAILED,
4424 "fgetxattr failed "
4425 "on fd %p for the key %s ",
4426 fd, key);
4427 break;
4428 }
4429 }
4430 value = GF_MALLOC(size + 1, gf_posix_mt_char);
4431 if (!value) {
4432 op_ret = -1;
4433 op_errno = errno;
4434 goto out;
4435 }
4436 if (have_val) {
4437 memcpy(value, value_buf, size);
4438 } else {
4439 bzero(value, size + 1);
4440 size = sys_fgetxattr(_fd, key, value, size);
4441 if (size == -1) {
4442 op_errno = errno;
4443 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_XATTR_FAILED,
4444 "fgetxattr failed o"
4445 "n the fd %p for the key %s ",
4446 fd, key);
4447 GF_FREE(value);
4448 break;
4449 }
4450 }
4451 value[size] = '\0';
4452
4453 op_ret = dict_set_dynptr(dict, key, value, size);
4454 if (op_ret) {
4455 op_errno = -op_ret;
4456 op_ret = -1;
4457 gf_msg(this->name, GF_LOG_ERROR, 0, P_MSG_DICT_SET_FAILED,
4458 "dict set operation "
4459 "failed on key %s",
4460 key);
4461 GF_FREE(value);
4462 goto out;
4463 }
4464 remaining_size -= key_len + 1;
4465 list_offset += key_len + 1;
4466
4467 } /* while (remaining_size > 0) */
4468
4469 done:
4470 op_ret = size;
4471
4472 if (xdata && (op_ret >= 0)) {
4473 xattr_rsp = posix_xattr_fill(this, NULL, NULL, fd, pfd->fd, xdata,
4474 &buf);
4475 }
4476
4477 if (dict) {
4478 dict_del(dict, GFID_XATTR_KEY);
4479 dict_del(dict, GF_XATTR_VOL_ID_KEY);
4480 }
4481
4482 out:
4483 SET_TO_OLD_FS_ID();
4484
4485 STACK_UNWIND_STRICT(fgetxattr, frame, op_ret, op_errno, dict, xattr_rsp);
4486
4487 if (xattr_rsp)
4488 dict_unref(xattr_rsp);
4489
4490 if (dict)
4491 dict_unref(dict);
4492
4493 GF_FREE(list);
4494
4495 return 0;
4496 }
4497
4498 static int
4499 _handle_fsetxattr_keyvalue_pair(dict_t *d, char *k, data_t *v, void *tmp)
4500 {
4501 posix_xattr_filler_t *filler = NULL;
4502
4503 filler = tmp;
4504
4505 return posix_fhandle_pair(filler->frame, filler->this, filler->fdnum, k, v,
4506 filler->flags, filler->stbuf, filler->fd);
4507 }
4508
4509 int32_t
4510 posix_fsetxattr(call_frame_t *frame, xlator_t *this, fd_t *fd, dict_t *dict,
4511 int flags, dict_t *xdata)
4512 {
4513 int32_t op_ret = -1;
4514 int32_t op_errno = 0;
4515 struct posix_fd *pfd = NULL;
4516 int _fd = -1;
4517 int ret = -1;
4518 struct iatt preop = {
4519 0,
4520 };
4521 struct iatt postop = {
4522 0,
4523 };
4524 dict_t *xattr = NULL;
4525 posix_xattr_filler_t filler = {
4526 0,
4527 };
4528 struct posix_private *priv = NULL;
4529
4530 DECLARE_OLD_FS_ID_VAR;
4531 SET_FS_ID(frame->root->uid, frame->root->gid);
4532
4533 VALIDATE_OR_GOTO(frame, out);
4534 VALIDATE_OR_GOTO(this, out);
4535 VALIDATE_OR_GOTO(fd, out);
4536 VALIDATE_OR_GOTO(dict, out);
4537
4538 priv = this->private;
4539 DISK_SPACE_CHECK_AND_GOTO(frame, priv, xdata, op_ret, op_errno, out);
4540
4541 ret = posix_fd_ctx_get(fd, this, &pfd, &op_errno);
4542 if (ret < 0) {
4543 gf_msg(this->name, GF_LOG_WARNING, op_errno, P_MSG_PFD_NULL,
4544 "pfd is NULL from fd=%p", fd);
4545 goto out;
4546 }
4547 _fd = pfd->fd;
4548
4549 ret = posix_fdstat(this, fd->inode, pfd->fd, &preop, _gf_true);
4550 if (ret == -1) {
4551 op_errno = errno;
4552 gf_msg(this->name, GF_LOG_ERROR, op_errno, P_MSG_FSTAT_FAILED,
4553 "fsetxattr (fstat)"
4554 "failed on fd=%p",
4555 fd);
4556 goto out;
4557 }
4558
4559 dict_del(dict, GFID_XATTR_KEY);
4560 dict_del(dict, GF_XATTR_VOL_ID_KEY);
4561
4562 filler.fdnum = _fd;
4563 filler.this = this;
4564 filler.frame = frame;
4565 filler.stbuf = &preop;
4566 filler.fd = fd;
4567 #ifdef GF_DARWIN_HOST_OS
4568 filler.flags = map_xattr_flags(flags);
4569 #else
4570 filler.flags = flags;
4571 #endif
4572 op_ret = dict_foreach(dict, _handle_fsetxattr_keyvalue_pair, &filler);
4573 if (op_ret < 0) {
4574 op_errno = -op_ret;
4575 op_ret = -1;
4576 }
4577
4578 if (!ret && xdata && dict_get(xdata, GLUSTERFS_DURABLE_OP)) {
4579 op_ret = sys_fsync(_fd);
4580 if (op_ret < 0) {
4581 op_ret = -1;
4582 op_errno = errno;
4583 gf_msg(this->name, GF_LOG_WARNING, errno,
4584 P_MSG_DURABILITY_REQ_NOT_SATISFIED,
4585 "could not satisfy durability request: "
4586 "reason ");
4587 }
4588 }
4589
4590 ret = posix_fdstat(this, fd->inode, pfd->fd, &postop, _gf_true);
4591 if (ret == -1) {
4592 op_errno = errno;
4593 gf_msg(this->name, GF_LOG_ERROR, op_errno, P_MSG_XATTR_FAILED,
4594 "fsetxattr (fstat)"
4595 "failed on fd=%p",
4596 fd);
4597 goto out;
4598 }
4599 xattr = dict_new();
4600 if (!xattr)
4601 goto out;
4602
4603 ret = posix_set_iatt_in_dict(xattr, &preop, &postop);
4604
4605 out:
4606 SET_TO_OLD_FS_ID();
4607
4608 STACK_UNWIND_STRICT(fsetxattr, frame, op_ret, op_errno, xattr);
4609
4610 if (xattr)
4611 dict_unref(xattr);
4612
4613 return 0;
4614 }
4615
4616 int
4617 _posix_remove_xattr(dict_t *dict, char *key, data_t *value, void *data)
4618 {
4619 int32_t op_ret = 0;
4620 xlator_t *this = NULL;
4621 posix_xattr_filler_t *filler = NULL;
4622
4623 filler = (posix_xattr_filler_t *)data;
4624 this = filler->this;
4625 #ifdef GF_DARWIN_HOST_OS
4626 struct posix_private *priv = NULL;
4627 priv = (struct posix_private *)this->private;
4628 char *newkey = NULL;
4629 if (priv->xattr_user_namespace == XATTR_STRIP) {
4630 gf_remove_prefix(XATTR_USER_PREFIX, key, &newkey);
4631 gf_msg_debug("remove_xattr", 0, "key %s => %s", key, newkey);
4632 key = newkey;
4633 }
4634 #endif
4635 /* Bulk remove xattr is internal fop in gluster. Some of the xattrs may
4636 * have special behavior. Ex: removexattr("posix.system_acl_access"),
4637 * removes more than one xattr on the file that could be present in the
4638 * bulk-removal request. Removexattr of these deleted xattrs will fail
4639 * with either ENODATA/ENOATTR. Since all this fop cares is removal of the
4640 * xattrs in bulk-remove request and if they are already deleted, it can be
4641 * treated as success.
4642 */
4643
4644 if (filler->real_path)
4645 op_ret = sys_lremovexattr(filler->real_path, key);
4646 else
4647 op_ret = sys_fremovexattr(filler->fdnum, key);
4648
4649 if (op_ret == -1) {
4650 if (errno == ENODATA || errno == ENOATTR)
4651 op_ret = 0;
4652 }
4653
4654 if (op_ret == -1) {
4655 filler->op_errno = errno;
4656 if (errno != ENOATTR && errno != ENODATA && errno != EPERM) {
4657 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_XATTR_FAILED,
4658 "removexattr failed on "
4659 "file/dir %s with gfid: %s (for %s)",
4660 filler->real_path ? filler->real_path : "",
4661 uuid_utoa(filler->inode->gfid), key);
4662 }
4663 }
4664 #ifdef GF_DARWIN_HOST_OS
4665 GF_FREE(newkey);
4666 #endif
4667 return op_ret;
4668 }
4669
4670 int
4671 posix_common_removexattr(call_frame_t *frame, loc_t *loc, fd_t *fd,
4672 const char *name, dict_t *xdata, int *op_errno,
4673 dict_t **xdata_rsp)
4674 {
4675 gf_boolean_t bulk_removexattr = _gf_false;
4676 gf_boolean_t disallow = _gf_false;
4677 char *real_path = NULL;
4678 struct posix_fd *pfd = NULL;
4679 int op_ret = 0;
4680 struct iatt preop = {
4681 0,
4682 };
4683 struct iatt postop = {
4684 0,
4685 };
4686 int ret = 0;
4687 int _fd = -1;
4688 xlator_t *this = frame->this;
4689 inode_t *inode = NULL;
4690 posix_xattr_filler_t filler = {0};
4691
4692 DECLARE_OLD_FS_ID_VAR;
4693
4694 SET_FS_ID(frame->root->uid, frame->root->gid);
4695
4696 if (loc) {
4697 MAKE_INODE_HANDLE(real_path, this, loc, NULL);
4698 if (!real_path) {
4699 op_ret = -1;
4700 *op_errno = ESTALE;
4701 goto out;
4702 }
4703 inode = loc->inode;
4704 } else {
4705 op_ret = posix_fd_ctx_get(fd, this, &pfd, op_errno);
4706 if (op_ret < 0) {
4707 gf_msg(this->name, GF_LOG_WARNING, *op_errno, P_MSG_PFD_NULL,
4708 "pfd is NULL from fd=%p", fd);
4709 goto out;
4710 }
4711 _fd = pfd->fd;
4712 inode = fd->inode;
4713 }
4714
4715 if (posix_is_gfid2path_xattr(name)) {
4716 op_ret = -1;
4717 *op_errno = ENOATTR;
4718 goto out;
4719 }
4720
4721 if (loc) {
4722 ret = posix_pstat(this, inode, loc->gfid, real_path, &preop, _gf_false,
4723 _gf_true);
4724 if (ret) {
4725 gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_PSTAT_FAILED,
4726 "pstat operaton failed on %s", real_path);
4727 }
4728 } else {
4729 ret = posix_fdstat(this, inode, _fd, &preop, _gf_true);
4730 if (ret) {
4731 gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_FDSTAT_FAILED,
4732 "fdstat operaton failed on %s", real_path ? real_path : "");
4733 }
4734 }
4735
4736 if (gf_get_index_by_elem(disallow_removexattrs, (char *)name) >= 0) {
4737 gf_msg(this->name, GF_LOG_WARNING, 0, P_MSG_XATTR_NOT_REMOVED,
4738 "Remove xattr called on %s for file/dir %s with gfid: "
4739 "%s",
4740 name, real_path ? real_path : "", uuid_utoa(inode->gfid));
4741 op_ret = -1;
4742 *op_errno = EPERM;
4743 goto out;
4744 } else if (posix_is_bulk_removexattr((char *)name, xdata)) {
4745 bulk_removexattr = _gf_true;
4746 (void)dict_has_key_from_array(xdata, disallow_removexattrs, &disallow);
4747 if (disallow) {
4748 gf_msg(this->name, GF_LOG_WARNING, 0, P_MSG_XATTR_NOT_REMOVED,
4749 "Bulk removexattr has keys that shouldn't be "
4750 "removed for file/dir %s with gfid: %s",
4751 real_path ? real_path : "", uuid_utoa(inode->gfid));
4752 op_ret = -1;
4753 *op_errno = EPERM;
4754 goto out;
4755 }
4756 }
4757
4758 if (bulk_removexattr) {
4759 filler.real_path = real_path;
4760 filler.this = this;
4761 filler.fdnum = _fd;
4762 filler.inode = inode;
4763 op_ret = dict_foreach(xdata, _posix_remove_xattr, &filler);
4764 if (op_ret) {
4765 *op_errno = filler.op_errno;
4766 goto out;
4767 }
4768 } else {
4769 if (loc)
4770 op_ret = sys_lremovexattr(real_path, name);
4771 else
4772 op_ret = sys_fremovexattr(_fd, name);
4773 if (op_ret == -1) {
4774 *op_errno = errno;
4775 if (*op_errno != ENOATTR && *op_errno != ENODATA &&
4776 *op_errno != EPERM) {
4777 gf_msg(this->name, GF_LOG_ERROR, *op_errno, P_MSG_XATTR_FAILED,
4778 "removexattr on %s with gfid %s "
4779 "(for %s)",
4780 real_path, uuid_utoa(inode->gfid), name);
4781 }
4782 goto out;
4783 }
4784 }
4785
4786 if (loc) {
4787 posix_set_ctime(frame, this, real_path, -1, inode, NULL);
4788 ret = posix_pstat(this, inode, loc->gfid, real_path, &postop, _gf_false,
4789 _gf_true);
4790 if (ret) {
4791 gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_PSTAT_FAILED,
4792 "pstat operaton failed on %s", real_path);
4793 }
4794 } else {
4795 posix_set_ctime(frame, this, NULL, _fd, inode, NULL);
4796 ret = posix_fdstat(this, inode, _fd, &postop, _gf_true);
4797 if (ret) {
4798 gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_FDSTAT_FAILED,
4799 "fdstat operaton failed on %s", real_path);
4800 }
4801 }
4802 if (ret)
4803 goto out;
4804 *xdata_rsp = dict_new();
4805 if (!*xdata_rsp)
4806 goto out;
4807
4808 ret = posix_set_iatt_in_dict(*xdata_rsp, &preop, &postop);
4809
4810 op_ret = 0;
4811 out:
4812 SET_TO_OLD_FS_ID();
4813 return op_ret;
4814 }
4815
4816 int32_t
4817 posix_removexattr(call_frame_t *frame, xlator_t *this, loc_t *loc,
4818 const char *name, dict_t *xdata)
4819 {
4820 int op_ret = -1;
4821 int op_errno = EINVAL;
4822 dict_t *xdata_rsp = NULL;
4823
4824 VALIDATE_OR_GOTO(loc, out);
4825
4826 op_ret = posix_common_removexattr(frame, loc, NULL, name, xdata, &op_errno,
4827 &xdata_rsp);
4828 out:
4829 STACK_UNWIND_STRICT(removexattr, frame, op_ret, op_errno, xdata_rsp);
4830
4831 if (xdata_rsp)
4832 dict_unref(xdata_rsp);
4833
4834 return 0;
4835 }
4836
4837 int32_t
4838 posix_fremovexattr(call_frame_t *frame, xlator_t *this, fd_t *fd,
4839 const char *name, dict_t *xdata)
4840 {
4841 int32_t op_ret = -1;
4842 int32_t op_errno = EINVAL;
4843 dict_t *xdata_rsp = NULL;
4844
4845 VALIDATE_OR_GOTO(fd, out);
4846
4847 op_ret = posix_common_removexattr(frame, NULL, fd, name, xdata, &op_errno,
4848 &xdata_rsp);
4849 out:
4850 STACK_UNWIND_STRICT(fremovexattr, frame, op_ret, op_errno, xdata_rsp);
4851
4852 if (xdata_rsp)
4853 dict_unref(xdata_rsp);
4854
4855 return 0;
4856 }
4857
4858 int32_t
4859 posix_fsyncdir(call_frame_t *frame, xlator_t *this, fd_t *fd, int datasync,
4860 dict_t *xdata)
4861 {
4862 int32_t op_ret = -1;
4863 int32_t op_errno = 0;
4864 int ret = -1;
4865 struct posix_fd *pfd = NULL;
4866
4867 VALIDATE_OR_GOTO(frame, out);
4868 VALIDATE_OR_GOTO(this, out);
4869 VALIDATE_OR_GOTO(fd, out);
4870
4871 ret = posix_fd_ctx_get(fd, this, &pfd, &op_errno);
4872 if (ret < 0) {
4873 gf_msg(this->name, GF_LOG_WARNING, op_errno, P_MSG_PFD_NULL,
4874 "pfd is NULL, fd=%p", fd);
4875 goto out;
4876 }
4877
4878 op_ret = 0;
4879
4880 out:
4881 STACK_UNWIND_STRICT(fsyncdir, frame, op_ret, op_errno, NULL);
4882
4883 return 0;
4884 }
4885
4886 void
4887 posix_print_xattr(dict_t *this, char *key, data_t *value, void *data)
4888 {
4889 gf_msg_debug("posix", 0, "(key/val) = (%s/%d)", key, data_to_int32(value));
4890 }
4891
4892 /**
4893 * add_array - add two arrays of 32-bit numbers (stored in network byte order)
4894 * dest = dest + src
4895 * @count: number of 32-bit numbers
4896 * FIXME: handle overflow
4897 */
4898
4899 static void
4900 __add_array(int32_t *dest, int32_t *src, int count)
4901 {
4902 int i = 0;
4903 int32_t destval = 0;
4904 for (i = 0; i < count; i++) {
4905 destval = be32toh(dest[i]);
4906 dest[i] = htobe32(destval + be32toh(src[i]));
4907 }
4908 }
4909
4910 static void
4911 __add_long_array(int64_t *dest, int64_t *src, int count)
4912 {
4913 int i = 0;
4914 for (i = 0; i < count; i++) {
4915 dest[i] = htobe64(be64toh(dest[i]) + be64toh(src[i]));
4916 }
4917 }
4918
4919 /* functions:
4920 __add_array_with_default
4921 __add_long_array_with_default
4922
4923 xattrop type:
4924 GF_XATTROP_ADD_ARRAY_WITH_DEFAULT
4925 GF_XATTROP_ADD_ARRAY64_WITH_DEFAULT
4926
4927 These operations are similar to 'GF_XATTROP_ADD_ARRAY',
4928 except that it adds a default value if xattr is missing
4929 or its value is zero on disk.
4930
4931 One use-case of this operation is in inode-quota.
4932 When a new directory is created, its default dir_count
4933 should be set to 1. So when a xattrop performed setting
4934 inode-xattrs, it should account initial dir_count
4935 1 if the xattrs are not present
4936
4937 Here is the usage of this operation
4938
4939 value required in xdata for each key
4940 struct array {
4941 int32_t newvalue_1;
4942 int32_t newvalue_2;
4943 ...
4944 int32_t newvalue_n;
4945 int32_t default_1;
4946 int32_t default_2;
4947 ...
4948 int32_t default_n;
4949 };
4950
4951 or
4952
4953 struct array {
4954 int32_t value_1;
4955 int32_t value_2;
4956 ...
4957 int32_t value_n;
4958 } data[2];
4959 fill data[0] with new value to add
4960 fill data[1] with default value
4961
4962 xattrop GF_XATTROP_ADD_ARRAY_WITH_DEFAULT
4963 for i from 1 to n
4964 {
4965 if (xattr (dest_i) is zero or not set in the disk)
4966 dest_i = newvalue_i + default_i
4967 else
4968 dest_i = dest_i + newvalue_i
4969 }
4970
4971 value in xdata after xattrop is successful
4972 struct array {
4973 int32_t dest_1;
4974 int32_t dest_2;
4975 ...
4976 int32_t dest_n;
4977 };
4978 */
4979 static void
4980 __add_array_with_default(int32_t *dest, int32_t *src, int count)
4981 {
4982 int i = 0;
4983 int32_t destval = 0;
4984
4985 for (i = 0; i < count; i++) {
4986 destval = be32toh(dest[i]);
4987 if (destval == 0)
4988 dest[i] = htobe32(be32toh(src[i]) + be32toh(src[count + i]));
4989 else
4990 dest[i] = htobe32(destval + be32toh(src[i]));
4991 }
4992 }
4993
4994 static void
4995 __add_long_array_with_default(int64_t *dest, int64_t *src, int count)
4996 {
4997 int i = 0;
4998 int64_t destval = 0;
4999
5000 for (i = 0; i < count; i++) {
5001 destval = be64toh(dest[i]);
5002 if (destval == 0)
5003 dest[i] = htobe64(be64toh(src[i]) + be64toh(src[i + count]));
5004 else
5005 dest[i] = htobe64(destval + be64toh(src[i]));
5006 }
5007 }
5008
5009 static int
5010 _posix_handle_xattr_keyvalue_pair(dict_t *d, char *k, data_t *v, void *tmp)
5011 {
5012 int size = 0;
5013 int count = 0;
5014 int op_ret = 0;
5015 int op_errno = 0;
5016 gf_xattrop_flags_t optype = 0;
5017 char *array = NULL;
5018 char *dst_data = NULL;
5019 inode_t *inode = NULL;
5020 xlator_t *this = NULL;
5021 posix_xattr_filler_t *filler = NULL;
5022 posix_inode_ctx_t *ctx = NULL;
5023
5024 filler = tmp;
5025
5026 optype = (gf_xattrop_flags_t)(filler->flags);
5027 this = filler->this;
5028 inode = filler->inode;
5029 count = v->len;
5030 if (optype == GF_XATTROP_ADD_ARRAY_WITH_DEFAULT ||
5031 optype == GF_XATTROP_ADD_ARRAY64_WITH_DEFAULT)
5032 count = count / 2;
5033
5034 array = GF_CALLOC(count, sizeof(char), gf_posix_mt_char);
5035
5036 #ifdef GF_DARWIN_HOST_OS
5037 struct posix_private *priv = NULL;
5038 priv = this->private;
5039 if (priv->xattr_user_namespace == XATTR_STRIP) {
5040 if (strncmp(k, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) == 0) {
5041 k += XATTR_USER_PREFIX_LEN;
5042 }
5043 }
5044 #endif
5045 op_ret = posix_inode_ctx_get_all(inode, this, &ctx);
5046 if (op_ret < 0) {
5047 op_errno = ENOMEM;
5048 goto out;
5049 }
5050
5051 pthread_mutex_lock(&ctx->xattrop_lock);
5052 {
5053 if (filler->real_path) {
5054 size = sys_lgetxattr(filler->real_path, k, (char *)array, count);
5055 } else {
5056 size = sys_fgetxattr(filler->fdnum, k, (char *)array, count);
5057 }
5058 if (size == -1) {
5059 op_errno = errno;
5060 if ((op_errno != ENODATA) && (op_errno != ENOATTR)) {
5061 if (op_errno == ENOTSUP) {
5062 GF_LOG_OCCASIONALLY(gf_posix_xattr_enotsup_log, this->name,
5063 GF_LOG_WARNING,
5064 "Extended attributes not "
5065 "supported by filesystem");
5066 } else if (op_errno != ENOENT ||
5067 !posix_special_xattr(marker_xattrs, k)) {
5068 if (filler->real_path)
5069 gf_msg(this->name,
5070 fop_log_level(GF_FOP_XATTROP, op_errno),
5071 op_errno, P_MSG_XATTR_FAILED,
5072 "getxattr failed on %s while "
5073 "doing xattrop: Key:%s ",
5074 filler->real_path, k);
5075 else
5076 gf_msg(this->name, GF_LOG_ERROR, op_errno,
5077 P_MSG_XATTR_FAILED,
5078 "fgetxattr failed on gfid=%s "
5079 "while doing xattrop: "
5080 "Key:%s (%s)",
5081 uuid_utoa(filler->inode->gfid), k,
5082 strerror(op_errno));
5083 }
5084
5085 op_ret = -1;
5086 goto unlock;
5087 }
5088
5089 if (optype == GF_XATTROP_GET_AND_SET) {
5090 GF_FREE(array);
5091 array = NULL;
5092 }
5093 }
5094 /* We only write back the xattr if it has been really modified
5095 * (i.e. v->data is not all 0's). Otherwise we return its value
5096 * but we don't update anything.
5097 *
5098 * If the xattr does not exist, a value of all 0's is returned
5099 * without creating it. */
5100 size = count;
5101 if (optype != GF_XATTROP_GET_AND_SET &&
5102 mem_0filled(v->data, v->len) == 0)
5103 goto unlock;
5104
5105 dst_data = array;
5106 switch (optype) {
5107 case GF_XATTROP_ADD_ARRAY:
5108 __add_array((int32_t *)array, (int32_t *)v->data, count / 4);
5109 break;
5110
5111 case GF_XATTROP_ADD_ARRAY64:
5112 __add_long_array((int64_t *)array, (int64_t *)v->data,
5113 count / 8);
5114 break;
5115
5116 case GF_XATTROP_ADD_ARRAY_WITH_DEFAULT:
5117 __add_array_with_default((int32_t *)array, (int32_t *)v->data,
5118 count / 4);
5119 break;
5120
5121 case GF_XATTROP_ADD_ARRAY64_WITH_DEFAULT:
5122 __add_long_array_with_default((int64_t *)array,
5123 (int64_t *)v->data, count / 8);
5124 break;
5125
5126 case GF_XATTROP_GET_AND_SET:
5127 dst_data = v->data;
5128 break;
5129
5130 default:
5131 gf_msg(this->name, GF_LOG_ERROR, EINVAL, P_MSG_UNKNOWN_OP,
5132 "Unknown xattrop type (%d)"
5133 " on %s. Please send a bug report to "
5134 "[email protected]",
5135 optype, filler->real_path);
5136 op_ret = -1;
5137 op_errno = EINVAL;
5138 goto unlock;
5139 }
5140
5141 if (filler->real_path) {
5142 size = sys_lsetxattr(filler->real_path, k, dst_data, count, 0);
5143 } else {
5144 size = sys_fsetxattr(filler->fdnum, k, (char *)dst_data, count, 0);
5145 }
5146 if (size == -1)
5147 op_errno = errno;
5148 }
5149 unlock:
5150 pthread_mutex_unlock(&ctx->xattrop_lock);
5151
5152 if (op_ret == -1)
5153 goto out;
5154
5155 if (size == -1) {
5156 if (filler->real_path)
5157 gf_msg(this->name, GF_LOG_ERROR, op_errno, P_MSG_XATTR_FAILED,
5158 "setxattr failed on %s "
5159 "while doing xattrop: key=%s",
5160 filler->real_path, k);
5161 else
5162 gf_msg(this->name, GF_LOG_ERROR, op_errno, P_MSG_XATTR_FAILED,
5163 "fsetxattr failed on gfid=%s while doing "
5164 "xattrop: key=%s (%s)",
5165 uuid_utoa(filler->inode->gfid), k, strerror(op_errno));
5166 op_ret = -1;
5167 goto out;
5168 } else if (array) {
5169 op_ret = dict_set_bin(filler->xattr, k, array, count);
5170 if (op_ret) {
5171 if (filler->real_path)
5172 gf_msg_debug(this->name, -size,
5173 "dict_set_bin failed (path=%s): key=%s",
5174 filler->real_path, k);
5175 else
5176 gf_msg_debug(this->name, -size,
5177 "dict_set_bin failed (gfid=%s): key=%s",
5178 uuid_utoa(filler->inode->gfid), k);
5179
5180 op_ret = -1;
5181 op_errno = EINVAL;
5182 GF_FREE(array);
5183 array = NULL;
5184 goto out;
5185 }
5186 array = NULL;
5187 }
5188
5189 out:
5190 if (op_ret < 0)
5191 filler->op_errno = op_errno;
5192
5193 if (array)
5194 GF_FREE(array);
5195
5196 return op_ret;
5197 }
5198
5199 /**
5200 * xattrop - xattr operations - for internal use by GlusterFS
5201 * @optype: ADD_ARRAY:
5202 * dict should contain:
5203 * "key" ==> array of 32-bit numbers
5204 */
5205
5206 int
5207 do_xattrop(call_frame_t *frame, xlator_t *this, loc_t *loc, fd_t *fd,
5208 gf_xattrop_flags_t optype, dict_t *xattr, dict_t *xdata)
5209 {
5210 int op_ret = 0;
5211 int op_errno = 0;
5212 int _fd = -1;
5213 char *real_path = NULL;
5214 struct posix_fd *pfd = NULL;
5215 inode_t *inode = NULL;
5216 posix_xattr_filler_t filler = {
5217 0,
5218 };
5219 dict_t *xattr_rsp = NULL;
5220 dict_t *xdata_rsp = NULL;
5221 struct iatt stbuf = {0};
5222
5223 VALIDATE_OR_GOTO(frame, out);
5224 VALIDATE_OR_GOTO(xattr, out);
5225 VALIDATE_OR_GOTO(this, out);
5226
5227 if (fd) {
5228 op_ret = posix_fd_ctx_get(fd, this, &pfd, &op_errno);
5229 if (op_ret < 0) {
5230 gf_msg(this->name, GF_LOG_WARNING,
5231 fop_log_level(GF_FOP_FXATTROP, op_errno),
5232 P_MSG_PFD_GET_FAILED,
5233 "failed to get pfd from"
5234 " fd=%p",
5235 fd);
5236 goto out;
5237 }
5238 _fd = pfd->fd;
5239 }
5240
5241 if (loc && !gf_uuid_is_null(loc->gfid)) {
5242 MAKE_INODE_HANDLE(real_path, this, loc, NULL);
5243 if (!real_path) {
5244 op_ret = -1;
5245 op_errno = ESTALE;
5246 goto out;
5247 }
5248 }
5249
5250 if (real_path) {
5251 inode = loc->inode;
5252 } else if (fd) {
5253 inode = fd->inode;
5254 }
5255
5256 xattr_rsp = dict_new();
5257 if (xattr_rsp == NULL) {
5258 op_ret = -1;
5259 op_errno = ENOMEM;
5260 goto out;
5261 }
5262
5263 filler.this = this;
5264 filler.fdnum = _fd;
5265 filler.real_path = real_path;
5266 filler.flags = (int)optype;
5267 filler.inode = inode;
5268 filler.xattr = xattr_rsp;
5269
5270 op_ret = dict_foreach(xattr, _posix_handle_xattr_keyvalue_pair, &filler);
5271 op_errno = filler.op_errno;
5272 if (op_ret < 0)
5273 goto out;
5274
5275 if (!xdata)
5276 goto out;
5277
5278 if (fd) {
5279 op_ret = posix_fdstat(this, inode, _fd, &stbuf, _gf_false);
5280 } else {
5281 op_ret = posix_pstat(this, inode, inode->gfid, real_path, &stbuf,
5282 _gf_false, _gf_false);
5283 }
5284 if (op_ret < 0) {
5285 op_errno = errno;
5286 goto out;
5287 }
5288 xdata_rsp = posix_xattr_fill(this, real_path, loc, fd, _fd, xdata, &stbuf);
5289 if (!xdata_rsp) {
5290 op_ret = -1;
5291 op_errno = ENOMEM;
5292 }
5293 posix_set_mode_in_dict(xdata, xdata_rsp, &stbuf);
5294 out:
5295
5296 STACK_UNWIND_STRICT(xattrop, frame, op_ret, op_errno, xattr_rsp, xdata_rsp);
5297
5298 if (xattr_rsp)
5299 dict_unref(xattr_rsp);
5300
5301 if (xdata_rsp)
5302 dict_unref(xdata_rsp);
5303 return 0;
5304 }
5305
5306 int
5307 posix_xattrop(call_frame_t *frame, xlator_t *this, loc_t *loc,
5308 gf_xattrop_flags_t optype, dict_t *xattr, dict_t *xdata)
5309 {
5310 do_xattrop(frame, this, loc, NULL, optype, xattr, xdata);
5311 return 0;
5312 }
5313
5314 int
5315 posix_fxattrop(call_frame_t *frame, xlator_t *this, fd_t *fd,
5316 gf_xattrop_flags_t optype, dict_t *xattr, dict_t *xdata)
5317 {
5318 do_xattrop(frame, this, NULL, fd, optype, xattr, xdata);
5319 return 0;
5320 }
5321
5322 int
5323 posix_access(call_frame_t *frame, xlator_t *this, loc_t *loc, int32_t mask,
5324 dict_t *xdata)
5325 {
5326 int32_t op_ret = -1;
5327 int32_t op_errno = 0;
5328 char *real_path = NULL;
5329
5330 DECLARE_OLD_FS_ID_VAR;
5331 SET_FS_ID(frame->root->uid, frame->root->gid);
5332
5333 VALIDATE_OR_GOTO(frame, out);
5334 VALIDATE_OR_GOTO(this, out);
5335 VALIDATE_OR_GOTO(loc, out);
5336
5337 MAKE_INODE_HANDLE(real_path, this, loc, NULL);
5338 if (!real_path) {
5339 op_ret = -1;
5340 op_errno = errno;
5341 goto out;
5342 }
5343
5344 op_ret = sys_access(real_path, mask & 07);
5345 if (op_ret == -1) {
5346 op_errno = errno;
5347 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_ACCESS_FAILED,
5348 "access failed on %s", real_path);
5349 goto out;
5350 }
5351 op_ret = 0;
5352
5353 out:
5354 SET_TO_OLD_FS_ID();
5355
5356 STACK_UNWIND_STRICT(access, frame, op_ret, op_errno, NULL);
5357 return 0;
5358 }
5359
5360 int32_t
5361 posix_ftruncate(call_frame_t *frame, xlator_t *this, fd_t *fd, off_t offset,
5362 dict_t *xdata)
5363 {
5364 int32_t op_ret = -1;
5365 int32_t op_errno = 0;
5366 int _fd = -1;
5367 struct iatt preop = {
5368 0,
5369 };
5370 struct iatt postop = {
5371 0,
5372 };
5373 struct posix_fd *pfd = NULL;
5374 int ret = -1;
5375 struct posix_private *priv = NULL;
5376 dict_t *rsp_xdata = NULL;
5377 gf_boolean_t cs_obj_status, cs_obj_repair;
5378
5379 DECLARE_OLD_FS_ID_VAR;
5380 SET_FS_ID(frame->root->uid, frame->root->gid);
5381
5382 VALIDATE_OR_GOTO(frame, out);
5383 VALIDATE_OR_GOTO(this, out);
5384 VALIDATE_OR_GOTO(fd, out);
5385
5386 priv = this->private;
5387 VALIDATE_OR_GOTO(priv, out);
5388
5389 ret = posix_fd_ctx_get(fd, this, &pfd, &op_errno);
5390 if (ret < 0) {
5391 gf_msg(this->name, GF_LOG_WARNING, op_errno, P_MSG_PFD_NULL,
5392 "pfd is NULL, fd=%p", fd);
5393 goto out;
5394 }
5395
5396 _fd = pfd->fd;
5397
5398 op_ret = posix_fdstat(this, fd->inode, _fd, &preop, _gf_true);
5399 if (op_ret == -1) {
5400 op_errno = errno;
5401 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_FSTAT_FAILED,
5402 "pre-operation fstat failed on fd=%p", fd);
5403 goto out;
5404 }
5405
5406 if (xdata) {
5407 cs_obj_status = dict_get_sizen(xdata, GF_CS_OBJECT_STATUS);
5408 cs_obj_repair = dict_get_sizen(xdata, GF_CS_OBJECT_REPAIR);
5409 if (cs_obj_status || cs_obj_repair) {
5410 op_ret = posix_cs_maintenance(this, fd, NULL, &_fd, &preop, NULL,
5411 cs_obj_status, cs_obj_repair,
5412 &rsp_xdata, _gf_false);
5413 if (op_ret < 0) {
5414 gf_msg(this->name, GF_LOG_ERROR, 0, 0,
5415 "file state check failed, fd %p", fd);
5416 op_errno = EIO;
5417 goto out;
5418 }
5419 }
5420 if (cs_obj_status)
5421 posix_update_iatt_buf(&preop, _fd, NULL);
5422 }
5423
5424 op_ret = sys_ftruncate(_fd, offset);
5425
5426 if (op_ret == -1) {
5427 op_errno = errno;
5428 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_TRUNCATE_FAILED,
5429 "ftruncate failed on fd=%p (%" PRId64 "", fd, offset);
5430 goto out;
5431 }
5432
5433 op_ret = posix_fdstat(this, fd->inode, _fd, &postop, _gf_true);
5434 if (op_ret == -1) {
5435 op_errno = errno;
5436 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_FSTAT_FAILED,
5437 "post-operation fstat failed on fd=%p", fd);
5438 goto out;
5439 }
5440
5441 posix_set_ctime(frame, this, NULL, pfd->fd, fd->inode, &postop);
5442
5443 op_ret = 0;
5444
5445 out:
5446 SET_TO_OLD_FS_ID();
5447
5448 STACK_UNWIND_STRICT(ftruncate, frame, op_ret, op_errno, &preop, &postop,
5449 NULL);
5450
5451 return 0;
5452 }
5453
5454 int32_t
5455 posix_fstat(call_frame_t *frame, xlator_t *this, fd_t *fd, dict_t *xdata)
5456 {
5457 int _fd = -1;
5458 int32_t op_ret = -1;
5459 int32_t op_errno = 0;
5460 struct iatt buf = {
5461 0,
5462 };
5463 struct posix_fd *pfd = NULL;
5464 dict_t *xattr_rsp = NULL;
5465 int ret = -1;
5466 struct posix_private *priv = NULL;
5467 gf_boolean_t cs_obj_status, cs_obj_repair;
5468
5469 DECLARE_OLD_FS_ID_VAR;
5470 SET_FS_ID(frame->root->uid, frame->root->gid);
5471
5472 VALIDATE_OR_GOTO(frame, out);
5473 VALIDATE_OR_GOTO(this, out);
5474 VALIDATE_OR_GOTO(fd, out);
5475
5476 priv = this->private;
5477 VALIDATE_OR_GOTO(priv, out);
5478
5479 if (!xdata)
5480 gf_msg_trace(this->name, 0, "null xdata passed, fd %p", fd);
5481
5482 ret = posix_fd_ctx_get(fd, this, &pfd, &op_errno);
5483 if (ret < 0) {
5484 gf_msg(this->name, GF_LOG_WARNING, op_errno, P_MSG_PFD_NULL,
5485 "pfd is NULL, fd=%p", fd);
5486 goto out;
5487 }
5488
5489 _fd = pfd->fd;
5490
5491 op_ret = posix_fdstat(this, fd->inode, _fd, &buf, _gf_true);
5492 if (op_ret == -1) {
5493 op_errno = errno;
5494 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_FSTAT_FAILED,
5495 "fstat failed on fd=%p", fd);
5496 goto out;
5497 }
5498
5499 if (xdata) {
5500 xattr_rsp = posix_xattr_fill(this, NULL, NULL, fd, _fd, xdata, &buf);
5501
5502 cs_obj_status = dict_get_sizen(xdata, GF_CS_OBJECT_STATUS);
5503 cs_obj_repair = dict_get_sizen(xdata, GF_CS_OBJECT_REPAIR);
5504 if (cs_obj_status || cs_obj_repair) {
5505 op_ret = posix_cs_maintenance(this, fd, NULL, &_fd, &buf, NULL,
5506 cs_obj_status, cs_obj_repair,
5507 &xattr_rsp, _gf_false);
5508 if (op_ret < 0) {
5509 gf_msg(this->name, GF_LOG_ERROR, 0, 0,
5510 "file state check failed, fd %p", fd);
5511 }
5512 }
5513 if (cs_obj_status) {
5514 posix_cs_build_xattr_rsp(this, &xattr_rsp, xdata, _fd, NULL);
5515 posix_update_iatt_buf(&buf, _fd, NULL);
5516 }
5517 }
5518
5519 op_ret = 0;
5520
5521 out:
5522 SET_TO_OLD_FS_ID();
5523
5524 STACK_UNWIND_STRICT(fstat, frame, op_ret, op_errno, &buf, xattr_rsp);
5525 if (xattr_rsp)
5526 dict_unref(xattr_rsp);
5527 return 0;
5528 }
5529
5530 int32_t
5531 posix_lease(call_frame_t *frame, xlator_t *this, loc_t *loc,
5532 struct gf_lease *lease, dict_t *xdata)
5533 {
5534 struct gf_lease nullease = {
5535 0,
5536 };
5537
5538 gf_msg(this->name, GF_LOG_CRITICAL, EINVAL, P_MSG_LEASE_DISABLED,
5539 "\"features/leases\" translator is not loaded. You need"
5540 "to use it for proper functioning of your application");
5541
5542 STACK_UNWIND_STRICT(lease, frame, -1, ENOSYS, &nullease, NULL);
5543 return 0;
5544 }
5545
5546 static int gf_posix_lk_log;
5547
5548 int32_t
5549 posix_lk(call_frame_t *frame, xlator_t *this, fd_t *fd, int32_t cmd,
5550 struct gf_flock *lock, dict_t *xdata)
5551 {
5552 struct gf_flock nullock = {
5553 0,
5554 };
5555
5556 GF_LOG_OCCASIONALLY(gf_posix_lk_log, this->name, GF_LOG_CRITICAL,
5557 "\"features/locks\" translator is "
5558 "not loaded. You need to use it for proper "
5559 "functioning of your application.");
5560
5561 STACK_UNWIND_STRICT(lk, frame, -1, ENOSYS, &nullock, NULL);
5562 return 0;
5563 }
5564
5565 int32_t
5566 posix_inodelk(call_frame_t *frame, xlator_t *this, const char *volume,
5567 loc_t *loc, int32_t cmd, struct gf_flock *lock, dict_t *xdata)
5568 {
5569 GF_LOG_OCCASIONALLY(gf_posix_lk_log, this->name, GF_LOG_CRITICAL,
5570 "\"features/locks\" translator is "
5571 "not loaded. You need to use it for proper "
5572 "functioning of your application.");
5573
5574 STACK_UNWIND_STRICT(inodelk, frame, -1, ENOSYS, NULL);
5575 return 0;
5576 }
5577
5578 int32_t
5579 posix_finodelk(call_frame_t *frame, xlator_t *this, const char *volume,
5580 fd_t *fd, int32_t cmd, struct gf_flock *lock, dict_t *xdata)
5581 {
5582 GF_LOG_OCCASIONALLY(gf_posix_lk_log, this->name, GF_LOG_CRITICAL,
5583 "\"features/locks\" translator is "
5584 "not loaded. You need to use it for proper "
5585 "functioning of your application.");
5586
5587 STACK_UNWIND_STRICT(finodelk, frame, -1, ENOSYS, NULL);
5588 return 0;
5589 }
5590
5591 int32_t
5592 posix_entrylk(call_frame_t *frame, xlator_t *this, const char *volume,
5593 loc_t *loc, const char *basename, entrylk_cmd cmd,
5594 entrylk_type type, dict_t *xdata)
5595 {
5596 GF_LOG_OCCASIONALLY(gf_posix_lk_log, this->name, GF_LOG_CRITICAL,
5597 "\"features/locks\" translator is "
5598 "not loaded. You need to use it for proper "
5599 "functioning of your application.");
5600
5601 STACK_UNWIND_STRICT(entrylk, frame, -1, ENOSYS, NULL);
5602 return 0;
5603 }
5604
5605 int32_t
5606 posix_fentrylk(call_frame_t *frame, xlator_t *this, const char *volume,
5607 fd_t *fd, const char *basename, entrylk_cmd cmd,
5608 entrylk_type type, dict_t *xdata)
5609 {
5610 GF_LOG_OCCASIONALLY(gf_posix_lk_log, this->name, GF_LOG_CRITICAL,
5611 "\"features/locks\" translator is "
5612 "not loaded. You need to use it for proper "
5613 "functioning of your application.");
5614
5615 STACK_UNWIND_STRICT(fentrylk, frame, -1, ENOSYS, NULL);
5616 return 0;
5617 }
5618
5619 static int
5620 gf_d_type_from_st_mode(mode_t st_mode)
5621 {
5622 switch (st_mode & S_IFMT) {
5623 case S_IFREG:
5624 return DT_REG;
5625 case S_IFDIR:
5626 return DT_DIR;
5627 case S_IFLNK:
5628 return DT_LNK;
5629 case S_IFBLK:
5630 return DT_BLK;
5631 case S_IFCHR:
5632 return DT_CHR;
5633 case S_IFIFO:
5634 return DT_FIFO;
5635 case S_IFSOCK:
5636 return DT_SOCK;
5637 default:
5638 return DT_UNKNOWN;
5639 }
5640 return DT_UNKNOWN;
5641 }
5642
5643 static int
5644 posix_fill_readdir(fd_t *fd, struct posix_fd *pfd, off_t off, size_t size,
5645 gf_dirent_t *entries, xlator_t *this, int32_t skip_dirs)
5646 {
5647 off_t in_case = -1;
5648 off_t last_off = 0;
5649 size_t filled = 0;
5650 int count = 0;
5651 int32_t this_size = -1;
5652 gf_dirent_t *this_entry = NULL;
5653 struct stat stbuf = {
5654 0,
5655 };
5656 int stat_ret = 1;
5657 struct dirent *entry = NULL;
5658 struct dirent scratch[2] = {
5659 {
5660 0,
5661 },
5662 };
5663 size_t entry_dname_len;
5664 gf_boolean_t is_root_gfid = __is_root_gfid(fd->inode->gfid);
5665
5666 if (!off) {
5667 rewinddir(pfd->dir);
5668 } else {
5669 seekdir(pfd->dir, off);
5670 #ifndef GF_LINUX_HOST_OS
5671 if ((u_long)telldir(pfd->dir) != off && off != pfd->dir_eof) {
5672 gf_msg(THIS->name, GF_LOG_ERROR, EINVAL, P_MSG_DIR_OPERATION_FAILED,
5673 "seekdir(0x%llx) failed on dir=%p: "
5674 "Invalid argument (offset reused from "
5675 "another DIR * structure?)",
5676 off, pfd->dir);
5677 errno = EINVAL;
5678 count = -1;
5679 goto out;
5680 }
5681 #endif /* GF_LINUX_HOST_OS */
5682 }
5683
5684 while (filled <= size) {
5685 in_case = (u_long)telldir(pfd->dir);
5686
5687 if (in_case == -1) {
5688 gf_msg(THIS->name, GF_LOG_ERROR, errno, P_MSG_DIR_OPERATION_FAILED,
5689 "telldir failed on dir=%p", pfd->dir);
5690 goto out;
5691 }
5692
5693 errno = 0;
5694
5695 entry = sys_readdir(pfd->dir, scratch);
5696
5697 if (!entry || errno != 0) {
5698 if (errno == EBADF) {
5699 gf_msg(THIS->name, GF_LOG_WARNING, errno,
5700 P_MSG_DIR_OPERATION_FAILED, "readdir failed on dir=%p",
5701 pfd->dir);
5702 goto out;
5703 }
5704 break;
5705 }
5706 stat_ret = 1;
5707
5708 #ifdef __NetBSD__
5709 /*
5710 * NetBSD with UFS1 backend uses backing files for
5711 * extended attributes. They can be found in a
5712 * .attribute file located at the root of the filesystem
5713 * We hide it to glusterfs clients, since chaos will occur
5714 * when the cluster/dht xlator decides to distribute
5715 * exended attribute backing file across storage servers.
5716 */
5717 if (__is_root_gfid(fd->inode->gfid) == 0 &&
5718 (!strcmp(entry->d_name, ".attribute")))
5719 continue;
5720 #endif /* __NetBSD__ */
5721
5722 if (is_root_gfid &&
5723 (!strcmp(GF_HIDDEN_PATH, entry->d_name))) {
5724 continue;
5725 }
5726
5727 if (skip_dirs) {
5728 if (DT_ISDIR(entry->d_type)) {
5729 continue;
5730 } else if (DT_UNKNOWN == entry->d_type) {
5731 memset(&stbuf, 0, sizeof(stbuf));
5732 stat_ret = sys_fstatat(pfd->fd, entry->d_name, &stbuf,
5733 AT_SYMLINK_NOFOLLOW);
5734 if ((stat_ret == 0) && S_ISDIR(stbuf.st_mode))
5735 continue;
5736 }
5737 }
5738
5739 entry_dname_len = strlen(entry->d_name);
5740 this_size = max(sizeof(gf_dirent_t), sizeof(gfx_dirplist)) +
5741 entry_dname_len + 1;
5742
5743 if (this_size + filled > size) {
5744 seekdir(pfd->dir, in_case);
5745 #ifndef GF_LINUX_HOST_OS
5746 if ((u_long)telldir(pfd->dir) != in_case &&
5747 in_case != pfd->dir_eof) {
5748 gf_msg(THIS->name, GF_LOG_ERROR, EINVAL,
5749 P_MSG_DIR_OPERATION_FAILED,
5750 "seekdir(0x%llx) failed on dir=%p: "
5751 "Invalid argument (offset reused from "
5752 "another DIR * structure?)",
5753 in_case, pfd->dir);
5754 errno = EINVAL;
5755 count = -1;
5756 goto out;
5757 }
5758 #endif /* GF_LINUX_HOST_OS */
5759 break;
5760 }
5761
5762 if (DT_UNKNOWN == entry->d_type) {
5763 if (stat_ret == 1) {
5764 memset(&stbuf, 0, sizeof(stbuf));
5765 stat_ret = sys_fstatat(pfd->fd, entry->d_name, &stbuf,
5766 AT_SYMLINK_NOFOLLOW);
5767 }
5768
5769 if (stat_ret == 0) {
5770 entry->d_type = gf_d_type_from_st_mode(stbuf.st_mode);
5771 } else {
5772 gf_msg_debug(THIS->name, errno,
5773 "fstatat failed on"
5774 " %s/%s",
5775 uuid_utoa(fd->inode->gfid), entry->d_name);
5776 }
5777 }
5778 /*
5779 * we store the offset of next entry here, which is
5780 * probably not intended, but code using syncop_readdir()
5781 * (glfs-heal.c, afr-self-heald.c) rely on it
5782 * for directory read resumption.
5783 */
5784
5785 last_off = (u_long)telldir(pfd->dir);
5786
5787 this_entry = gf_dirent_for_name2(entry->d_name, entry_dname_len,
5788 entry->d_ino, last_off, entry->d_type,
5789 NULL);
5790
5791 if (!this_entry) {
5792 gf_msg(THIS->name, GF_LOG_ERROR, errno,
5793 P_MSG_GF_DIRENT_CREATE_FAILED,
5794 "could not create "
5795 "gf_dirent for entry %s",
5796 entry->d_name);
5797 goto out;
5798 }
5799
5800 list_add_tail(&this_entry->list, &entries->list);
5801
5802 filled += this_size;
5803 count++;
5804 }
5805
5806 if ((!sys_readdir(pfd->dir, scratch) && (errno == 0))) {
5807 /* Indicate EOF */
5808 errno = ENOENT;
5809 /* Remember EOF offset for later detection */
5810 pfd->dir_eof = (u_long)last_off;
5811 }
5812 out:
5813 return count;
5814 }
5815
5816 dict_t *
5817 posix_entry_xattr_fill(xlator_t *this, inode_t *inode, fd_t *fd,
5818 char *entry_path, dict_t *dict, struct iatt *stbuf)
5819 {
5820 loc_t tmp_loc = {
5821 0,
5822 };
5823
5824 /* if we don't send the 'loc', open-fd-count be a problem. */
5825 tmp_loc.inode = inode;
5826
5827 return posix_xattr_fill(this, entry_path, &tmp_loc, NULL, -1, dict, stbuf);
5828 }
5829
5830 int
5831 posix_readdirp_fill(xlator_t *this, fd_t *fd, gf_dirent_t *entries,
5832 dict_t *dict)
5833 {
5834 gf_dirent_t *entry = NULL;
5835 inode_table_t *itable = NULL;
5836 inode_t *inode = NULL;
5837 char *hpath = NULL;
5838 int len = 0;
5839 struct iatt stbuf = {
5840 0,
5841 };
5842 uuid_t gfid;
5843 int ret = -1;
5844 gf_boolean_t do_update_iatt_buf = _gf_false;
5845
5846 if (list_empty(&entries->list))
5847 return 0;
5848
5849 itable = fd->inode->table;
5850
5851 hpath = alloca(PATH_MAX);
5852 len = posix_handle_path(this, fd->inode->gfid, NULL, hpath, PATH_MAX);
5853 if (len <= 0) {
5854 gf_msg(this->name, GF_LOG_WARNING, 0, P_MSG_HANDLEPATH_FAILED,
5855 "Failed to create handle path, fd=%p, gfid=%s", fd,
5856 uuid_utoa(fd->inode->gfid));
5857 return -1;
5858 }
5859 len = strlen(hpath);
5860 hpath[len] = '/';
5861
5862 if (dict && (dict_get_sizen(dict, GF_CS_OBJECT_STATUS)))
5863 do_update_iatt_buf = _gf_true;
5864
5865 list_for_each_entry(entry, &entries->list, list)
5866 {
5867 inode = inode_grep(fd->inode->table, fd->inode, entry->d_name);
5868 if (inode)
5869 gf_uuid_copy(gfid, inode->gfid);
5870 else
5871 bzero(gfid, 16);
5872
5873 strcpy(&hpath[len + 1], entry->d_name);
5874
5875 ret = posix_pstat(this, inode, gfid, hpath, &stbuf, _gf_false,
5876 _gf_true);
5877
5878 if (ret == -1) {
5879 if (inode)
5880 inode_unref(inode);
5881 continue;
5882 }
5883
5884 if (do_update_iatt_buf)
5885 posix_update_iatt_buf(&stbuf, -1, hpath);
5886
5887 if (!inode)
5888 inode = inode_find(itable, stbuf.ia_gfid);
5889
5890 if (!inode)
5891 inode = inode_new(itable);
5892
5893 entry->inode = inode;
5894
5895 if (dict) {
5896 entry->dict = posix_entry_xattr_fill(this, entry->inode, fd, hpath,
5897 dict, &stbuf);
5898 }
5899
5900 entry->d_stat = stbuf;
5901 if (stbuf.ia_ino)
5902 entry->d_ino = stbuf.ia_ino;
5903
5904 if (entry->d_type == DT_UNKNOWN && !IA_ISINVAL(stbuf.ia_type)) {
5905 /* The platform supports d_type but the underlying
5906 filesystem doesn't. We set d_type to the correct
5907 value from ia_type */
5908 entry->d_type = gf_d_type_from_ia_type(stbuf.ia_type);
5909 }
5910
5911 inode = NULL;
5912 }
5913
5914 return 0;
5915 }
5916
5917 int32_t
5918 posix_do_readdir(call_frame_t *frame, xlator_t *this, fd_t *fd, size_t size,
5919 off_t off, int whichop, dict_t *dict)
5920 {
5921 struct posix_fd *pfd = NULL;
5922 int ret = -1;
5923 int count = 0;
5924 int32_t op_ret = -1;
5925 int32_t op_errno = 0;
5926 gf_dirent_t entries;
5927 int32_t skip_dirs = 0;
5928
5929 VALIDATE_OR_GOTO(frame, out);
5930 VALIDATE_OR_GOTO(this, out);
5931 VALIDATE_OR_GOTO(fd, out);
5932
5933 INIT_LIST_HEAD(&entries.list);
5934
5935 ret = posix_fd_ctx_get(fd, this, &pfd, &op_errno);
5936 if (ret < 0) {
5937 gf_msg(this->name, GF_LOG_WARNING, op_errno, P_MSG_PFD_NULL,
5938 "pfd is NULL, fd=%p", fd);
5939 goto out;
5940 }
5941
5942 if ((!pfd->dir) || (pfd->fd < 0)) {
5943 gf_msg(this->name, GF_LOG_WARNING, EINVAL, P_MSG_PFD_NULL,
5944 "posix ctx(%p, %d) is invalid for %s", pfd->dir, pfd->fd,
5945 uuid_utoa(fd->inode->gfid));
5946 op_errno = EINVAL;
5947 goto out;
5948 }
5949
5950 /* When READDIR_FILTER option is set to on, we can filter out
5951 * directory's entry from the entry->list.
5952 */
5953 ret = dict_get_int32_sizen(dict, GF_READDIR_SKIP_DIRS, &skip_dirs);
5954
5955 LOCK(&fd->lock);
5956 {
5957 /* posix_fill_readdir performs multiple separate individual
5958 readdir() calls to fill up the buffer.
5959
5960 In case of NFS where the same anonymous FD is shared between
5961 different applications, reading a common directory can
5962 result in the anonymous fd getting re-used unsafely between
5963 the two readdir requests (in two different io-threads).
5964
5965 It would also help, in the future, to replace the loop
5966 around readdir() with a single large getdents() call.
5967 */
5968 count = posix_fill_readdir(fd, pfd, off, size, &entries, this,
5969 skip_dirs);
5970 }
5971 UNLOCK(&fd->lock);
5972
5973 /* pick ENOENT to indicate EOF */
5974 op_errno = errno;
5975 op_ret = count;
5976
5977 if (whichop != GF_FOP_READDIRP)
5978 goto out;
5979
5980 posix_readdirp_fill(this, fd, &entries, dict);
5981
5982 out:
5983 if (whichop == GF_FOP_READDIR)
5984 STACK_UNWIND_STRICT(readdir, frame, op_ret, op_errno, &entries, NULL);
5985 else
5986 STACK_UNWIND_STRICT(readdirp, frame, op_ret, op_errno, &entries, NULL);
5987
5988 gf_dirent_free(&entries);
5989
5990 return 0;
5991 }
5992
5993 int32_t
5994 posix_readdir(call_frame_t *frame, xlator_t *this, fd_t *fd, size_t size,
5995 off_t off, dict_t *xdata)
5996 {
5997 posix_do_readdir(frame, this, fd, size, off, GF_FOP_READDIR, xdata);
5998 return 0;
5999 }
6000
6001 int32_t
6002 posix_readdirp(call_frame_t *frame, xlator_t *this, fd_t *fd, size_t size,
6003 off_t off, dict_t *dict)
6004 {
6005 gf_dirent_t entries;
6006 int32_t op_ret = -1, op_errno = 0;
6007 gf_dirent_t *entry = NULL;
6008
6009 if ((dict != NULL) && (dict_get(dict, GET_ANCESTRY_DENTRY_KEY))) {
6010 INIT_LIST_HEAD(&entries.list);
6011
6012 op_ret = posix_get_ancestry(this, fd->inode, &entries, NULL,
6013 POSIX_ANCESTRY_DENTRY, &op_errno, dict);
6014 if (op_ret >= 0) {
6015 op_ret = 0;
6016
6017 list_for_each_entry(entry, &entries.list, list)
6018 {
6019 op_ret++;
6020 }
6021 }
6022
6023 STACK_UNWIND_STRICT(readdirp, frame, op_ret, op_errno, &entries, NULL);
6024
6025 gf_dirent_free(&entries);
6026 return 0;
6027 }
6028
6029 posix_do_readdir(frame, this, fd, size, off, GF_FOP_READDIRP, dict);
6030 return 0;
6031 }
6032
6033 int32_t
6034 posix_rchecksum(call_frame_t *frame, xlator_t *this, fd_t *fd, off_t offset,
6035 int32_t len, dict_t *xdata)
6036 {
6037 char *alloc_buf = NULL;
6038 char *buf = NULL;
6039 int _fd = -1;
6040 struct posix_fd *pfd = NULL;
6041 int op_ret = -1;
6042 int op_errno = 0;
6043 int ret = 0;
6044 ssize_t bytes_read = 0;
6045 int32_t weak_checksum = 0;
6046 int32_t zerofillcheck = 0;
6047 /* Protocol version 4 uses 32 bytes i.e SHA256_DIGEST_LENGTH,
6048 so this is used. */
6049 unsigned char md5_checksum[SHA256_DIGEST_LENGTH] = {0};
6050 unsigned char strong_checksum[SHA256_DIGEST_LENGTH] = {0};
6051 unsigned char *checksum = NULL;
6052 struct posix_private *priv = NULL;
6053 dict_t *rsp_xdata = NULL;
6054 gf_boolean_t buf_has_zeroes = _gf_false;
6055 struct iatt preop = {
6056 0,
6057 };
6058 gf_boolean_t cs_obj_status, cs_obj_repair;
6059
6060 VALIDATE_OR_GOTO(frame, out);
6061 VALIDATE_OR_GOTO(this, out);
6062 VALIDATE_OR_GOTO(fd, out);
6063
6064 priv = this->private;
6065
6066 alloc_buf = _page_aligned_alloc(len, &buf, _gf_false);
6067 if (!alloc_buf) {
6068 op_errno = ENOMEM;
6069 goto out;
6070 }
6071
6072 rsp_xdata = dict_new();
6073 if (!rsp_xdata) {
6074 op_errno = ENOMEM;
6075 goto out;
6076 }
6077
6078 ret = posix_fd_ctx_get(fd, this, &pfd, &op_errno);
6079 if (ret < 0) {
6080 gf_msg(this->name, GF_LOG_WARNING, -ret, P_MSG_PFD_NULL,
6081 "pfd is NULL, fd=%p", fd);
6082 goto out;
6083 }
6084
6085 _fd = pfd->fd;
6086
6087 if (xdata) {
6088 cs_obj_status = dict_get_sizen(xdata, GF_CS_OBJECT_STATUS);
6089 cs_obj_repair = dict_get_sizen(xdata, GF_CS_OBJECT_REPAIR);
6090 if (cs_obj_status || cs_obj_repair) {
6091 op_ret = posix_fdstat(this, fd->inode, _fd, &preop, _gf_false);
6092 if (op_ret == -1) {
6093 op_errno = errno;
6094 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_FSTAT_FAILED,
6095 "pre-operation fstat failed on fd=%p", fd);
6096 goto out;
6097 }
6098
6099 op_ret = posix_cs_maintenance(this, fd, NULL, &_fd, &preop, NULL,
6100 cs_obj_status, cs_obj_repair,
6101 &rsp_xdata, _gf_false);
6102 if (op_ret < 0) {
6103 gf_msg(this->name, GF_LOG_ERROR, 0, 0,
6104 "file state check failed, fd %p", fd);
6105 op_errno = EIO;
6106 goto out;
6107 }
6108 }
6109 }
6110
6111 LOCK(&fd->lock);
6112 {
6113 if (priv->aio_capable && priv->aio_init_done)
6114 __posix_fd_set_odirect(
6115 fd, pfd, 0,
6116 (DIRECT_ALIGNED(buf, priv) && DIRECT_ALIGNED(offset, priv) &&
6117 DIRECT_ALIGNED(len, priv)));
6118
6119 bytes_read = sys_pread(_fd, buf, len, offset);
6120 if (bytes_read < 0) {
6121 gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_PREAD_FAILED,
6122 "pread of %d bytes returned %zd", len, bytes_read);
6123
6124 op_errno = errno;
6125 }
6126 }
6127 UNLOCK(&fd->lock);
6128
6129 if (bytes_read < 0)
6130 goto out;
6131
6132 if (xdata &&
6133 dict_get_int32(xdata, "check-zero-filled", &zerofillcheck) == 0) {
6134 buf_has_zeroes = (mem_0filled(buf, bytes_read)) ? _gf_false : _gf_true;
6135 ret = dict_set_uint32(rsp_xdata, "buf-has-zeroes", buf_has_zeroes);
6136 if (ret) {
6137 gf_msg(this->name, GF_LOG_WARNING, -ret, P_MSG_DICT_SET_FAILED,
6138 "%s: Failed to set "
6139 "dictionary value for key: %s",
6140 uuid_utoa(fd->inode->gfid), "buf-has-zeroes");
6141 op_errno = -ret;
6142 goto out;
6143 }
6144 }
6145 weak_checksum = gf_rsync_weak_checksum((unsigned char *)buf,
6146 (size_t)bytes_read);
6147
6148 if (priv->fips_mode_rchecksum) {
6149 ret = dict_set_int32(rsp_xdata, "fips-mode-rchecksum", 1);
6150 if (ret) {
6151 gf_msg(this->name, GF_LOG_WARNING, -ret, P_MSG_DICT_SET_FAILED,
6152 "%s: Failed to set "
6153 "dictionary value for key: %s",
6154 uuid_utoa(fd->inode->gfid), "fips-mode-rchecksum");
6155 goto out;
6156 }
6157 checksum = strong_checksum;
6158 gf_rsync_strong_checksum((unsigned char *)buf, (size_t)bytes_read,
6159 (unsigned char *)checksum);
6160 } else {
6161 checksum = md5_checksum;
6162 gf_rsync_md5_checksum((unsigned char *)buf, (size_t)bytes_read,
6163 (unsigned char *)checksum);
6164 }
6165 op_ret = 0;
6166
6167 posix_set_ctime(frame, this, NULL, _fd, fd->inode, NULL);
6168
6169 out:
6170 STACK_UNWIND_STRICT(rchecksum, frame, op_ret, op_errno, weak_checksum,
6171 checksum, rsp_xdata);
6172 if (rsp_xdata)
6173 dict_unref(rsp_xdata);
6174 GF_FREE(alloc_buf);
6175
6176 return 0;
6177 }
6178
6179 int
6180 posix_forget(xlator_t *this, inode_t *inode)
6181 {
6182 int ret = 0;
6183 char *unlink_path = NULL;
6184 uint64_t ctx_uint1 = 0;
6185 uint64_t ctx_uint2 = 0;
6186 posix_inode_ctx_t *ctx = NULL;
6187 struct posix_private *priv = this->private;
6188
6189 ret = inode_ctx_del2(inode, this, &ctx_uint1, &ctx_uint2);
6190
6191 if (ctx_uint2)
6192 GF_FREE((posix_mdata_t *)(uintptr_t)ctx_uint2);
6193
6194 if (!ctx_uint1)
6195 return 0;
6196
6197 ctx = (posix_inode_ctx_t *)(uintptr_t)ctx_uint1;
6198
6199 if (ctx->unlink_flag == GF_UNLINK_TRUE) {
6200 POSIX_GET_FILE_UNLINK_PATH(priv->base_path, inode->gfid, unlink_path);
6201 if (!unlink_path) {
6202 gf_msg(this->name, GF_LOG_ERROR, ENOMEM, P_MSG_UNLINK_FAILED,
6203 "Failed to remove gfid :%s", uuid_utoa(inode->gfid));
6204 ret = -1;
6205 } else {
6206 ret = sys_unlink(unlink_path);
6207 }
6208 }
6209
6210 pthread_mutex_destroy(&ctx->xattrop_lock);
6211 pthread_mutex_destroy(&ctx->write_atomic_lock);
6212 pthread_mutex_destroy(&ctx->pgfid_lock);
6213 GF_FREE(ctx);
6214
6215 return ret;
6216 }