Debian Codegraph @

xlators/storage/posix/src/posix-entry-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 <stdint.h>
18 #include <sys/time.h>
19 #include <errno.h>
20 #include <libgen.h>
21 #include <pthread.h>
22 #include <ftw.h>
23 #include <sys/stat.h>
24 #include <signal.h>
25 #include <sys/uio.h>
26 #include <unistd.h>
27
28 #ifndef GF_BSD_HOST_OS
29 #include <alloca.h>
30 #endif /* GF_BSD_HOST_OS */
31
32 #ifdef HAVE_LINKAT
33 #include <fcntl.h>
34 #endif /* HAVE_LINKAT */
35
36 #include <glusterfs/dict.h>
37 #include <glusterfs/logging.h>
38 #include "posix.h"
39 #include "posix-handle.h"
40 #include <glusterfs/compat-errno.h>
41 #include <glusterfs/compat.h>
42 #include <glusterfs/syscall.h>
43 #include <glusterfs/statedump.h>
44 #include <glusterfs/locking.h>
45 #include "posix-aio.h"
46 #include <glusterfs/glusterfs-acl.h>
47 #include "posix-messages.h"
48 #include "posix-metadata.h"
49 #include <glusterfs/events.h>
50 #include "posix-gfid-path.h"
51 #include <glusterfs/compat-uuid.h>
52 #include <glusterfs/syncop.h>
53
54 // extern char *marker_xattrs[];
55 #define ALIGN_SIZE 4096
56
57 #undef HAVE_SET_FSID
58 #ifdef HAVE_SET_FSID
59
60 #define DECLARE_OLD_FS_ID_VAR \
61 uid_t old_fsuid; \
62 gid_t old_fsgid;
63
64 #define SET_FS_ID(uid, gid) \
65 do { \
66 old_fsuid = setfsuid(uid); \
67 old_fsgid = setfsgid(gid); \
68 } while (0)
69
70 #define SET_TO_OLD_FS_ID() \
71 do { \
72 setfsuid(old_fsuid); \
73 setfsgid(old_fsgid); \
74 } while (0)
75
76 #else
77
78 #define DECLARE_OLD_FS_ID_VAR
79 #define SET_FS_ID(uid, gid)
80 #define SET_TO_OLD_FS_ID()
81
82 #endif
83
84 static int
85 posix_unlink_stale_linkto(call_frame_t *frame, xlator_t *this,
86 const char *real_path, int32_t *op_errno, loc_t *loc);
87 static gf_boolean_t
88 posix_symlinks_match(xlator_t *this, loc_t *loc, uuid_t gfid)
89 {
90 struct posix_private *priv = NULL;
91 char linkname_actual[PATH_MAX] = {
92 0,
93 };
94 char linkname_expected[PATH_MAX] = {0};
95 char *dir_handle = NULL;
96 ssize_t len = 0;
97 size_t handle_size = 0;
98 gf_boolean_t ret = _gf_false;
99
100 priv = this->private;
101 handle_size = POSIX_GFID_HANDLE_SIZE(priv->base_path_length);
102 dir_handle = alloca0(handle_size);
103
104 snprintf(linkname_expected, PATH_MAX, "../../%02x/%02x/%s/%s",
105 loc->pargfid[0], loc->pargfid[1], uuid_utoa(loc->pargfid),
106 loc->name);
107
108 MAKE_HANDLE_GFID_PATH(dir_handle, this, gfid);
109 len = sys_readlink(dir_handle, linkname_actual, PATH_MAX);
110 if (len < 0 || len == PATH_MAX) {
111 if (len == PATH_MAX) {
112 errno = EINVAL;
113 }
114
115 if (errno != ENOENT) {
116 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LSTAT_FAILED,
117 "readlink[%s] failed", dir_handle);
118 }
119 goto out;
120 }
121 linkname_actual[len] = '\0';
122
123 if (!strcmp(linkname_actual, linkname_expected))
124 ret = _gf_true;
125
126 out:
127 return ret;
128 }
129
130 static dict_t *
131 posix_dict_set_nlink(dict_t *req, dict_t *res, int32_t nlink)
132 {
133 int ret = -1;
134
135 if (req == NULL || !dict_get_sizen(req, GF_REQUEST_LINK_COUNT_XDATA))
136 goto out;
137
138 if (res == NULL)
139 res = dict_new();
140 if (res == NULL)
141 goto out;
142
143 ret = dict_set_uint32(res, GF_RESPONSE_LINK_COUNT_XDATA, nlink);
144 if (ret == -1)
145 gf_msg("posix", GF_LOG_WARNING, 0, P_MSG_SET_XDATA_FAIL,
146 "Failed to set GF_RESPONSE_LINK_COUNT_XDATA");
147 out:
148 return res;
149 }
150
151 /* Regular fops */
152
153 int32_t
154 posix_lookup(call_frame_t *frame, xlator_t *this, loc_t *loc, dict_t *xdata)
155 {
156 struct iatt buf = {
157 0,
158 };
159 int32_t op_ret = -1;
160 int32_t entry_ret = 0;
161 int32_t op_errno = 0;
162 dict_t *xattr = NULL;
163 char *real_path = NULL;
164 char *par_path = NULL;
165 char *gfid_path = NULL;
166 uuid_t gfid = {0};
167 struct iatt postparent = {
168 0,
169 };
170 struct stat statbuf = {0};
171 int32_t gfidless = 0;
172 char *pgfid_xattr_key = NULL;
173 int32_t nlink_samepgfid = 0;
174 struct posix_private *priv = NULL;
175 posix_inode_ctx_t *ctx = NULL;
176 int ret = 0;
177 int dfd = -1;
178 uint32_t lookup_unlink_dir = 0;
179 char *unlink_path = NULL;
180 struct stat lstatbuf = {
181 0,
182 };
183 gf_boolean_t cs_obj_status, cs_obj_repair;
184
185 VALIDATE_OR_GOTO(frame, out);
186 VALIDATE_OR_GOTO(this, out);
187 VALIDATE_OR_GOTO(loc, out);
188 VALIDATE_OR_GOTO(this->private, out);
189
190 priv = this->private;
191
192 /* The Hidden directory should be for housekeeping purpose and it
193 should not get any gfid on it */
194 if (__is_root_gfid(loc->pargfid) && loc->name &&
195 (strcmp(loc->name, GF_HIDDEN_PATH) == 0)) {
196 gf_msg(this->name, GF_LOG_WARNING, EPERM, P_MSG_LOOKUP_NOT_PERMITTED,
197 "Lookup issued on %s,"
198 " which is not permitted",
199 GF_HIDDEN_PATH);
200 op_errno = EPERM;
201 op_ret = -1;
202 goto out;
203 }
204
205 #ifdef __NetBSD__
206 /* Same for NetBSD's .attribute directory */
207 if (__is_root_gfid(loc->pargfid) && loc->name &&
208 (strcmp(loc->name, ".attribute") == 0)) {
209 gf_msg(this->name, GF_LOG_WARNING, EPERM, P_MSG_LOOKUP_NOT_PERMITTED,
210 "Lookup issued on .attribute,"
211 " which is not permitted");
212 op_errno = EPERM;
213 op_ret = -1;
214 goto out;
215 }
216 #endif /* __NetBSD__ */
217
218 op_ret = dict_get_int32_sizen(xdata, GF_GFIDLESS_LOOKUP, &gfidless);
219 op_ret = -1;
220 if (gf_uuid_is_null(loc->pargfid) || (loc->name == NULL)) {
221 /* nameless lookup */
222 op_ret = op_errno = errno = 0;
223 MAKE_INODE_HANDLE(real_path, this, loc, &buf);
224
225 /* The gfid will be renamed to ".glusterfs/unlink" in case
226 * there are any open fds on the file in posix_unlink path.
227 * So client can request server to do nameless lookup with
228 * xdata = GF_UNLINKED_LOOKUP in ".glusterfs/unlink"
229 * dir if a client wants to know the status of the all open fds
230 * on the unlinked file. If the file still present in the
231 * ".glusterfs/unlink" dir then it indicates there still
232 * open fds present on the file and the file is still under
233 * unlink process */
234 if (op_ret < 0 && errno == ENOENT) {
235 ret = dict_get_uint32(xdata, GF_UNLINKED_LOOKUP,
236 &lookup_unlink_dir);
237 if (!ret && lookup_unlink_dir) {
238 op_ret = op_errno = errno = 0;
239 POSIX_GET_FILE_UNLINK_PATH(priv->base_path, loc->gfid,
240 unlink_path);
241 ret = sys_lstat(unlink_path, &lstatbuf);
242 if (ret) {
243 op_ret = -1;
244 op_errno = errno;
245 } else {
246 iatt_from_stat(&buf, &lstatbuf);
247 buf.ia_nlink = 0;
248 }
249 goto nameless_lookup_unlink_dir_out;
250 }
251 }
252 } else {
253 MAKE_ENTRY_HANDLE(real_path, par_path, this, loc, &buf);
254 if (!real_path || !par_path) {
255 op_ret = -1;
256 op_errno = ESTALE;
257 goto out;
258 }
259 if (gf_uuid_is_null(loc->inode->gfid)) {
260 op_ret = posix_gfid_heal(this, real_path, loc, xdata);
261 if (op_ret < 0) {
262 op_errno = -op_ret;
263 op_ret = -1;
264 goto out;
265 }
266 MAKE_ENTRY_HANDLE(real_path, par_path, this, loc, &buf);
267 }
268 }
269
270 op_errno = errno;
271
272 if (op_ret == -1) {
273 if (op_errno != ENOENT) {
274 gf_msg(this->name, GF_LOG_WARNING, op_errno, P_MSG_LSTAT_FAILED,
275 "lstat on %s failed", real_path ? real_path : "null");
276 }
277 entry_ret = -1;
278 if (loc_is_nameless(loc)) {
279 if (!op_errno)
280 op_errno = ESTALE;
281 loc_gfid(loc, gfid);
282 MAKE_HANDLE_ABSPATH_FD(gfid_path, this, gfid, dfd);
283 ret = sys_fstatat(dfd, gfid_path, &statbuf, 0);
284 if (ret == 0 && ((statbuf.st_mode & S_IFMT) == S_IFDIR))
285 /*Don't unset if it was a symlink to a dir.*/
286 goto parent;
287 ret = sys_fstatat(dfd, gfid_path, &statbuf, AT_SYMLINK_NOFOLLOW);
288 if (ret == 0 && statbuf.st_nlink == 1) {
289 gf_msg(this->name, GF_LOG_WARNING, op_errno,
290 P_MSG_HANDLE_DELETE,
291 "Found stale gfid "
292 "handle %s, removing it.",
293 gfid_path);
294 posix_handle_unset_gfid(this, gfid);
295 }
296 }
297 goto parent;
298 }
299
300 if (xdata && (op_ret == 0)) {
301 xattr = posix_xattr_fill(this, real_path, loc, NULL, -1, xdata, &buf);
302
303 cs_obj_status = dict_get_sizen(xdata, GF_CS_OBJECT_STATUS);
304 cs_obj_repair = dict_get_sizen(xdata, GF_CS_OBJECT_REPAIR);
305
306 if (cs_obj_status || cs_obj_repair)
307 posix_cs_maintenance(this, NULL, loc, NULL, &buf, real_path,
308 cs_obj_status, cs_obj_repair, &xattr,
309 _gf_true);
310
311 if (dict_get_sizen(xdata, GF_CLEAN_WRITE_PROTECTION)) {
312 ret = sys_lremovexattr(real_path, GF_PROTECT_FROM_EXTERNAL_WRITES);
313 if (ret == -1 && (errno != ENODATA && errno != ENOATTR))
314 gf_msg(this->name, GF_LOG_ERROR, P_MSG_XATTR_NOT_REMOVED, errno,
315 "removexattr failed. key %s path %s",
316 GF_PROTECT_FROM_EXTERNAL_WRITES, loc->path);
317 }
318
319 if (cs_obj_status)
320 posix_update_iatt_buf(&buf, -1, real_path);
321 }
322
323 if (priv->update_pgfid_nlinks) {
324 if (!gf_uuid_is_null(loc->pargfid) && !IA_ISDIR(buf.ia_type)) {
325 MAKE_PGFID_XATTR_KEY(pgfid_xattr_key, PGFID_XATTR_KEY_PREFIX,
326 loc->pargfid);
327
328 op_ret = posix_inode_ctx_get_all(loc->inode, this, &ctx);
329 if (op_ret < 0) {
330 op_errno = ENOMEM;
331 goto out;
332 }
333
334 pthread_mutex_lock(&ctx->pgfid_lock);
335 {
336 SET_PGFID_XATTR_IF_ABSENT(real_path, pgfid_xattr_key,
337 nlink_samepgfid, XATTR_CREATE, op_ret,
338 this, unlock);
339 }
340 unlock:
341 pthread_mutex_unlock(&ctx->pgfid_lock);
342 }
343 }
344
345 parent:
346 if (par_path) {
347 op_ret = posix_pstat(this, loc->parent, loc->pargfid, par_path,
348 &postparent, _gf_false, _gf_true);
349 if (op_ret == -1) {
350 op_errno = errno;
351 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LSTAT_FAILED,
352 "post-operation lstat on"
353 " parent %s failed",
354 par_path);
355 if (op_errno == ENOENT)
356 /* If parent directory is missing in a lookup,
357 errno should be ESTALE (bad handle) and not
358 ENOENT (missing entry)
359 */
360 op_errno = ESTALE;
361 goto out;
362 }
363 }
364
365 op_ret = entry_ret;
366 out:
367 if (!op_ret && !gfidless && gf_uuid_is_null(buf.ia_gfid)) {
368 gf_msg(this->name, GF_LOG_ERROR, ENODATA, P_MSG_NULL_GFID,
369 "buf->ia_gfid is null for %s",
370 (real_path) ? real_path : "(null)");
371 op_ret = -1;
372 op_errno = ENODATA;
373 }
374
375 /* TODO: get the path */
376 /* In the full run of regression, I was not able to hit this case, hence
377 leaving it as TODO. Good to have logic of resolving GFID only access
378 to a path for many other features too. But initial version can just
379 be knowning that we are hitting the scenario in certain usecases */
380 if ((op_ret == 0) && (dict_get_sizen(xdata, "get-full-path"))) {
381 /* Get the path */
382 gf_log(this->name, GF_LOG_INFO,
383 "%s: inode path not completely resolved. Asking for full path",
384 loc->path);
385 }
386
387 if (op_ret == 0)
388 op_errno = 0;
389
390 nameless_lookup_unlink_dir_out:
391 STACK_UNWIND_STRICT(lookup, frame, op_ret, op_errno,
392 (loc) ? loc->inode : NULL, &buf, xattr, &postparent);
393
394 if (xattr)
395 dict_unref(xattr);
396
397 return 0;
398 }
399
400 static int32_t
401 posix_set_gfid2path_xattr(xlator_t *this, const char *path, uuid_t pgfid,
402 const char *bname)
403 {
404 char xxh64[GF_XXH64_DIGEST_LENGTH * 2 + 1] = {
405 0,
406 };
407 char pgfid_bname[1024] = {
408 0,
409 };
410 char *key = NULL;
411 const size_t key_size = GFID2PATH_XATTR_KEY_PREFIX_LENGTH +
412 GF_XXH64_DIGEST_LENGTH * 2 + 1;
413 int ret = 0;
414 int len;
415
416 len = snprintf(pgfid_bname, sizeof(pgfid_bname), "%s/%s", uuid_utoa(pgfid),
417 bname);
418 gf_xxh64_wrapper((unsigned char *)pgfid_bname, len,
419 GF_XXHSUM64_DEFAULT_SEED, xxh64);
420 key = alloca(key_size);
421 snprintf(key, key_size, GFID2PATH_XATTR_KEY_PREFIX "%s", xxh64);
422
423 ret = sys_lsetxattr(path, key, pgfid_bname, len, XATTR_CREATE);
424 if (ret == -1) {
425 gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_PGFID_OP,
426 "setting gfid2path xattr failed on %s: key = %s ", path, key);
427 }
428
429 return ret;
430 }
431
432 static int
433 posix_acl_xattr_from_dict(const char *path, char *key, dict_t *xattr_req)
434 {
435 int ret = 0;
436 data_t *data = NULL;
437
438 data = dict_get(xattr_req, key);
439 if (data) {
440 ret = sys_lsetxattr(path, key, data->data, data->len, 0);
441 #ifdef __FreeBSD__
442 if (ret != -1) {
443 ret = 0;
444 }
445 #endif /* __FreeBSD__ */
446 }
447
448 return ret;
449 }
450
451 static int
452 posix_acl_xattr_set(const char *path, dict_t *xattr_req)
453 {
454 int ret = 0;
455
456 if (!xattr_req)
457 goto out;
458
459 ret = posix_acl_xattr_from_dict(path, POSIX_ACL_ACCESS_XATTR, xattr_req);
460 if (ret)
461 goto out;
462
463 ret = posix_acl_xattr_from_dict(path, POSIX_ACL_DEFAULT_XATTR, xattr_req);
464
465 out:
466 return ret;
467 }
468
469 int
470 posix_mknod(call_frame_t *frame, xlator_t *this, loc_t *loc, mode_t mode,
471 dev_t dev, mode_t umask, dict_t *xdata)
472 {
473 int tmp_fd = 0;
474 int32_t op_ret = -1;
475 int32_t op_errno = 0;
476 char *real_path = 0;
477 char *par_path = 0;
478 struct iatt stbuf = {
479 0,
480 };
481 struct posix_private *priv = NULL;
482 gid_t gid = 0;
483 struct iatt preparent = {
484 0,
485 };
486 struct iatt postparent = {
487 0,
488 };
489 uuid_t uuid_req = {
490 0,
491 };
492 int32_t nlink_samepgfid = 0;
493 char *pgfid_xattr_key = NULL;
494 gf_boolean_t entry_created = _gf_false, gfid_set = _gf_false;
495 gf_boolean_t linked = _gf_false;
496 gf_loglevel_t level = GF_LOG_NONE;
497 mode_t mode_bit = 0;
498 posix_inode_ctx_t *ctx = NULL;
499
500 DECLARE_OLD_FS_ID_VAR;
501
502 VALIDATE_OR_GOTO(frame, out);
503 VALIDATE_OR_GOTO(this, out);
504 VALIDATE_OR_GOTO(loc, out);
505
506 priv = this->private;
507 VALIDATE_OR_GOTO(priv, out);
508 GFID_NULL_CHECK_AND_GOTO(frame, this, loc, xdata, op_ret, op_errno,
509 uuid_req, out);
510 MAKE_ENTRY_HANDLE(real_path, par_path, this, loc, NULL);
511
512 mode_bit = (priv->create_mask & mode) | priv->force_create_mode;
513 mode = posix_override_umask(mode, mode_bit);
514
515 gid = frame->root->gid;
516
517 SET_FS_ID(frame->root->uid, gid);
518 DISK_SPACE_CHECK_AND_GOTO(frame, priv, xdata, op_ret, op_errno, out);
519 if (!real_path || !par_path) {
520 op_ret = -1;
521 op_errno = ESTALE;
522 goto out;
523 }
524
525 op_ret = posix_pstat(this, loc->parent, loc->pargfid, par_path, &preparent,
526 _gf_false, _gf_true);
527 if (op_ret == -1) {
528 op_errno = errno;
529 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LSTAT_FAILED,
530 "pre-operation lstat on parent of %s failed", real_path);
531 goto out;
532 }
533
534 if (preparent.ia_prot.sgid) {
535 gid = preparent.ia_gid;
536 }
537
538 /* Check if the 'gfid' already exists, because this mknod may be an
539 internal call from distribute for creating 'linkfile', and that
540 linkfile may be for a hardlinked file */
541 if (dict_get_sizen(xdata, GLUSTERFS_INTERNAL_FOP_KEY)) {
542 dict_del_sizen(xdata, GLUSTERFS_INTERNAL_FOP_KEY);
543 /* trash xlator did not bring the uuid_via the call
544 * to GFID_NULL_CHECK_AND_GOTO() above.
545 * Fetch it explicitly here.
546 */
547 if (frame->root->pid == GF_SERVER_PID_TRASH) {
548 op_ret = dict_get_gfuuid(xdata, "gfid-req", &uuid_req);
549 if (op_ret) {
550 gf_msg_debug(this->name, 0,
551 "failed to get the gfid from dict for %s",
552 loc->path);
553 goto real_op;
554 }
555 }
556
557 op_ret = posix_create_link_if_gfid_exists(this, uuid_req, real_path,
558 loc->inode->table);
559 if (!op_ret) {
560 linked = _gf_true;
561 goto post_op;
562 }
563 }
564
565 real_op:
566 #ifdef __NetBSD__
567 if (S_ISFIFO(mode))
568 op_ret = mkfifo(real_path, mode);
569 else
570 #endif /* __NetBSD__ */
571 op_ret = sys_mknod(real_path, mode, dev);
572
573 if (op_ret == -1) {
574 op_errno = errno;
575 if ((op_errno == EINVAL) && S_ISREG(mode)) {
576 /* Over Darwin, mknod with (S_IFREG|mode)
577 doesn't work */
578 tmp_fd = sys_creat(real_path, mode);
579 if (tmp_fd == -1) {
580 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_CREATE_FAILED,
581 "create failed on"
582 "%s",
583 real_path);
584 goto out;
585 }
586 sys_close(tmp_fd);
587 } else {
588 if (op_errno == EEXIST) {
589 level = GF_LOG_DEBUG;
590
591 if (dict_get_sizen(xdata, GF_FORCE_REPLACE_KEY)) {
592 dict_del_sizen(xdata, GF_FORCE_REPLACE_KEY);
593 op_ret = posix_unlink_stale_linkto(frame, this, real_path,
594 &op_errno, loc);
595 if (op_ret == 0)
596 goto real_op;
597
598 level = GF_LOG_ERROR;
599 }
600
601 } else
602 level = GF_LOG_ERROR;
603 gf_msg(this->name, level, errno, P_MSG_MKNOD_FAILED,
604 "mknod on %s failed", real_path);
605 goto out;
606 }
607 }
608
609 entry_created = _gf_true;
610
611 #ifndef HAVE_SET_FSID
612 op_ret = sys_lchown(real_path, frame->root->uid, gid);
613 if (op_ret == -1) {
614 op_errno = errno;
615 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LCHOWN_FAILED,
616 "lchown on %s failed", real_path);
617 goto out;
618 }
619 #endif
620
621 post_op:
622 op_ret = posix_acl_xattr_set(real_path, xdata);
623 if (op_ret) {
624 gf_msg(this->name, GF_LOG_ERROR, 0, P_MSG_ACL_FAILED,
625 "setting ACLs on %s failed", real_path);
626 }
627
628 if (priv->update_pgfid_nlinks) {
629 MAKE_PGFID_XATTR_KEY(pgfid_xattr_key, PGFID_XATTR_KEY_PREFIX,
630 loc->pargfid);
631 op_ret = posix_inode_ctx_get_all(loc->inode, this, &ctx);
632 if (op_ret < 0) {
633 op_errno = ENOMEM;
634 goto out;
635 }
636
637 pthread_mutex_lock(&ctx->pgfid_lock);
638 {
639 LINK_MODIFY_PGFID_XATTR(real_path, pgfid_xattr_key, nlink_samepgfid,
640 0, op_ret, this, unlock);
641 }
642 unlock:
643 pthread_mutex_unlock(&ctx->pgfid_lock);
644 }
645
646 if (priv->gfid2path) {
647 posix_set_gfid2path_xattr(this, real_path, loc->pargfid, loc->name);
648 }
649
650 op_ret = posix_entry_create_xattr_set(this, loc, real_path, xdata);
651 if (op_ret) {
652 if (errno != EEXIST)
653 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_XATTR_FAILED,
654 "setting xattrs on %s failed", real_path);
655 else
656 gf_msg_debug(this->name, 0, "setting xattrs on %s failed",
657 real_path);
658 }
659
660 if (!linked) {
661 op_ret = posix_gfid_set(this, real_path, loc, xdata, frame->root->pid,
662 &op_errno);
663 if (op_ret) {
664 gf_msg(this->name, GF_LOG_ERROR, op_errno, P_MSG_GFID_FAILED,
665 "setting gfid on %s failed", real_path);
666 goto out;
667 } else {
668 gfid_set = _gf_true;
669 }
670 }
671
672 op_ret = posix_pstat(this, loc->inode, NULL, real_path, &stbuf, _gf_false,
673 _gf_true);
674 if (op_ret == -1) {
675 op_errno = errno;
676 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_MKNOD_FAILED,
677 "mknod on %s failed", real_path);
678 goto out;
679 }
680
681 posix_set_ctime(frame, this, real_path, -1, loc->inode, &stbuf);
682
683 op_ret = posix_pstat(this, loc->parent, loc->pargfid, par_path, &postparent,
684 _gf_false, _gf_true);
685 if (op_ret == -1) {
686 op_errno = errno;
687 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LSTAT_FAILED,
688 "post-operation lstat on parent %s failed", par_path);
689 goto out;
690 }
691
692 posix_set_parent_ctime(frame, this, par_path, -1, loc->parent, &postparent);
693
694 op_ret = 0;
695
696 out:
697 SET_TO_OLD_FS_ID();
698
699 if (op_ret < 0) {
700 if (entry_created) {
701 if (S_ISREG(mode))
702 sys_unlink(real_path);
703 else
704 sys_rmdir(real_path);
705 }
706
707 if (gfid_set)
708 posix_gfid_unset(this, xdata);
709 }
710
711 STACK_UNWIND_STRICT(mknod, frame, op_ret, op_errno,
712 (loc) ? loc->inode : NULL, &stbuf, &preparent,
713 &postparent, NULL);
714
715 return 0;
716 }
717
718 int
719 posix_mkdir(call_frame_t *frame, xlator_t *this, loc_t *loc, mode_t mode,
720 mode_t umask, dict_t *xdata)
721 {
722 int32_t op_ret = -1;
723 int32_t op_errno = 0;
724 char *real_path = NULL, *gfid_path = NULL;
725 char *par_path = NULL, *xattr_name = NULL;
726 int xattr_name_len;
727 struct iatt stbuf = {
728 0,
729 };
730 struct posix_private *priv = NULL;
731 gid_t gid = 0;
732 struct iatt preparent = {
733 0,
734 };
735 struct iatt postparent = {
736 0,
737 };
738 gf_boolean_t entry_created = _gf_false, gfid_set = _gf_false;
739 uuid_t uuid_req = {
740 0,
741 };
742 ssize_t size = 0;
743 dict_t *xdata_rsp = NULL;
744 char *disk_xattr = NULL;
745 data_t *arg_data = NULL;
746 char pgfid[GF_UUID_BUF_SIZE] = {0};
747 mode_t mode_bit = 0;
748
749 DECLARE_OLD_FS_ID_VAR;
750
751 VALIDATE_OR_GOTO(frame, out);
752 VALIDATE_OR_GOTO(this, out);
753 VALIDATE_OR_GOTO(loc, out);
754
755 /* The Hidden directory should be for housekeeping purpose and it
756 should not get created from a user request */
757 if (__is_root_gfid(loc->pargfid) &&
758 (strcmp(loc->name, GF_HIDDEN_PATH) == 0)) {
759 gf_msg(this->name, GF_LOG_WARNING, EPERM, P_MSG_MKDIR_NOT_PERMITTED,
760 "mkdir issued on %s, which"
761 "is not permitted",
762 GF_HIDDEN_PATH);
763 op_errno = EPERM;
764 op_ret = -1;
765 goto out;
766 }
767
768 #ifdef __NetBSD__
769 /* Same for NetBSD's .attribute directory */
770 if (__is_root_gfid(loc->pargfid) &&
771 (strcmp(loc->name, ".attribute") == 0)) {
772 gf_msg(this->name, GF_LOG_WARNING, EPERM, P_MSG_MKDIR_NOT_PERMITTED,
773 "mkdir issued on .attribute, which"
774 "is not permitted");
775 op_errno = EPERM;
776 op_ret = -1;
777 goto out;
778 }
779 #endif
780
781 priv = this->private;
782 VALIDATE_OR_GOTO(priv, out);
783 GFID_NULL_CHECK_AND_GOTO(frame, this, loc, xdata, op_ret, op_errno,
784 uuid_req, out);
785 DISK_SPACE_CHECK_AND_GOTO(frame, priv, xdata, op_ret, op_errno, out);
786
787 MAKE_ENTRY_HANDLE(real_path, par_path, this, loc, NULL);
788 if (!real_path || !par_path) {
789 op_ret = -1;
790 op_errno = ESTALE;
791 goto out;
792 }
793
794 gid = frame->root->gid;
795
796 op_ret = posix_pstat(this, loc->inode, NULL, real_path, &stbuf, _gf_false,
797 _gf_false);
798
799 SET_FS_ID(frame->root->uid, gid);
800
801 mode_bit = (priv->create_directory_mask & mode) |
802 priv->force_directory_mode;
803 mode = posix_override_umask(mode, mode_bit);
804
805 if (xdata) {
806 if (!gf_uuid_compare(stbuf.ia_gfid, uuid_req)) {
807 op_ret = -1;
808 op_errno = EEXIST;
809 goto out;
810 }
811 }
812
813 if (!gf_uuid_is_null(uuid_req)) {
814 op_ret = posix_istat(this, loc->inode, uuid_req, NULL, &stbuf,
815 _gf_false);
816 if ((op_ret == 0) && IA_ISDIR(stbuf.ia_type)) {
817 gfid_path = alloca(PATH_MAX);
818 size = posix_handle_path(this, uuid_req, NULL, gfid_path, PATH_MAX);
819 if (size <= 0) {
820 op_errno = ESTALE;
821 op_ret = -1;
822 goto out;
823 }
824
825 if (frame->root->pid != GF_CLIENT_PID_SELF_HEALD) {
826 gf_msg(this->name, GF_LOG_WARNING, 0, P_MSG_DIR_OF_SAME_ID,
827 "mkdir (%s): "
828 "gfid (%s) is already associated with "
829 "directory (%s). Hence, both "
830 "directories will share same gfid and "
831 "this can lead to inconsistencies.",
832 loc->path, uuid_utoa(uuid_req),
833 gfid_path ? gfid_path : "<NULL>");
834
835 gf_event(EVENT_POSIX_SAME_GFID,
836 "gfid=%s;"
837 "path=%s;newpath=%s;brick=%s:%s",
838 uuid_utoa(uuid_req), gfid_path ? gfid_path : "<NULL>",
839 loc->path, priv->hostname, priv->base_path);
840 }
841 if (!posix_symlinks_match(this, loc, uuid_req))
842 /* For afr selfheal of dir renames, we need to
843 * remove the old symlink in order for
844 * posix_gfid_set to set the symlink to the
845 * new dir.*/
846 posix_handle_unset_gfid(this, stbuf.ia_gfid);
847 }
848 } else if (frame->root->pid != GF_SERVER_PID_TRASH) {
849 op_ret = -1;
850 op_errno = EPERM;
851 gf_msg_callingfn(this->name, GF_LOG_WARNING, op_errno, P_MSG_NULL_GFID,
852 "mkdir (%s): is issued without "
853 "gfid-req %p",
854 loc->path, xdata);
855 goto out;
856 }
857
858 op_ret = posix_pstat(this, loc->parent, loc->pargfid, par_path, &preparent,
859 _gf_false, _gf_true);
860 if (op_ret == -1) {
861 op_errno = errno;
862 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LSTAT_FAILED,
863 "pre-operation lstat on parent %s failed", par_path);
864 goto out;
865 }
866
867 if (preparent.ia_prot.sgid) {
868 gid = preparent.ia_gid;
869 mode |= S_ISGID;
870 }
871
872 op_ret = dict_get_str_sizen(xdata, GF_PREOP_PARENT_KEY, &xattr_name);
873 if (xattr_name != NULL) {
874 xattr_name_len = strlen(xattr_name);
875 arg_data = dict_getn(xdata, xattr_name, xattr_name_len);
876 if (arg_data) {
877 if (loc->parent)
878 gf_uuid_unparse(loc->parent->gfid, pgfid);
879 else
880 gf_uuid_unparse(loc->pargfid, pgfid);
881
882 size = 256;
883 disk_xattr = GF_MALLOC(size + 1, gf_posix_mt_char);
884 if (!disk_xattr) {
885 op_ret = -1;
886 op_errno = errno;
887 gf_msg(this->name, GF_LOG_ERROR, errno,
888 P_MSG_PREOP_CHECK_FAILED,
889 "mkdir (%s/%s): GF_MALLOC failed during"
890 " preop of mkdir (%s)",
891 pgfid, loc->name, real_path);
892 goto out;
893 }
894 disk_xattr[size] = '\0';
895
896 size = sys_lgetxattr(par_path, xattr_name, disk_xattr, size);
897 if (size == -1) {
898 if (disk_xattr) {
899 GF_FREE(disk_xattr);
900 disk_xattr = NULL;
901 }
902 if (errno != ERANGE) {
903 op_ret = -1;
904 op_errno = errno;
905 gf_msg(this->name, GF_LOG_ERROR, errno,
906 P_MSG_PREOP_CHECK_FAILED,
907 "mkdir (%s/%s): getxattr failed during"
908 " preop of mkdir (%s).",
909 pgfid, loc->name, real_path);
910 goto out;
911 }
912 gf_msg(this->name, GF_LOG_INFO, errno, P_MSG_PREOP_CHECK_FAILED,
913 "mkdir (%s/%s): getxattr on key "
914 "(%s) path (%s) failed due to "
915 " buffer overflow",
916 pgfid, loc->name, xattr_name, par_path);
917 size = sys_lgetxattr(par_path, xattr_name, NULL, 0);
918 if (size == -1) {
919 op_ret = -1;
920 op_errno = errno;
921 gf_msg(this->name, GF_LOG_ERROR, errno,
922 P_MSG_PREOP_CHECK_FAILED,
923 "mkdir (%s/%s): getxattr on key (%s)"
924 " path (%s) failed ",
925 pgfid, loc->name, xattr_name, par_path);
926 goto out;
927 }
928 disk_xattr = GF_MALLOC(size + 1, gf_posix_mt_char);
929 if (!disk_xattr) {
930 op_ret = -1;
931 op_errno = errno;
932 gf_msg(this->name, GF_LOG_ERROR, errno,
933 P_MSG_PREOP_CHECK_FAILED,
934 "mkdir (%s/%s): GF_MALLOC failed during"
935 " preop of mkdir (%s)",
936 pgfid, loc->name, real_path);
937 goto out;
938 }
939 disk_xattr[size] = '\0';
940 size = sys_lgetxattr(par_path, xattr_name, disk_xattr, size);
941 if (size == -1) {
942 op_errno = errno;
943 gf_msg(this->name, GF_LOG_ERROR, errno,
944 P_MSG_PREOP_CHECK_FAILED,
945 "mkdir (%s/%s): getxattr on "
946 " key (%s) path (%s) failed "
947 "(%s)",
948 pgfid, loc->name, xattr_name, par_path,
949 strerror(errno));
950 goto out;
951 }
952 }
953 if ((arg_data->len != size) ||
954 (memcmp(arg_data->data, disk_xattr, size))) {
955 gf_msg(this->name, GF_LOG_INFO, EIO, P_MSG_PREOP_CHECK_FAILED,
956 "mkdir (%s/%s): failing preop of "
957 "mkdir (%s) as on-disk"
958 " xattr value differs from argument "
959 "value for key %s",
960 pgfid, loc->name, real_path, xattr_name);
961 op_ret = -1;
962 op_errno = EIO;
963
964 xdata_rsp = dict_new();
965 if (xdata_rsp == NULL) {
966 gf_msg(this->name, GF_LOG_ERROR, ENOMEM,
967 P_MSG_PREOP_CHECK_FAILED,
968 "mkdir (%s/%s): "
969 "dict allocation failed",
970 pgfid, loc->name);
971 op_errno = ENOMEM;
972 goto out;
973 }
974
975 op_errno = dict_set_int8(xdata_rsp, GF_PREOP_CHECK_FAILED, 1);
976 if (op_errno < 0)
977 op_errno = errno;
978 goto out;
979 }
980
981 dict_deln(xdata, xattr_name, xattr_name_len);
982 }
983
984 dict_del_sizen(xdata, GF_PREOP_PARENT_KEY);
985 }
986
987 op_ret = sys_mkdir(real_path, mode);
988 if (op_ret == -1) {
989 op_errno = errno;
990 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_MKDIR_FAILED,
991 "mkdir of %s failed", real_path);
992 goto out;
993 }
994
995 entry_created = _gf_true;
996
997 #ifndef HAVE_SET_FSID
998 op_ret = sys_chown(real_path, frame->root->uid, gid);
999 if (op_ret == -1) {
1000 op_errno = errno;
1001 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_CHOWN_FAILED,
1002 "chown on %s failed", real_path);
1003 goto out;
1004 }
1005 #endif
1006 op_ret = posix_acl_xattr_set(real_path, xdata);
1007 if (op_ret) {
1008 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_ACL_FAILED,
1009 "setting ACLs on %s failed ", real_path);
1010 }
1011
1012 op_ret = posix_entry_create_xattr_set(this, loc, real_path, xdata);
1013 if (op_ret) {
1014 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_XATTR_FAILED,
1015 "setting xattrs on %s failed", real_path);
1016 }
1017
1018 op_ret = posix_gfid_set(this, real_path, loc, xdata, frame->root->pid,
1019 &op_errno);
1020 if (op_ret) {
1021 gf_msg(this->name, GF_LOG_ERROR, op_errno, P_MSG_GFID_FAILED,
1022 "setting gfid on %s failed", real_path);
1023 goto out;
1024 } else {
1025 gfid_set = _gf_true;
1026 }
1027
1028 op_ret = posix_pstat(this, loc->inode, NULL, real_path, &stbuf, _gf_false,
1029 _gf_true);
1030 if (op_ret == -1) {
1031 op_errno = errno;
1032 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LSTAT_FAILED,
1033 "lstat on %s failed", real_path);
1034 goto out;
1035 }
1036
1037 posix_set_ctime(frame, this, real_path, -1, loc->inode, &stbuf);
1038
1039 op_ret = posix_pstat(this, loc->parent, loc->pargfid, par_path, &postparent,
1040 _gf_false, _gf_true);
1041 if (op_ret == -1) {
1042 op_errno = errno;
1043 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LSTAT_FAILED,
1044 "post-operation lstat on parent of %s failed", real_path);
1045 goto out;
1046 }
1047
1048 posix_set_parent_ctime(frame, this, par_path, -1, loc->parent, &postparent);
1049
1050 op_ret = 0;
1051
1052 out:
1053 SET_TO_OLD_FS_ID();
1054
1055 if (disk_xattr)
1056 GF_FREE(disk_xattr);
1057
1058 if (op_ret < 0) {
1059 if (entry_created)
1060 sys_rmdir(real_path);
1061
1062 if (gfid_set)
1063 posix_gfid_unset(this, xdata);
1064 }
1065
1066 STACK_UNWIND_STRICT(mkdir, frame, op_ret, op_errno,
1067 (loc) ? loc->inode : NULL, &stbuf, &preparent,
1068 &postparent, xdata_rsp);
1069
1070 if (xdata_rsp)
1071 dict_unref(xdata_rsp);
1072
1073 return 0;
1074 }
1075
1076 static int
1077 posix_add_unlink_to_ctx(inode_t *inode, xlator_t *this, char *unlink_path)
1078 {
1079 uint64_t ctx = GF_UNLINK_FALSE;
1080 int ret = 0;
1081
1082 if (!unlink_path) {
1083 gf_msg(this->name, GF_LOG_ERROR, ENOMEM, P_MSG_UNLINK_FAILED,
1084 "Creation of unlink entry failed");
1085 ret = -1;
1086 goto out;
1087 }
1088
1089 ctx = GF_UNLINK_TRUE;
1090 ret = posix_inode_ctx_set_unlink_flag(inode, this, ctx);
1091 if (ret < 0) {
1092 goto out;
1093 }
1094
1095 out:
1096 return ret;
1097 }
1098
1099 static int32_t
1100 posix_move_gfid_to_unlink(xlator_t *this, uuid_t gfid, loc_t *loc)
1101 {
1102 char *unlink_path = NULL;
1103 char *gfid_path = NULL;
1104 int ret = -1;
1105 struct posix_private *priv_posix = NULL;
1106
1107 priv_posix = (struct posix_private *)this->private;
1108
1109 MAKE_HANDLE_GFID_PATH(gfid_path, this, gfid);
1110
1111 POSIX_GET_FILE_UNLINK_PATH(priv_posix->base_path, loc->inode->gfid,
1112 unlink_path);
1113 if (!unlink_path) {
1114 ret = -1;
1115 goto out;
1116 }
1117 gf_msg_debug(this->name, 0, "Moving gfid: %s to unlink_path : %s",
1118 gfid_path, unlink_path);
1119 ret = sys_rename(gfid_path, unlink_path);
1120 if (ret < 0) {
1121 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_UNLINK_FAILED,
1122 "Creation of unlink entry failed for gfid: %s", unlink_path);
1123 goto out;
1124 }
1125 ret = posix_add_unlink_to_ctx(loc->inode, this, unlink_path);
1126 if (ret < 0)
1127 goto out;
1128
1129 out:
1130 return ret;
1131 }
1132
1133 static int32_t
1134 posix_unlink_gfid_handle_and_entry(call_frame_t *frame, xlator_t *this,
1135 const char *real_path, struct iatt *stbuf,
1136 int32_t *op_errno, loc_t *loc,
1137 gf_boolean_t get_link_count,
1138 dict_t *rsp_dict)
1139 {
1140 int32_t ret = 0;
1141 struct iatt prebuf = {
1142 0,
1143 };
1144 gf_boolean_t locked = _gf_false;
1145 gf_boolean_t update_ctime = _gf_false;
1146
1147 /* Unlink the gfid_handle_first */
1148 if (stbuf && stbuf->ia_nlink == 1) {
1149 LOCK(&loc->inode->lock);
1150
1151 if (loc->inode->fd_count == 0) {
1152 UNLOCK(&loc->inode->lock);
1153 ret = posix_handle_unset_gfid(this, stbuf->ia_gfid);
1154 } else {
1155 UNLOCK(&loc->inode->lock);
1156 ret = posix_move_gfid_to_unlink(this, stbuf->ia_gfid, loc);
1157 }
1158 if (ret) {
1159 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_UNLINK_FAILED,
1160 "unlink of gfid handle "
1161 "failed for path:%s with gfid %s",
1162 real_path, uuid_utoa(stbuf->ia_gfid));
1163 }
1164 } else {
1165 update_ctime = _gf_true;
1166 }
1167
1168 if (get_link_count) {
1169 LOCK(&loc->inode->lock);
1170 locked = _gf_true;
1171 /* Since this stat is to get link count and not for time
1172 * attributes, intentionally passing inode as NULL
1173 */
1174 ret = posix_pstat(this, NULL, loc->gfid, real_path, &prebuf, _gf_true,
1175 _gf_false);
1176 if (ret) {
1177 UNLOCK(&loc->inode->lock);
1178 locked = _gf_false;
1179 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LSTAT_FAILED,
1180 "lstat on %s failed", real_path);
1181 goto err;
1182 }
1183 }
1184
1185 /* Unlink the actual file */
1186 ret = sys_unlink(real_path);
1187
1188 if (locked) {
1189 UNLOCK(&loc->inode->lock);
1190 locked = _gf_false;
1191 }
1192
1193 if (ret == -1) {
1194 if (op_errno)
1195 *op_errno = errno;
1196 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_UNLINK_FAILED,
1197 "unlink of %s failed", real_path);
1198 goto err;
1199 }
1200
1201 if (update_ctime) {
1202 posix_set_ctime(frame, this, NULL, -1, loc->inode, stbuf);
1203 }
1204
1205 if (rsp_dict) {
1206 ret = dict_set_uint32(rsp_dict, GET_LINK_COUNT, prebuf.ia_nlink);
1207 if (ret)
1208 gf_msg(this->name, GF_LOG_WARNING, 0, P_MSG_SET_XDATA_FAIL,
1209 "failed to set " GET_LINK_COUNT " for %s", real_path);
1210 }
1211
1212 return 0;
1213
1214 err:
1215 if (locked) {
1216 UNLOCK(&loc->inode->lock);
1217 locked = _gf_false;
1218 }
1219 return -1;
1220 }
1221
1222 static int
1223 posix_unlink_stale_linkto(call_frame_t *frame, xlator_t *this,
1224 const char *real_path, int32_t *op_errno, loc_t *loc)
1225 {
1226 int ret = 0;
1227 struct iatt stbuf = {
1228 0,
1229 };
1230
1231 /* get the stale file gfid and stat-info */
1232 ret = posix_pstat(this, NULL, NULL, real_path, &stbuf, _gf_false,
1233 _gf_false);
1234 if (ret) {
1235 if (errno == ENOENT) {
1236 ret = 0; /* retry creation if file doesn't exist */
1237 gf_msg(this->name, GF_LOG_INFO, errno, P_MSG_LSTAT_FAILED,
1238 "lstat on %s failed: file unlinked by another client",
1239 real_path);
1240 } else {
1241 gf_msg(this->name, GF_LOG_INFO, errno, P_MSG_LSTAT_FAILED,
1242 "lstat on %s failed", real_path);
1243 }
1244 goto out;
1245 }
1246 /* unlink only if linkto file*/
1247 if (IS_DHT_LINKFILE_MODE(&stbuf)) {
1248 gf_msg(this->name, GF_LOG_INFO, 0, P_MSG_HANDLE_CREATE,
1249 "unlinking stale linkto: %s gfid: %s", real_path,
1250 uuid_utoa(stbuf.ia_gfid));
1251 ret = posix_unlink_gfid_handle_and_entry(
1252 frame, this, real_path, &stbuf, op_errno, loc, _gf_false, NULL);
1253
1254 } else {
1255 gf_msg(this->name, GF_LOG_DEBUG, 0, P_MSG_HANDLE_CREATE,
1256 "skip unlinking stale data-file: %s gfid: %s", real_path,
1257 uuid_utoa(stbuf.ia_gfid));
1258 ret = -1;
1259 }
1260
1261 out:
1262 return ret;
1263 }
1264
1265 static gf_boolean_t
1266 posix_skip_non_linkto_unlink(dict_t *xdata, loc_t *loc, char *key,
1267 const int keylen, const char *linkto_xattr,
1268 struct iatt *stbuf, const char *real_path)
1269 {
1270 gf_boolean_t skip_unlink = _gf_false;
1271 gf_boolean_t is_dht_linkto_file = _gf_false;
1272 int unlink_if_linkto = 0;
1273 ssize_t xattr_size = -1;
1274 int op_ret = -1;
1275
1276 op_ret = dict_get_int32n(xdata, key, keylen, &unlink_if_linkto);
1277
1278 if (!op_ret && unlink_if_linkto) {
1279 is_dht_linkto_file = IS_DHT_LINKFILE_MODE(stbuf);
1280 if (!is_dht_linkto_file)
1281 return _gf_true;
1282
1283 LOCK(&loc->inode->lock);
1284
1285 xattr_size = sys_lgetxattr(real_path, linkto_xattr, NULL, 0);
1286
1287 UNLOCK(&loc->inode->lock);
1288
1289 if (xattr_size <= 0)
1290 skip_unlink = _gf_true;
1291
1292 gf_msg("posix", GF_LOG_INFO, 0, P_MSG_XATTR_STATUS,
1293 "linkto_xattr status: %" PRIu32 " for %s", skip_unlink,
1294 real_path);
1295 }
1296 return skip_unlink;
1297 }
1298
1299 static int32_t
1300 posix_remove_gfid2path_xattr(xlator_t *this, const char *path, uuid_t pgfid,
1301 const char *bname)
1302 {
1303 char xxh64[GF_XXH64_DIGEST_LENGTH * 2 + 1] = {
1304 0,
1305 };
1306 char pgfid_bname[1024] = {
1307 0,
1308 };
1309 int ret = 0;
1310 char *key = NULL;
1311 const size_t key_size = GFID2PATH_XATTR_KEY_PREFIX_LENGTH +
1312 GF_XXH64_DIGEST_LENGTH * 2 + 1;
1313 int len;
1314
1315 len = snprintf(pgfid_bname, sizeof(pgfid_bname), "%s/%s", uuid_utoa(pgfid),
1316 bname);
1317 gf_xxh64_wrapper((unsigned char *)pgfid_bname, len,
1318 GF_XXHSUM64_DEFAULT_SEED, xxh64);
1319 key = alloca(key_size);
1320 snprintf(key, key_size, GFID2PATH_XATTR_KEY_PREFIX "%s", xxh64);
1321
1322 ret = sys_lremovexattr(path, key);
1323 if (ret == -1) {
1324 gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_PGFID_OP,
1325 "removing gfid2path xattr failed on %s: key = %s", path, key);
1326 }
1327
1328 return ret;
1329 }
1330
1331 int32_t
1332 posix_unlink(call_frame_t *frame, xlator_t *this, loc_t *loc, int xflag,
1333 dict_t *xdata)
1334 {
1335 int32_t op_ret = -1;
1336 int32_t op_errno = 0;
1337 char *real_path = NULL;
1338 char *par_path = NULL;
1339 int32_t fd = -1;
1340 int ret = -1;
1341 struct iatt stbuf = {
1342 0,
1343 };
1344 struct iatt postbuf = {
1345 0,
1346 };
1347 struct posix_private *priv = NULL;
1348 struct iatt preparent = {
1349 0,
1350 };
1351 struct iatt postparent = {
1352 0,
1353 };
1354 char *pgfid_xattr_key = NULL;
1355 int32_t nlink_samepgfid = 0;
1356 int32_t check_open_fd = 0;
1357 int32_t skip_unlink = 0;
1358 int32_t fdstat_requested = 0;
1359 dict_t *unwind_dict = NULL;
1360 gf_boolean_t get_link_count = _gf_false;
1361 posix_inode_ctx_t *ctx = NULL;
1362
1363 DECLARE_OLD_FS_ID_VAR;
1364
1365 VALIDATE_OR_GOTO(frame, out);
1366 VALIDATE_OR_GOTO(this, out);
1367 VALIDATE_OR_GOTO(this->private, out);
1368 VALIDATE_OR_GOTO(loc, out);
1369
1370 SET_FS_ID(frame->root->uid, frame->root->gid);
1371 MAKE_ENTRY_HANDLE(real_path, par_path, this, loc, &stbuf);
1372 if (!real_path || !par_path) {
1373 op_ret = -1;
1374 op_errno = ESTALE;
1375 goto out;
1376 }
1377
1378 op_ret = posix_pstat(this, loc->parent, loc->pargfid, par_path, &preparent,
1379 _gf_false, _gf_true);
1380 if (op_ret == -1) {
1381 op_errno = errno;
1382 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LSTAT_FAILED,
1383 "pre-operation lstat on parent %s failed", par_path);
1384 goto out;
1385 }
1386
1387 priv = this->private;
1388
1389 op_ret = dict_get_int32_sizen(xdata, DHT_SKIP_OPEN_FD_UNLINK,
1390 &check_open_fd);
1391
1392 if (!op_ret && check_open_fd) {
1393 LOCK(&loc->inode->lock);
1394
1395 if (loc->inode->fd_count) {
1396 skip_unlink = 1;
1397 }
1398
1399 UNLOCK(&loc->inode->lock);
1400
1401 gf_msg(this->name, GF_LOG_INFO, 0, P_MSG_KEY_STATUS_INFO,
1402 "open-fd-key-status: %" PRIu32 " for %s", skip_unlink,
1403 real_path);
1404
1405 if (skip_unlink) {
1406 op_ret = -1;
1407 op_errno = EBUSY;
1408 goto out;
1409 }
1410 }
1411 /*
1412 * If either of the function return true, skip_unlink.
1413 * If first first function itself return true,
1414 * we don't need to call second function, skip unlink.
1415 */
1416 skip_unlink = posix_skip_non_linkto_unlink(
1417 xdata, loc, DHT_SKIP_NON_LINKTO_UNLINK,
1418 SLEN(DHT_SKIP_NON_LINKTO_UNLINK), DHT_LINKTO, &stbuf, real_path);
1419 if (skip_unlink) {
1420 op_ret = -1;
1421 op_errno = EBUSY;
1422 goto out;
1423 }
1424
1425 if (IA_ISREG(loc->inode->ia_type) && xdata &&
1426 dict_get_sizen(xdata, DHT_IATT_IN_XDATA_KEY)) {
1427 fdstat_requested = 1;
1428 }
1429
1430 if (fdstat_requested ||
1431 (priv->background_unlink && IA_ISREG(loc->inode->ia_type))) {
1432 fd = sys_open(real_path, O_RDONLY, 0);
1433 if (fd == -1) {
1434 op_ret = -1;
1435 op_errno = errno;
1436 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_OPEN_FAILED,
1437 "open of %s failed", real_path);
1438 goto out;
1439 }
1440 }
1441
1442 if (priv->update_pgfid_nlinks && (stbuf.ia_nlink > 1)) {
1443 MAKE_PGFID_XATTR_KEY(pgfid_xattr_key, PGFID_XATTR_KEY_PREFIX,
1444 loc->pargfid);
1445 op_ret = posix_inode_ctx_get_all(loc->inode, this, &ctx);
1446 if (op_ret < 0) {
1447 op_errno = ENOMEM;
1448 goto out;
1449 }
1450 pthread_mutex_lock(&ctx->pgfid_lock);
1451 {
1452 UNLINK_MODIFY_PGFID_XATTR(real_path, pgfid_xattr_key,
1453 nlink_samepgfid, 0, op_ret, this, unlock);
1454 }
1455 unlock:
1456 pthread_mutex_unlock(&ctx->pgfid_lock);
1457
1458 if (op_ret < 0) {
1459 gf_msg(this->name, GF_LOG_WARNING, 0, P_MSG_XATTR_FAILED,
1460 "modification of "
1461 "parent gfid xattr failed (path:%s gfid:%s)",
1462 real_path, uuid_utoa(loc->inode->gfid));
1463 if (op_errno != ENOATTR)
1464 /* Allow unlink if pgfid xattr is not set. */
1465 goto out;
1466 }
1467 }
1468
1469 if (priv->gfid2path && (stbuf.ia_nlink > 1)) {
1470 op_ret = posix_remove_gfid2path_xattr(this, real_path, loc->pargfid,
1471 loc->name);
1472 if (op_ret < 0) {
1473 /* Allow unlink if pgfid xattr is not set. */
1474 if (errno != ENOATTR)
1475 goto out;
1476 }
1477 }
1478
1479 unwind_dict = dict_new();
1480 if (!unwind_dict) {
1481 op_errno = ENOMEM;
1482 op_ret = -1;
1483 goto out;
1484 }
1485
1486 if (xdata && dict_get_sizen(xdata, GF_GET_FILE_BLOCK_COUNT)) {
1487 ret = dict_set_uint64(unwind_dict, GF_GET_FILE_BLOCK_COUNT,
1488 stbuf.ia_blocks);
1489 if (ret)
1490 gf_msg(this->name, GF_LOG_WARNING, 0, P_MSG_SET_XDATA_FAIL,
1491 "Failed to set %s in rsp dict", GF_GET_FILE_BLOCK_COUNT);
1492 }
1493
1494 if (xdata && dict_get_sizen(xdata, GET_LINK_COUNT))
1495 get_link_count = _gf_true;
1496 op_ret = posix_unlink_gfid_handle_and_entry(frame, this, real_path, &stbuf,
1497 &op_errno, loc, get_link_count,
1498 unwind_dict);
1499 if (op_ret == -1) {
1500 goto out;
1501 }
1502
1503 if (fdstat_requested) {
1504 op_ret = posix_fdstat(this, loc->inode, fd, &postbuf, _gf_true);
1505 if (op_ret == -1) {
1506 op_errno = errno;
1507 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_FSTAT_FAILED,
1508 "post operation "
1509 "fstat failed on fd=%d",
1510 fd);
1511 goto out;
1512 }
1513 op_ret = posix_set_iatt_in_dict(unwind_dict, NULL, &postbuf);
1514 if (op_ret == -1) {
1515 op_errno = ENOMEM;
1516 gf_msg(this->name, GF_LOG_ERROR, ENOMEM, P_MSG_DICT_SET_FAILED,
1517 "failed to set fdstat in dict");
1518 }
1519 }
1520
1521 op_ret = posix_pstat(this, loc->parent, loc->pargfid, par_path, &postparent,
1522 _gf_false, _gf_true);
1523 if (op_ret == -1) {
1524 op_errno = errno;
1525 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LSTAT_FAILED,
1526 "post-operation lstat on parent %s failed", par_path);
1527 goto out;
1528 }
1529
1530 posix_set_parent_ctime(frame, this, par_path, -1, loc->parent, &postparent);
1531
1532 unwind_dict = posix_dict_set_nlink(xdata, unwind_dict, stbuf.ia_nlink);
1533 op_ret = 0;
1534 out:
1535 SET_TO_OLD_FS_ID();
1536
1537 STACK_UNWIND_STRICT(unlink, frame, op_ret, op_errno, &preparent,
1538 &postparent, unwind_dict);
1539
1540 if (fd != -1) {
1541 sys_close(fd);
1542 }
1543
1544 /* unref unwind_dict*/
1545 if (unwind_dict) {
1546 dict_unref(unwind_dict);
1547 }
1548
1549 return 0;
1550 }
1551
1552 int
1553 posix_rmdir(call_frame_t *frame, xlator_t *this, loc_t *loc, int flags,
1554 dict_t *xdata)
1555 {
1556 int32_t op_ret = -1;
1557 int32_t op_errno = 0;
1558 char *real_path = NULL;
1559 char *par_path = NULL;
1560 char *gfid_str = NULL;
1561 struct iatt preparent = {
1562 0,
1563 };
1564 struct iatt postparent = {
1565 0,
1566 };
1567 struct iatt stbuf = {
1568 0,
1569 };
1570 struct posix_private *priv = NULL;
1571 char tmp_path[PATH_MAX] = {
1572 0,
1573 };
1574
1575 DECLARE_OLD_FS_ID_VAR;
1576
1577 VALIDATE_OR_GOTO(frame, out);
1578 VALIDATE_OR_GOTO(this, out);
1579 VALIDATE_OR_GOTO(loc, out);
1580
1581 SET_FS_ID(frame->root->uid, frame->root->gid);
1582
1583 /* The Hidden directory should be for housekeeping purpose and it
1584 should not get deleted from inside process */
1585 if (__is_root_gfid(loc->pargfid) &&
1586 (strcmp(loc->name, GF_HIDDEN_PATH) == 0)) {
1587 gf_msg(this->name, GF_LOG_WARNING, EPERM, P_MSG_RMDIR_NOT_PERMITTED,
1588 "rmdir issued on %s, which"
1589 "is not permitted",
1590 GF_HIDDEN_PATH);
1591 op_errno = EPERM;
1592 op_ret = -1;
1593 goto out;
1594 }
1595
1596 #ifdef __NetBSD__
1597 /* Same for NetBSD's .attribute directory */
1598 if (__is_root_gfid(loc->pargfid) &&
1599 (strcmp(loc->name, ".attribute") == 0)) {
1600 gf_msg(this->name, GF_LOG_WARNING, EPERM, P_MSG_RMDIR_NOT_PERMITTED,
1601 "rmdir issued on .attribute, which"
1602 "is not permitted");
1603 op_errno = EPERM;
1604 op_ret = -1;
1605 goto out;
1606 }
1607 #endif
1608
1609 priv = this->private;
1610
1611 MAKE_ENTRY_HANDLE(real_path, par_path, this, loc, &stbuf);
1612 if (!real_path || !par_path) {
1613 op_ret = -1;
1614 op_errno = ESTALE;
1615 goto out;
1616 }
1617
1618 op_ret = posix_pstat(this, loc->parent, loc->pargfid, par_path, &preparent,
1619 _gf_false, _gf_true);
1620 if (op_ret == -1) {
1621 op_errno = errno;
1622 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LSTAT_FAILED,
1623 "pre-operation lstat on parent %s failed", par_path);
1624 goto out;
1625 }
1626
1627 if (flags) {
1628 op_ret = sys_mkdir(priv->trash_path, 0755);
1629 if (errno != EEXIST && op_ret == -1) {
1630 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_MKDIR_FAILED,
1631 "mkdir of %s failed", priv->trash_path);
1632 } else {
1633 gfid_str = uuid_utoa(stbuf.ia_gfid);
1634 (void)snprintf(tmp_path, sizeof(tmp_path), "%s/%s",
1635 priv->trash_path, gfid_str);
1636 gf_msg_debug(this->name, 0, "Moving %s to %s", real_path, tmp_path);
1637 op_ret = sys_rename(real_path, tmp_path);
1638 }
1639 } else {
1640 op_ret = sys_rmdir(real_path);
1641 }
1642 op_errno = errno;
1643
1644 if (op_ret == 0) {
1645 if (posix_symlinks_match(this, loc, stbuf.ia_gfid))
1646 posix_handle_unset_gfid(this, stbuf.ia_gfid);
1647 }
1648
1649 if (op_errno == EEXIST)
1650 /* Solaris sets errno = EEXIST instead of ENOTEMPTY */
1651 op_errno = ENOTEMPTY;
1652
1653 /* No need to log a common error as ENOTEMPTY */
1654 if (op_ret == -1 && op_errno != ENOTEMPTY) {
1655 gf_msg(this->name, GF_LOG_ERROR, op_errno, P_MSG_RMDIR_FAILED,
1656 "rmdir of %s failed", real_path);
1657 }
1658
1659 if (op_ret == -1) {
1660 if (op_errno == ENOTEMPTY) {
1661 gf_msg_debug(this->name, 0, "%s on %s failed",
1662 (flags) ? "rename" : "rmdir", real_path);
1663 } else {
1664 gf_msg(this->name, GF_LOG_ERROR, op_errno,
1665 P_MSG_DIR_OPERATION_FAILED, "%s on %s failed",
1666 (flags) ? "rename" : "rmdir", real_path);
1667 }
1668 goto out;
1669 }
1670
1671 op_ret = posix_pstat(this, loc->parent, loc->pargfid, par_path, &postparent,
1672 _gf_false, _gf_true);
1673 if (op_ret == -1) {
1674 op_errno = errno;
1675 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LSTAT_FAILED,
1676 "post-operation lstat on parent of %s failed", par_path);
1677 goto out;
1678 }
1679
1680 posix_set_parent_ctime(frame, this, par_path, -1, loc->parent, &postparent);
1681
1682 out:
1683 SET_TO_OLD_FS_ID();
1684
1685 STACK_UNWIND_STRICT(rmdir, frame, op_ret, op_errno, &preparent, &postparent,
1686 NULL);
1687
1688 return 0;
1689 }
1690
1691 int
1692 posix_symlink(call_frame_t *frame, xlator_t *this, const char *linkname,
1693 loc_t *loc, mode_t umask, dict_t *xdata)
1694 {
1695 int32_t op_ret = -1;
1696 int32_t op_errno = 0;
1697 char *real_path = 0;
1698 char *par_path = 0;
1699 struct iatt stbuf = {
1700 0,
1701 };
1702 struct posix_private *priv = NULL;
1703 gid_t gid = 0;
1704 struct iatt preparent = {
1705 0,
1706 };
1707 struct iatt postparent = {
1708 0,
1709 };
1710 char *pgfid_xattr_key = NULL;
1711 int32_t nlink_samepgfid = 0;
1712 gf_boolean_t entry_created = _gf_false, gfid_set = _gf_false;
1713 uuid_t uuid_req = {
1714 0,
1715 };
1716
1717 DECLARE_OLD_FS_ID_VAR;
1718
1719 VALIDATE_OR_GOTO(frame, out);
1720 VALIDATE_OR_GOTO(this, out);
1721 VALIDATE_OR_GOTO(linkname, out);
1722 VALIDATE_OR_GOTO(loc, out);
1723
1724 priv = this->private;
1725 VALIDATE_OR_GOTO(priv, out);
1726 GFID_NULL_CHECK_AND_GOTO(frame, this, loc, xdata, op_ret, op_errno,
1727 uuid_req, out);
1728 DISK_SPACE_CHECK_AND_GOTO(frame, priv, xdata, op_ret, op_errno, out);
1729
1730 MAKE_ENTRY_HANDLE(real_path, par_path, this, loc, &stbuf);
1731
1732 gid = frame->root->gid;
1733 if (!real_path || !par_path) {
1734 op_ret = -1;
1735 op_errno = ESTALE;
1736 goto out;
1737 }
1738
1739 SET_FS_ID(frame->root->uid, gid);
1740
1741 op_ret = posix_pstat(this, loc->parent, loc->pargfid, par_path, &preparent,
1742 _gf_false, _gf_true);
1743 if (op_ret == -1) {
1744 op_errno = errno;
1745 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LSTAT_FAILED,
1746 "pre-operation lstat on parent %s failed", par_path);
1747 goto out;
1748 }
1749
1750 if (preparent.ia_prot.sgid) {
1751 gid = preparent.ia_gid;
1752 }
1753
1754 op_ret = sys_symlink(linkname, real_path);
1755
1756 if (op_ret == -1) {
1757 op_errno = errno;
1758 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_SYMLINK_FAILED,
1759 "symlink of %s --> %s failed", real_path, linkname);
1760 goto out;
1761 }
1762
1763 entry_created = _gf_true;
1764
1765 posix_set_ctime(frame, this, real_path, -1, loc->inode, &stbuf);
1766
1767 #ifndef HAVE_SET_FSID
1768 op_ret = sys_lchown(real_path, frame->root->uid, gid);
1769 if (op_ret == -1) {
1770 op_errno = errno;
1771 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LCHOWN_FAILED,
1772 "lchown failed on %s", real_path);
1773 goto out;
1774 }
1775 #endif
1776 op_ret = posix_acl_xattr_set(real_path, xdata);
1777 if (op_ret) {
1778 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_ACL_FAILED,
1779 "setting ACLs on %s failed", real_path);
1780 }
1781
1782 if (priv->update_pgfid_nlinks) {
1783 MAKE_PGFID_XATTR_KEY(pgfid_xattr_key, PGFID_XATTR_KEY_PREFIX,
1784 loc->pargfid);
1785 nlink_samepgfid = 1;
1786 SET_PGFID_XATTR(real_path, pgfid_xattr_key, nlink_samepgfid,
1787 XATTR_CREATE, op_ret, this, ignore);
1788 }
1789
1790 if (priv->gfid2path) {
1791 posix_set_gfid2path_xattr(this, real_path, loc->pargfid, loc->name);
1792 }
1793
1794 ignore:
1795 op_ret = posix_entry_create_xattr_set(this, loc, real_path, xdata);
1796 if (op_ret) {
1797 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_XATTR_FAILED,
1798 "setting xattrs on %s failed ", real_path);
1799 }
1800
1801 op_ret = posix_gfid_set(this, real_path, loc, xdata, frame->root->pid,
1802 &op_errno);
1803 if (op_ret) {
1804 gf_msg(this->name, GF_LOG_ERROR, op_errno, P_MSG_GFID_FAILED,
1805 "setting gfid on %s failed", real_path);
1806 goto out;
1807 } else {
1808 gfid_set = _gf_true;
1809 }
1810
1811 op_ret = posix_pstat(this, loc->inode, NULL, real_path, &stbuf, _gf_false,
1812 _gf_true);
1813 if (op_ret == -1) {
1814 op_errno = errno;
1815 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LSTAT_FAILED,
1816 "lstat failed on %s", real_path);
1817 goto out;
1818 }
1819
1820 op_ret = posix_pstat(this, loc->parent, loc->pargfid, par_path, &postparent,
1821 _gf_false, _gf_true);
1822 if (op_ret == -1) {
1823 op_errno = errno;
1824 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LSTAT_FAILED,
1825 "post-operation lstat on parent %s failed", par_path);
1826 goto out;
1827 }
1828
1829 posix_set_parent_ctime(frame, this, par_path, -1, loc->parent, &postparent);
1830
1831 op_ret = 0;
1832
1833 out:
1834 SET_TO_OLD_FS_ID();
1835
1836 if (op_ret < 0) {
1837 if (entry_created)
1838 sys_unlink(real_path);
1839
1840 if (gfid_set)
1841 posix_gfid_unset(this, xdata);
1842 }
1843
1844 STACK_UNWIND_STRICT(symlink, frame, op_ret, op_errno,
1845 (loc) ? loc->inode : NULL, &stbuf, &preparent,
1846 &postparent, NULL);
1847
1848 return 0;
1849 }
1850
1851 int
1852 posix_rename(call_frame_t *frame, xlator_t *this, loc_t *oldloc, loc_t *newloc,
1853 dict_t *xdata)
1854 {
1855 int32_t op_ret = -1;
1856 int32_t op_errno = 0;
1857 char *real_oldpath = NULL;
1858 char *real_newpath = NULL;
1859 char *par_oldpath = NULL;
1860 char *par_newpath = NULL;
1861 struct iatt stbuf = {
1862 0,
1863 };
1864 struct posix_private *priv = NULL;
1865 int was_present = 1;
1866 struct iatt preoldparent = {
1867 0,
1868 };
1869 struct iatt postoldparent = {
1870 0,
1871 };
1872 struct iatt prenewparent = {
1873 0,
1874 };
1875 struct iatt postnewparent = {
1876 0,
1877 };
1878 char olddirid[64];
1879 char newdirid[64];
1880 uuid_t victim = {0};
1881 int was_dir = 0;
1882 int nlink = 0;
1883 int64_t blocks = 0;
1884 char *pgfid_xattr_key = NULL;
1885 int32_t nlink_samepgfid = 0;
1886 char *gfid_path = NULL;
1887 dict_t *unwind_dict = NULL;
1888 gf_boolean_t locked = _gf_false;
1889 gf_boolean_t get_link_count = _gf_false;
1890 posix_inode_ctx_t *ctx_old = NULL;
1891 posix_inode_ctx_t *ctx_new = NULL;
1892
1893 DECLARE_OLD_FS_ID_VAR;
1894
1895 VALIDATE_OR_GOTO(frame, out);
1896 VALIDATE_OR_GOTO(this, out);
1897 VALIDATE_OR_GOTO(oldloc, out);
1898 VALIDATE_OR_GOTO(newloc, out);
1899
1900 priv = this->private;
1901 VALIDATE_OR_GOTO(priv, out);
1902
1903 SET_FS_ID(frame->root->uid, frame->root->gid);
1904 MAKE_ENTRY_HANDLE(real_oldpath, par_oldpath, this, oldloc, NULL);
1905 if (!real_oldpath || !par_oldpath) {
1906 op_ret = -1;
1907 op_errno = ESTALE;
1908 goto out;
1909 }
1910
1911 MAKE_ENTRY_HANDLE(real_newpath, par_newpath, this, newloc, &stbuf);
1912 if (!real_newpath || !par_newpath) {
1913 op_ret = -1;
1914 op_errno = ESTALE;
1915 goto out;
1916 }
1917
1918 unwind_dict = dict_new();
1919 if (!unwind_dict) {
1920 op_ret = -1;
1921 op_errno = ENOMEM;
1922 goto out;
1923 }
1924
1925 op_ret = posix_pstat(this, oldloc->parent, oldloc->pargfid, par_oldpath,
1926 &preoldparent, _gf_false, _gf_true);
1927 if (op_ret == -1) {
1928 op_errno = errno;
1929 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LSTAT_FAILED,
1930 "pre-operation lstat on parent %s failed", par_oldpath);
1931 goto out;
1932 }
1933
1934 op_ret = posix_pstat(this, newloc->parent, newloc->pargfid, par_newpath,
1935 &prenewparent, _gf_false, _gf_false);
1936 if (op_ret == -1) {
1937 op_errno = errno;
1938 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LSTAT_FAILED,
1939 "pre-operation lstat on parent of %s failed", par_newpath);
1940 goto out;
1941 }
1942
1943 op_ret = posix_pstat(this, newloc->inode, NULL, real_newpath, &stbuf,
1944 _gf_false, _gf_false);
1945 if ((op_ret == -1) && (errno == ENOENT)) {
1946 was_present = 0;
1947 } else {
1948 gf_uuid_copy(victim, stbuf.ia_gfid);
1949 if (IA_ISDIR(stbuf.ia_type))
1950 was_dir = 1;
1951 nlink = stbuf.ia_nlink;
1952 blocks = stbuf.ia_blocks;
1953 if (IA_ISDIR(stbuf.ia_type)) {
1954 if (!newloc->inode) {
1955 gf_msg(this->name, GF_LOG_WARNING, EEXIST, P_MSG_DIR_FOUND,
1956 "found directory at %s while expecting ENOENT",
1957 real_newpath);
1958 op_ret = -1;
1959 op_errno = EEXIST;
1960 goto out;
1961 }
1962 if (gf_uuid_compare(newloc->inode->gfid, stbuf.ia_gfid)) {
1963 gf_msg(this->name, GF_LOG_WARNING, EEXIST, P_MSG_DIR_FOUND,
1964 "found directory %s at %s while renaming %s",
1965 uuid_utoa_r(newloc->inode->gfid, olddirid), real_newpath,
1966 uuid_utoa_r(stbuf.ia_gfid, newdirid));
1967 op_ret = -1;
1968 op_errno = EEXIST;
1969 goto out;
1970 }
1971 }
1972 }
1973
1974 op_ret = posix_inode_ctx_get_all(oldloc->inode, this, &ctx_old);
1975 if (op_ret < 0) {
1976 op_ret = -1;
1977 op_errno = ENOMEM;
1978 goto out;
1979 }
1980
1981 if (newloc->inode) {
1982 op_ret = posix_inode_ctx_get_all(newloc->inode, this, &ctx_new);
1983 if (op_ret < 0) {
1984 op_ret = -1;
1985 op_errno = ENOMEM;
1986 goto out;
1987 }
1988 }
1989
1990 pthread_mutex_lock(&ctx_old->pgfid_lock);
1991 {
1992 if (!IA_ISDIR(oldloc->inode->ia_type) && priv->update_pgfid_nlinks) {
1993 MAKE_PGFID_XATTR_KEY(pgfid_xattr_key, PGFID_XATTR_KEY_PREFIX,
1994 oldloc->pargfid);
1995 UNLINK_MODIFY_PGFID_XATTR(real_oldpath, pgfid_xattr_key,
1996 nlink_samepgfid, 0, op_ret, this, unlock);
1997 }
1998
1999 if ((xdata) && (dict_get(xdata, GET_LINK_COUNT)) && (real_newpath) &&
2000 (was_present) && ctx_new) {
2001 pthread_mutex_lock(&ctx_new->pgfid_lock);
2002 locked = _gf_true;
2003 get_link_count = _gf_true;
2004 op_ret = posix_pstat(this, newloc->inode, newloc->gfid,
2005 real_newpath, &stbuf, _gf_false, _gf_false);
2006 if ((op_ret == -1) && (errno != ENOENT)) {
2007 op_errno = errno;
2008 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LSTAT_FAILED,
2009 "lstat on %s failed", real_newpath);
2010 goto unlock;
2011 }
2012 }
2013
2014 op_ret = sys_rename(real_oldpath, real_newpath);
2015 if (op_ret == -1) {
2016 op_errno = errno;
2017 if (op_errno == ENOTEMPTY) {
2018 gf_msg_debug(this->name, op_errno,
2019 "rename of %s to"
2020 " %s failed",
2021 real_oldpath, real_newpath);
2022 } else {
2023 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_RENAME_FAILED,
2024 "rename of %s to %s failed", real_oldpath, real_newpath);
2025 }
2026
2027 if (priv->update_pgfid_nlinks &&
2028 !IA_ISDIR(oldloc->inode->ia_type)) {
2029 LINK_MODIFY_PGFID_XATTR(real_oldpath, pgfid_xattr_key,
2030 nlink_samepgfid, 0, op_ret, this,
2031 unlock);
2032 }
2033
2034 goto unlock;
2035 }
2036
2037 if (locked) {
2038 pthread_mutex_unlock(&ctx_new->pgfid_lock);
2039 locked = _gf_false;
2040 }
2041
2042 if ((get_link_count) &&
2043 (dict_set_uint32(unwind_dict, GET_LINK_COUNT, stbuf.ia_nlink)))
2044 gf_msg(this->name, GF_LOG_WARNING, 0, P_MSG_SET_XDATA_FAIL,
2045 "failed to set " GET_LINK_COUNT " for %s", real_newpath);
2046
2047 if (!IA_ISDIR(oldloc->inode->ia_type) && priv->update_pgfid_nlinks) {
2048 MAKE_PGFID_XATTR_KEY(pgfid_xattr_key, PGFID_XATTR_KEY_PREFIX,
2049 newloc->pargfid);
2050 LINK_MODIFY_PGFID_XATTR(real_newpath, pgfid_xattr_key,
2051 nlink_samepgfid, 0, op_ret, this, unlock);
2052 }
2053
2054 if (!IA_ISDIR(oldloc->inode->ia_type) && priv->gfid2path) {
2055 MAKE_HANDLE_ABSPATH(gfid_path, this, oldloc->inode->gfid);
2056
2057 posix_remove_gfid2path_xattr(this, gfid_path, oldloc->pargfid,
2058 oldloc->name);
2059 posix_set_gfid2path_xattr(this, gfid_path, newloc->pargfid,
2060 newloc->name);
2061 }
2062 }
2063
2064 unlock:
2065 if (locked) {
2066 pthread_mutex_unlock(&ctx_new->pgfid_lock);
2067 locked = _gf_false;
2068 }
2069 pthread_mutex_unlock(&ctx_old->pgfid_lock);
2070
2071 if (op_ret < 0) {
2072 gf_msg(this->name, GF_LOG_WARNING, 0, P_MSG_XATTR_FAILED,
2073 "modification of "
2074 "parent gfid xattr failed (gfid:%s)",
2075 uuid_utoa(oldloc->inode->gfid));
2076 goto out;
2077 }
2078
2079 if (was_dir)
2080 posix_handle_unset_gfid(this, victim);
2081
2082 if (was_present && !was_dir && nlink == 1)
2083 posix_handle_unset_gfid(this, victim);
2084
2085 if (IA_ISDIR(oldloc->inode->ia_type)) {
2086 posix_handle_unset_gfid(this, oldloc->inode->gfid);
2087 posix_handle_soft(this, real_newpath, newloc, oldloc->inode->gfid,
2088 NULL);
2089 }
2090
2091 op_ret = posix_pstat(this, newloc->inode, NULL, real_newpath, &stbuf,
2092 _gf_false, _gf_true);
2093 if (op_ret == -1) {
2094 op_errno = errno;
2095 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LSTAT_FAILED,
2096 "lstat on %s failed", real_newpath);
2097 goto out;
2098 }
2099
2100 /* Since the same inode is later used and dst inode is not present,
2101 * update ctime on source inode. It can't use old path because it
2102 * doesn't exist and xattr has to be stored on disk */
2103 posix_set_ctime(frame, this, real_newpath, -1, oldloc->inode, &stbuf);
2104
2105 op_ret = posix_pstat(this, oldloc->parent, oldloc->pargfid, par_oldpath,
2106 &postoldparent, _gf_false, _gf_true);
2107 if (op_ret == -1) {
2108 op_errno = errno;
2109 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LSTAT_FAILED,
2110 "post-operation lstat on parent %s failed", par_oldpath);
2111 goto out;
2112 }
2113
2114 posix_set_parent_ctime(frame, this, par_oldpath, -1, oldloc->parent,
2115 &postoldparent);
2116
2117 op_ret = posix_pstat(this, newloc->parent, newloc->pargfid, par_newpath,
2118 &postnewparent, _gf_false, _gf_true);
2119 if (op_ret == -1) {
2120 op_errno = errno;
2121 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LSTAT_FAILED,
2122 "post-operation lstat on parent %s failed", par_newpath);
2123 goto out;
2124 }
2125
2126 posix_set_parent_ctime(frame, this, par_newpath, -1, newloc->parent,
2127 &postnewparent);
2128
2129 if (was_present) {
2130 unwind_dict = posix_dict_set_nlink(xdata, unwind_dict, nlink);
2131 int ret = dict_set_int64(unwind_dict, GF_GET_FILE_BLOCK_COUNT, blocks);
2132 if (IS_ERROR(ret)) {
2133 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_DICT_SET_FAILED,
2134 "%s: dict set of Block size failed", par_newpath);
2135 }
2136 }
2137 op_ret = 0;
2138 out:
2139
2140 SET_TO_OLD_FS_ID();
2141
2142 STACK_UNWIND_STRICT(rename, frame, op_ret, op_errno, &stbuf, &preoldparent,
2143 &postoldparent, &prenewparent, &postnewparent,
2144 unwind_dict);
2145
2146 if (unwind_dict)
2147 dict_unref(unwind_dict);
2148
2149 return 0;
2150 }
2151
2152 int
2153 posix_link(call_frame_t *frame, xlator_t *this, loc_t *oldloc, loc_t *newloc,
2154 dict_t *xdata)
2155 {
2156 int32_t op_ret = -1;
2157 int32_t op_errno = 0;
2158 char *real_oldpath = 0;
2159 char *real_newpath = 0;
2160 char *par_newpath = 0;
2161 struct iatt stbuf = {
2162 0,
2163 };
2164 struct posix_private *priv = NULL;
2165 struct iatt preparent = {
2166 0,
2167 };
2168 struct iatt postparent = {
2169 0,
2170 };
2171 int32_t nlink_samepgfid = 0;
2172 char *pgfid_xattr_key = NULL;
2173 gf_boolean_t entry_created = _gf_false;
2174 posix_inode_ctx_t *ctx = NULL;
2175
2176 DECLARE_OLD_FS_ID_VAR;
2177
2178 VALIDATE_OR_GOTO(frame, out);
2179 VALIDATE_OR_GOTO(this, out);
2180 VALIDATE_OR_GOTO(oldloc, out);
2181 VALIDATE_OR_GOTO(newloc, out);
2182
2183 priv = this->private;
2184 VALIDATE_OR_GOTO(priv, out);
2185 DISK_SPACE_CHECK_AND_GOTO(frame, priv, xdata, op_ret, op_errno, out);
2186
2187 SET_FS_ID(frame->root->uid, frame->root->gid);
2188 MAKE_INODE_HANDLE(real_oldpath, this, oldloc, &stbuf);
2189 if (!real_oldpath) {
2190 op_errno = errno;
2191 goto out;
2192 }
2193
2194 if (priv->max_hardlinks && stbuf.ia_nlink >= priv->max_hardlinks) {
2195 op_ret = -1;
2196 op_errno = EMLINK;
2197 gf_log(this->name, GF_LOG_ERROR,
2198 "hardlink failed: %s exceeds max link count (%u/%u).",
2199 real_oldpath, stbuf.ia_nlink, priv->max_hardlinks);
2200 goto out;
2201 }
2202
2203 MAKE_ENTRY_HANDLE(real_newpath, par_newpath, this, newloc, &stbuf);
2204 if (!real_newpath || !par_newpath) {
2205 op_ret = -1;
2206 op_errno = ESTALE;
2207 goto out;
2208 }
2209
2210 op_ret = posix_pstat(this, newloc->parent, newloc->pargfid, par_newpath,
2211 &preparent, _gf_false, _gf_true);
2212 if (op_ret == -1) {
2213 op_errno = errno;
2214 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LSTAT_FAILED,
2215 "lstat failed: %s", par_newpath);
2216 goto out;
2217 }
2218
2219 real_op:
2220 op_ret = sys_link(real_oldpath, real_newpath);
2221
2222 if (op_ret == -1) {
2223 op_errno = errno;
2224 if (op_errno == EEXIST) {
2225 if (dict_get_sizen(xdata, GF_FORCE_REPLACE_KEY)) {
2226 dict_del_sizen(xdata, GF_FORCE_REPLACE_KEY);
2227 op_ret = posix_unlink_stale_linkto(frame, this, real_newpath,
2228 &op_errno, newloc);
2229 if (op_ret == 0)
2230 goto real_op;
2231 }
2232 }
2233 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LINK_FAILED,
2234 "link %s to %s failed", real_oldpath, real_newpath);
2235 goto out;
2236 }
2237
2238 entry_created = _gf_true;
2239
2240 op_ret = posix_pstat(this, newloc->inode, NULL, real_newpath, &stbuf,
2241 _gf_false, _gf_true);
2242 if (op_ret == -1) {
2243 op_errno = errno;
2244 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LSTAT_FAILED,
2245 "lstat on %s failed", real_newpath);
2246 goto out;
2247 }
2248
2249 posix_set_ctime(frame, this, real_newpath, -1, newloc->inode, &stbuf);
2250
2251 op_ret = posix_pstat(this, newloc->parent, newloc->pargfid, par_newpath,
2252 &postparent, _gf_false, _gf_true);
2253 if (op_ret == -1) {
2254 op_errno = errno;
2255 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LSTAT_FAILED,
2256 "lstat failed: %s", par_newpath);
2257 goto out;
2258 }
2259
2260 posix_set_parent_ctime(frame, this, par_newpath, -1, newloc->parent,
2261 &postparent);
2262
2263 if (priv->update_pgfid_nlinks) {
2264 MAKE_PGFID_XATTR_KEY(pgfid_xattr_key, PGFID_XATTR_KEY_PREFIX,
2265 newloc->pargfid);
2266
2267 op_ret = posix_inode_ctx_get_all(newloc->inode, this, &ctx);
2268 if (op_ret < 0) {
2269 op_errno = ENOMEM;
2270 goto out;
2271 }
2272
2273 pthread_mutex_lock(&ctx->pgfid_lock);
2274 {
2275 LINK_MODIFY_PGFID_XATTR(real_newpath, pgfid_xattr_key,
2276 nlink_samepgfid, 0, op_ret, this, unlock);
2277 }
2278 unlock:
2279 pthread_mutex_unlock(&ctx->pgfid_lock);
2280
2281 if (op_ret < 0) {
2282 gf_msg(this->name, GF_LOG_WARNING, 0, P_MSG_XATTR_FAILED,
2283 "modification of "
2284 "parent gfid xattr failed (path:%s gfid:%s)",
2285 real_newpath, uuid_utoa(newloc->inode->gfid));
2286 goto out;
2287 }
2288 }
2289
2290 if (priv->gfid2path) {
2291 if (stbuf.ia_nlink <= MAX_GFID2PATH_LINK_SUP) {
2292 op_ret = posix_set_gfid2path_xattr(this, real_newpath,
2293 newloc->pargfid, newloc->name);
2294 if (op_ret) {
2295 op_errno = errno;
2296 goto out;
2297 }
2298 } else {
2299 gf_msg(this->name, GF_LOG_INFO, 0, P_MSG_XATTR_NOTSUP,
2300 "Link count exceeded. "
2301 "gfid2path xattr not set (path:%s gfid:%s)",
2302 real_newpath, uuid_utoa(newloc->inode->gfid));
2303 }
2304 }
2305
2306 op_ret = 0;
2307
2308 out:
2309 SET_TO_OLD_FS_ID();
2310
2311 STACK_UNWIND_STRICT(link, frame, op_ret, op_errno,
2312 (oldloc) ? oldloc->inode : NULL, &stbuf, &preparent,
2313 &postparent, NULL);
2314
2315 if (op_ret < 0) {
2316 if (entry_created)
2317 sys_unlink(real_newpath);
2318 }
2319
2320 return 0;
2321 }
2322
2323 int
2324 posix_create(call_frame_t *frame, xlator_t *this, loc_t *loc, int32_t flags,
2325 mode_t mode, mode_t umask, fd_t *fd, dict_t *xdata)
2326 {
2327 int32_t op_ret = -1;
2328 int32_t op_errno = 0;
2329 int32_t _fd = -1;
2330 int _flags = 0;
2331 char *real_path = NULL;
2332 char *par_path = NULL;
2333 struct iatt stbuf = {
2334 0,
2335 };
2336 struct posix_fd *pfd = NULL;
2337 struct posix_private *priv = NULL;
2338 int was_present = 1;
2339
2340 gid_t gid = 0;
2341 struct iatt preparent = {
2342 0,
2343 };
2344 struct iatt postparent = {
2345 0,
2346 };
2347
2348 int nlink_samepgfid = 0;
2349 char *pgfid_xattr_key = NULL;
2350 gf_boolean_t entry_created = _gf_false, gfid_set = _gf_false;
2351 mode_t mode_bit = 0;
2352 uuid_t uuid_req = {
2353 0,
2354 };
2355
2356 dict_t *xdata_rsp = dict_ref(xdata);
2357
2358 DECLARE_OLD_FS_ID_VAR;
2359
2360 VALIDATE_OR_GOTO(frame, out);
2361 VALIDATE_OR_GOTO(this, out);
2362 VALIDATE_OR_GOTO(this->private, out);
2363 VALIDATE_OR_GOTO(loc, out);
2364 VALIDATE_OR_GOTO(fd, out);
2365
2366 priv = this->private;
2367 VALIDATE_OR_GOTO(priv, out);
2368 GFID_NULL_CHECK_AND_GOTO(frame, this, loc, xdata, op_ret, op_errno,
2369 uuid_req, out);
2370 DISK_SPACE_CHECK_AND_GOTO(frame, priv, xdata, op_ret, op_errno, out);
2371
2372 MAKE_ENTRY_HANDLE(real_path, par_path, this, loc, &stbuf);
2373
2374 gid = frame->root->gid;
2375
2376 SET_FS_ID(frame->root->uid, gid);
2377 if (!real_path || !par_path) {
2378 op_ret = -1;
2379 op_errno = ESTALE;
2380 goto out;
2381 }
2382
2383 op_ret = posix_pstat(this, loc->parent, loc->pargfid, par_path, &preparent,
2384 _gf_false, _gf_true);
2385 if (op_ret == -1) {
2386 op_errno = errno;
2387 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LSTAT_FAILED,
2388 "pre-operation lstat on parent %s failed", par_path);
2389 goto out;
2390 }
2391
2392 if (preparent.ia_prot.sgid) {
2393 gid = preparent.ia_gid;
2394 }
2395
2396 if (!flags) {
2397 _flags = O_CREAT | O_RDWR | O_EXCL;
2398 } else {
2399 _flags = flags | O_CREAT;
2400 }
2401
2402 op_ret = posix_pstat(this, loc->inode, NULL, real_path, &stbuf, _gf_false,
2403 _gf_false);
2404 if ((op_ret == -1) && (errno == ENOENT)) {
2405 was_present = 0;
2406 if (posix_is_layout_stale(xdata, par_path, this)) {
2407 op_ret = -1;
2408 op_errno = EIO;
2409 if (!xdata_rsp) {
2410 xdata_rsp = dict_new();
2411 if (!xdata_rsp) {
2412 op_errno = ENOMEM;
2413 goto out;
2414 }
2415 }
2416
2417 if (dict_set_int32_sizen(xdata_rsp, GF_PREOP_CHECK_FAILED, 1) ==
2418 -1) {
2419 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_DICT_SET_FAILED,
2420 "setting key %s in dict failed", GF_PREOP_CHECK_FAILED);
2421 }
2422
2423 goto out;
2424 }
2425 }
2426
2427 if (priv->o_direct)
2428 _flags |= O_DIRECT;
2429
2430 mode_bit = (priv->create_mask & mode) | priv->force_create_mode;
2431 mode = posix_override_umask(mode, mode_bit);
2432 _fd = sys_open(real_path, _flags, mode);
2433
2434 if (_fd == -1) {
2435 op_errno = errno;
2436 op_ret = -1;
2437 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_OPEN_FAILED,
2438 "open on %s failed", real_path);
2439 goto out;
2440 }
2441
2442 if ((_flags & O_CREAT) && (_flags & O_EXCL)) {
2443 entry_created = _gf_true;
2444 }
2445
2446 if (was_present)
2447 goto fill_stat;
2448
2449 #ifndef HAVE_SET_FSID
2450 op_ret = sys_chown(real_path, frame->root->uid, gid);
2451 if (op_ret == -1) {
2452 op_errno = errno;
2453 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_CHOWN_FAILED,
2454 "chown on %s failed", real_path);
2455 }
2456 #endif
2457 op_ret = posix_acl_xattr_set(real_path, xdata);
2458 if (op_ret) {
2459 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_ACL_FAILED,
2460 "setting ACLs on %s failed", real_path);
2461 }
2462
2463 if (priv->update_pgfid_nlinks) {
2464 MAKE_PGFID_XATTR_KEY(pgfid_xattr_key, PGFID_XATTR_KEY_PREFIX,
2465 loc->pargfid);
2466 nlink_samepgfid = 1;
2467 SET_PGFID_XATTR(real_path, pgfid_xattr_key, nlink_samepgfid,
2468 XATTR_CREATE, op_ret, this, ignore);
2469 }
2470
2471 if (priv->gfid2path) {
2472 posix_set_gfid2path_xattr(this, real_path, loc->pargfid, loc->name);
2473 }
2474 ignore:
2475 op_ret = posix_entry_create_xattr_set(this, loc, real_path, xdata);
2476 if (op_ret) {
2477 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_XATTR_FAILED,
2478 "setting xattrs on %s failed ", real_path);
2479 }
2480
2481 fill_stat:
2482 op_ret = posix_gfid_set(this, real_path, loc, xdata, frame->root->pid,
2483 &op_errno);
2484 if (op_ret) {
2485 gf_msg(this->name, GF_LOG_ERROR, op_errno, P_MSG_GFID_FAILED,
2486 "setting gfid on %s failed", real_path);
2487 goto out;
2488 } else {
2489 gfid_set = _gf_true;
2490 }
2491
2492 op_ret = posix_fdstat(this, loc->inode, _fd, &stbuf, _gf_true);
2493 if (op_ret == -1) {
2494 op_errno = errno;
2495 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_FSTAT_FAILED,
2496 "fstat on %d failed", _fd);
2497 goto out;
2498 }
2499
2500 posix_set_ctime(frame, this, real_path, -1, loc->inode, &stbuf);
2501
2502 op_ret = posix_pstat(this, loc->parent, loc->pargfid, par_path, &postparent,
2503 _gf_false, _gf_true);
2504 if (op_ret == -1) {
2505 op_errno = errno;
2506 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LSTAT_FAILED,
2507 "post-operation lstat on parent %s failed", par_path);
2508 goto out;
2509 }
2510
2511 posix_set_parent_ctime(frame, this, par_path, -1, loc->parent, &postparent);
2512
2513 op_ret = -1;
2514 pfd = GF_CALLOC(1, sizeof(*pfd), gf_posix_mt_posix_fd);
2515 if (!pfd) {
2516 op_errno = errno;
2517 goto out;
2518 }
2519
2520 pfd->flags = flags;
2521 pfd->fd = _fd;
2522
2523 op_ret = fd_ctx_set(fd, this, (uint64_t)(long)pfd);
2524 if (op_ret)
2525 gf_msg(this->name, GF_LOG_WARNING, 0, P_MSG_FD_PATH_SETTING_FAILED,
2526 "failed to set the fd context path=%s fd=%p", real_path, fd);
2527
2528 op_ret = 0;
2529
2530 out:
2531 SET_TO_OLD_FS_ID();
2532
2533 if (op_ret < 0) {
2534 if (_fd != -1)
2535 sys_close(_fd);
2536
2537 if (entry_created)
2538 sys_unlink(real_path);
2539
2540 if (gfid_set)
2541 posix_gfid_unset(this, xdata);
2542 }
2543
2544 STACK_UNWIND_STRICT(create, frame, op_ret, op_errno, fd,
2545 (loc) ? loc->inode : NULL, &stbuf, &preparent,
2546 &postparent, xdata_rsp);
2547
2548 if (xdata_rsp)
2549 dict_unref(xdata_rsp);
2550
2551 return 0;
2552 }
2553
2554 /* TODO: Ensure atomocity of put, and rollback in case of failure
2555 * One of the ways, is to perform put in the hidden directory
2556 * and rename it to the specified location, if the put was successful
2557 */
2558 int32_t
2559 posix_put(call_frame_t *frame, xlator_t *this, loc_t *loc, mode_t mode,
2560 mode_t umask, uint32_t flags, struct iovec *vector, int32_t count,
2561 off_t offset, struct iobref *iobref, dict_t *xattr, dict_t *xdata)
2562 {
2563 int32_t op_ret = -1;
2564 int32_t op_errno = 0;
2565 fd_t *fd = NULL;
2566 char *real_path = NULL;
2567 char *par_path = NULL;
2568 struct iatt stbuf = {
2569 0,
2570 };
2571 struct iatt preparent = {
2572 0,
2573 };
2574 struct iatt postparent = {
2575 0,
2576 };
2577
2578 MAKE_ENTRY_HANDLE(real_path, par_path, this, loc, &stbuf);
2579
2580 if (!real_path || !par_path) {
2581 op_ret = -1;
2582 op_errno = ESTALE;
2583 goto out;
2584 }
2585
2586 op_ret = posix_pstat(this, loc->parent, loc->pargfid, par_path, &preparent,
2587 _gf_false, _gf_true);
2588 if (op_ret < 0) {
2589 op_errno = errno;
2590 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LSTAT_FAILED,
2591 "pre-operation lstat on parent %s failed", par_path);
2592 goto out;
2593 }
2594 fd = fd_create(loc->inode, getpid());
2595 if (!fd) {
2596 op_ret = -1;
2597 op_errno = ENOMEM;
2598 goto out;
2599 }
2600 fd->flags = flags;
2601
2602 /* No xlators are expected below posix, but we cannot still call
2603 * sys_create() directly here, as posix_create does many other things like
2604 * chmod, setxattr etc. along with sys_create(). But we cannot also directly
2605 * call posix_create() as it calls STACK_UNWIND. Hence using syncop()
2606 */
2607 op_ret = syncop_create(this, loc, flags, mode, fd, &stbuf, xdata, NULL);
2608 if (op_ret < 0) {
2609 op_errno = errno;
2610 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_CREATE_FAILED,
2611 "create of %s failed", loc->path);
2612 goto out;
2613 }
2614
2615 op_ret = posix_pstat(this, loc->parent, loc->pargfid, par_path, &postparent,
2616 _gf_false, _gf_true);
2617 if (op_ret < 0) {
2618 op_errno = errno;
2619 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LSTAT_FAILED,
2620 "post-operation lstat on parent %s failed", par_path);
2621 goto out;
2622 }
2623
2624 op_ret = syncop_writev(this, fd, vector, count, offset, iobref, flags, NULL,
2625 NULL, xdata, NULL);
2626 if (op_ret < 0) {
2627 op_errno = errno;
2628 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_WRITE_FAILED,
2629 "write on file %s failed", loc->path);
2630 goto out;
2631 }
2632
2633 op_ret = syncop_fsetxattr(this, fd, xattr, flags, xdata, NULL);
2634 if (op_ret < 0) {
2635 op_errno = errno;
2636 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_XATTR_FAILED,
2637 "setxattr on file %s failed", loc->path);
2638 goto out;
2639 }
2640
2641 op_ret = syncop_flush(this, fd, xdata, NULL);
2642 if (op_ret < 0) {
2643 op_errno = errno;
2644 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_CLOSE_FAILED,
2645 "setxattr on file %s failed", loc->path);
2646 goto out;
2647 }
2648
2649 op_ret = posix_pstat(this, loc->inode, loc->gfid, real_path, &stbuf,
2650 _gf_false, _gf_true);
2651 if (op_ret < 0) {
2652 op_errno = errno;
2653 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_LSTAT_FAILED,
2654 "post-operation lstat on %s failed", real_path);
2655 goto out;
2656 }
2657 out:
2658 STACK_UNWIND_STRICT(put, frame, op_ret, op_errno, loc->inode, &stbuf,
2659 &preparent, &postparent, NULL);
2660
2661 return 0;
2662 }