Debian Codegraph @

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

raw symbols SCIP
1 /*
2 Copyright (c) 2018 Red Hat, Inc. <http://www.redhat.com>
3 This file is part of GlusterFS.
4
5 This file is licensed to you under your choice of the GNU Lesser
6 General Public License, version 3 or any later version (LGPLv3 or
7 later), or the GNU General Public License, version 2 (GPLv2), in all
8 cases as published by the Free Software Foundation.
9 */
10
11 #include "posix.h"
12 #include "posix-metadata.h"
13 #include "posix-handle.h"
14 #include "posix-messages.h"
15 #include <glusterfs/syscall.h>
16 #include <glusterfs/compat-errno.h>
17 #include <glusterfs/compat.h>
18
19 static int gf_posix_xattr_enotsup_log;
20
21 /* posix_mdata_to_disk converts posix_mdata_t into network byte order to
22 * save it on disk in machine independent format
23 */
24 static inline void
25 posix_mdata_to_disk(posix_mdata_disk_t *out, posix_mdata_t *in)
26 {
27 out->version = in->version;
28 out->flags = htobe64(in->flags);
29
30 out->ctime.tv_sec = htobe64(in->ctime.tv_sec);
31 out->ctime.tv_nsec = htobe64(in->ctime.tv_nsec);
32
33 out->mtime.tv_sec = htobe64(in->mtime.tv_sec);
34 out->mtime.tv_nsec = htobe64(in->mtime.tv_nsec);
35
36 out->atime.tv_sec = htobe64(in->atime.tv_sec);
37 out->atime.tv_nsec = htobe64(in->atime.tv_nsec);
38 }
39
40 /* posix_mdata_from_disk converts posix_mdata_disk_t into host byte order
41 */
42 static inline void
43 posix_mdata_from_disk(posix_mdata_t *out, posix_mdata_disk_t *in)
44 {
45 out->version = in->version;
46 out->flags = be64toh(in->flags);
47
48 out->ctime.tv_sec = be64toh(in->ctime.tv_sec);
49 out->ctime.tv_nsec = be64toh(in->ctime.tv_nsec);
50
51 out->mtime.tv_sec = be64toh(in->mtime.tv_sec);
52 out->mtime.tv_nsec = be64toh(in->mtime.tv_nsec);
53
54 out->atime.tv_sec = be64toh(in->atime.tv_sec);
55 out->atime.tv_nsec = be64toh(in->atime.tv_nsec);
56 }
57
58 void
59 posix_mdata_iatt_from_disk(struct mdata_iatt *out, posix_mdata_disk_t *in)
60 {
61 out->ia_ctime = be64toh(in->ctime.tv_sec);
62 out->ia_ctime_nsec = be64toh(in->ctime.tv_nsec);
63
64 out->ia_mtime = be64toh(in->mtime.tv_sec);
65 out->ia_mtime_nsec = be64toh(in->mtime.tv_nsec);
66
67 out->ia_atime = be64toh(in->atime.tv_sec);
68 out->ia_atime_nsec = be64toh(in->atime.tv_nsec);
69 }
70
71 /* posix_fetch_mdata_xattr fetches the posix_mdata_t from disk */
72 static int
73 posix_fetch_mdata_xattr(xlator_t *this, const char *real_path_arg, int _fd,
74 inode_t *inode, posix_mdata_t *metadata, int *op_errno)
75 {
76 ssize_t size = 256;
77 int op_ret = -1;
78 char *value = NULL;
79 gf_boolean_t fd_based_fop = _gf_false;
80 char *real_path = NULL;
81
82 if (!metadata) {
83 goto out;
84 }
85
86 if (_fd != -1) {
87 fd_based_fop = _gf_true;
88 }
89 if (!(fd_based_fop || real_path_arg)) {
90 GF_VALIDATE_OR_GOTO(this->name, inode, out);
91 MAKE_HANDLE_PATH(real_path, this, inode->gfid, NULL);
92 if (!real_path) {
93 *op_errno = errno;
94 gf_msg(this->name, GF_LOG_WARNING, *op_errno, P_MSG_LSTAT_FAILED,
95 "lstat on gfid %s failed", uuid_utoa(inode->gfid));
96 goto out;
97 }
98 }
99
100 value = GF_MALLOC(size * sizeof(char), gf_posix_mt_char);
101 if (!value) {
102 *op_errno = ENOMEM;
103 goto out;
104 }
105
106 if (fd_based_fop) {
107 size = sys_fgetxattr(_fd, GF_XATTR_MDATA_KEY, value, size);
108 } else if (real_path_arg) {
109 size = sys_lgetxattr(real_path_arg, GF_XATTR_MDATA_KEY, value, size);
110 } else if (real_path) {
111 size = sys_lgetxattr(real_path, GF_XATTR_MDATA_KEY, value, size);
112 }
113
114 if (size == -1) {
115 *op_errno = errno;
116 if (value) {
117 GF_FREE(value);
118 value = NULL;
119 }
120 if ((*op_errno == ENOTSUP) || (*op_errno == ENOSYS)) {
121 GF_LOG_OCCASIONALLY(gf_posix_xattr_enotsup_log, this->name,
122 GF_LOG_WARNING,
123 "Extended attributes not supported"
124 " (try remounting brick with 'user xattr' "
125 "flag)");
126 } else if (*op_errno == ENOATTR || *op_errno == ENODATA) {
127 gf_msg_debug(this->name, 0,
128 "No such attribute:%s for file %s gfid: %s",
129 GF_XATTR_MDATA_KEY,
130 real_path ? real_path
131 : (real_path_arg ? real_path_arg : "null"),
132 inode ? uuid_utoa(inode->gfid) : "null");
133 goto out;
134 }
135
136 if (fd_based_fop) {
137 size = sys_fgetxattr(_fd, GF_XATTR_MDATA_KEY, NULL, 0);
138 } else if (real_path_arg) {
139 size = sys_lgetxattr(real_path_arg, GF_XATTR_MDATA_KEY, NULL, 0);
140 } else if (real_path) {
141 size = sys_lgetxattr(real_path, GF_XATTR_MDATA_KEY, NULL, 0);
142 }
143
144 if (size == -1) { /* give up now and exist with an error */
145 *op_errno = errno;
146 gf_msg(this->name, GF_LOG_ERROR, *op_errno, P_MSG_XATTR_FAILED,
147 "getxattr failed on %s gfid: %s key: %s ",
148 real_path ? real_path
149 : (real_path_arg ? real_path_arg : "null"),
150 inode ? uuid_utoa(inode->gfid) : "null", GF_XATTR_MDATA_KEY);
151 goto out;
152 }
153
154 value = GF_MALLOC(size * sizeof(char), gf_posix_mt_char);
155 if (!value) {
156 *op_errno = ENOMEM;
157 goto out;
158 }
159
160 if (fd_based_fop) {
161 size = sys_fgetxattr(_fd, GF_XATTR_MDATA_KEY, value, size);
162 } else if (real_path_arg) {
163 size = sys_lgetxattr(real_path_arg, GF_XATTR_MDATA_KEY, value,
164 size);
165 } else if (real_path) {
166 size = sys_lgetxattr(real_path, GF_XATTR_MDATA_KEY, value, size);
167 }
168 if (size == -1) {
169 *op_errno = errno;
170 gf_msg(this->name, GF_LOG_ERROR, *op_errno, P_MSG_XATTR_FAILED,
171 "getxattr failed on %s gfid: %s key: %s ",
172 real_path ? real_path
173 : (real_path_arg ? real_path_arg : "null"),
174 inode ? uuid_utoa(inode->gfid) : "null", GF_XATTR_MDATA_KEY);
175 goto out;
176 }
177 }
178 posix_mdata_from_disk(metadata, (posix_mdata_disk_t *)value);
179
180 op_ret = 0;
181 out:
182 if (value)
183 GF_FREE(value);
184 return op_ret;
185 }
186
187 /* posix_store_mdata_xattr stores the posix_mdata_t on disk */
188 static int
189 posix_store_mdata_xattr(xlator_t *this, const char *real_path_arg, int fd,
190 inode_t *inode, posix_mdata_t *metadata)
191 {
192 char *real_path = NULL;
193 int op_ret = 0;
194 gf_boolean_t fd_based_fop = _gf_false;
195 posix_mdata_disk_t disk_metadata;
196
197 if (!metadata) {
198 op_ret = -1;
199 goto out;
200 }
201
202 if (fd != -1) {
203 fd_based_fop = _gf_true;
204 }
205 if (!(fd_based_fop || real_path_arg)) {
206 MAKE_HANDLE_PATH(real_path, this, inode->gfid, NULL);
207 if (!real_path) {
208 gf_msg(this->name, GF_LOG_DEBUG, errno, P_MSG_LSTAT_FAILED,
209 "lstat on gfid %s failed", uuid_utoa(inode->gfid));
210 op_ret = -1;
211 goto out;
212 }
213 }
214
215 /* Set default version as 1 */
216 posix_mdata_to_disk(&disk_metadata, metadata);
217
218 if (fd_based_fop) {
219 op_ret = sys_fsetxattr(fd, GF_XATTR_MDATA_KEY, (void *)&disk_metadata,
220 sizeof(posix_mdata_disk_t), 0);
221 } else if (real_path_arg) {
222 op_ret = sys_lsetxattr(real_path_arg, GF_XATTR_MDATA_KEY,
223 (void *)&disk_metadata,
224 sizeof(posix_mdata_disk_t), 0);
225 } else if (real_path) {
226 op_ret = sys_lsetxattr(real_path, GF_XATTR_MDATA_KEY,
227 (void *)&disk_metadata,
228 sizeof(posix_mdata_disk_t), 0);
229 }
230
231 #ifdef GF_DARWIN_HOST_OS
232 if (real_path_arg) {
233 posix_dump_buffer(this, real_path_arg, GF_XATTR_MDATA_KEY, value, 0);
234 } else if (real_path) {
235 posix_dump_buffer(this, real_path, GF_XATTR_MDATA_KEY, value, 0);
236 }
237 #endif
238 out:
239 if (op_ret < 0) {
240 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_XATTR_FAILED,
241 "file: %s: gfid: %s key:%s ",
242 real_path ? real_path : (real_path_arg ? real_path_arg : "null"),
243 uuid_utoa(inode->gfid), GF_XATTR_MDATA_KEY);
244 }
245 return op_ret;
246 }
247
248 /* _posix_get_mdata_xattr gets posix_mdata_t from inode context. If it fails
249 * to get it from inode context, gets it from disk. This is with out inode lock.
250 */
251 int
252 __posix_get_mdata_xattr(xlator_t *this, const char *real_path, int _fd,
253 inode_t *inode, struct iatt *stbuf)
254 {
255 uint64_t ctx;
256 posix_mdata_t *mdata = NULL;
257 int ret = -1;
258 int op_errno = 0;
259
260 /* Handle readdirp: inode might be null, time attributes should be served
261 * from xattr not from backend's file attributes */
262 if (inode) {
263 ret = __inode_ctx_get1(inode, this, &ctx);
264 if (ret == 0) {
265 mdata = (posix_mdata_t *)(uintptr_t)ctx;
266 }
267 } else {
268 ret = -1;
269 }
270
271 if (ret == -1 || !mdata) {
272 mdata = GF_CALLOC(1, sizeof(posix_mdata_t), gf_posix_mt_mdata_attr);
273 if (!mdata) {
274 gf_msg(this->name, GF_LOG_ERROR, ENOMEM, P_MSG_NOMEM,
275 "Could not allocate mdata. file: %s: gfid: %s",
276 real_path ? real_path : "null",
277 inode ? uuid_utoa(inode->gfid) : "null");
278 ret = -1;
279 goto out;
280 }
281
282 ret = posix_fetch_mdata_xattr(this, real_path, _fd, inode, mdata,
283 &op_errno);
284
285 if (ret == 0) {
286 /* Got mdata from disk, set it in inode ctx. This case
287 * is hit when in-memory status is lost due to brick
288 * down scenario
289 */
290 if (inode) {
291 ctx = (uint64_t)(uintptr_t)mdata;
292 __inode_ctx_set1(inode, this, &ctx);
293 }
294 } else {
295 /* Failed to get mdata from disk, xattr missing.
296 * This happens when the file is created before
297 * ctime is enabled.
298 */
299 if (stbuf && op_errno != ENOENT) {
300 ret = 0;
301 GF_FREE(mdata);
302 goto out;
303 } else {
304 /* This case should not be hit. If it hits,
305 * don't fail, log warning, free mdata and move
306 * on
307 */
308 gf_msg(this->name, GF_LOG_WARNING, op_errno,
309 P_MSG_FETCHMDATA_FAILED, "file: %s: gfid: %s key:%s ",
310 real_path ? real_path : "null",
311 inode ? uuid_utoa(inode->gfid) : "null",
312 GF_XATTR_MDATA_KEY);
313 GF_FREE(mdata);
314 ret = 0;
315 goto out;
316 }
317 }
318 }
319
320 ret = 0;
321
322 if (ret == 0 && stbuf) {
323 stbuf->ia_ctime = mdata->ctime.tv_sec;
324 stbuf->ia_ctime_nsec = mdata->ctime.tv_nsec;
325 stbuf->ia_mtime = mdata->mtime.tv_sec;
326 stbuf->ia_mtime_nsec = mdata->mtime.tv_nsec;
327 stbuf->ia_atime = mdata->atime.tv_sec;
328 stbuf->ia_atime_nsec = mdata->atime.tv_nsec;
329 }
330 /* Not set in inode context, hence free mdata */
331 if (!inode) {
332 GF_FREE(mdata);
333 }
334
335 out:
336 return ret;
337 }
338
339 /* posix_get_mdata_xattr gets posix_mdata_t from inode context. If it fails
340 * to get it from inode context, gets it from disk. This is with inode lock.
341 */
342 int
343 posix_get_mdata_xattr(xlator_t *this, const char *real_path, int _fd,
344 inode_t *inode, struct iatt *stbuf)
345 {
346 int ret = -1;
347
348 GF_VALIDATE_OR_GOTO(this->name, inode, out);
349
350 LOCK(&inode->lock);
351 {
352 ret = __posix_get_mdata_xattr(this, real_path, _fd, inode, stbuf);
353 }
354 UNLOCK(&inode->lock);
355
356 out:
357 return ret;
358 }
359
360 static int
361 posix_compare_timespec(struct timespec *first, struct timespec *second)
362 {
363 if (first->tv_sec == second->tv_sec)
364 return first->tv_nsec - second->tv_nsec;
365 else
366 return first->tv_sec - second->tv_sec;
367 }
368
369 int
370 posix_set_mdata_xattr_legacy_files(xlator_t *this, inode_t *inode,
371 const char *realpath,
372 struct mdata_iatt *mdata_iatt, int *op_errno)
373 {
374 uint64_t ctx;
375 posix_mdata_t *mdata = NULL;
376 posix_mdata_t imdata = {
377 0,
378 };
379 int ret = 0;
380 gf_boolean_t mdata_already_set = _gf_false;
381
382 GF_VALIDATE_OR_GOTO("posix", this, out);
383 GF_VALIDATE_OR_GOTO(this->name, inode, out);
384
385 LOCK(&inode->lock);
386 {
387 ret = __inode_ctx_get1(inode, this, &ctx);
388 if (ret == 0 && ctx) {
389 mdata = (posix_mdata_t *)(uintptr_t)ctx;
390 mdata_already_set = _gf_true;
391 } else {
392 mdata = GF_CALLOC(1, sizeof(posix_mdata_t), gf_posix_mt_mdata_attr);
393 if (!mdata) {
394 gf_msg(this->name, GF_LOG_ERROR, ENOMEM, P_MSG_NOMEM,
395 "Could not allocate mdata. gfid: %s",
396 uuid_utoa(inode->gfid));
397 ret = -1;
398 *op_errno = ENOMEM;
399 goto unlock;
400 }
401
402 ret = posix_fetch_mdata_xattr(this, realpath, -1, inode,
403 (void *)mdata, op_errno);
404 if (ret == 0) {
405 /* Got mdata from disk. This is a race, another client
406 * has healed the xattr during lookup. So set it in inode
407 * ctx */
408 ctx = (uint64_t)(uintptr_t)mdata;
409 __inode_ctx_set1(inode, this, &ctx);
410 mdata_already_set = _gf_true;
411 } else {
412 *op_errno = 0;
413 mdata->version = 1;
414 mdata->flags = 0;
415 mdata->ctime.tv_sec = mdata_iatt->ia_ctime;
416 mdata->ctime.tv_nsec = mdata_iatt->ia_ctime_nsec;
417 mdata->atime.tv_sec = mdata_iatt->ia_atime;
418 mdata->atime.tv_nsec = mdata_iatt->ia_atime_nsec;
419 mdata->mtime.tv_sec = mdata_iatt->ia_mtime;
420 mdata->mtime.tv_nsec = mdata_iatt->ia_mtime_nsec;
421
422 ctx = (uint64_t)(uintptr_t)mdata;
423 __inode_ctx_set1(inode, this, &ctx);
424 }
425 }
426
427 if (mdata_already_set) {
428 /* Compare and update the larger time */
429 imdata.ctime.tv_sec = mdata_iatt->ia_ctime;
430 imdata.ctime.tv_nsec = mdata_iatt->ia_ctime_nsec;
431 imdata.atime.tv_sec = mdata_iatt->ia_atime;
432 imdata.atime.tv_nsec = mdata_iatt->ia_atime_nsec;
433 imdata.mtime.tv_sec = mdata_iatt->ia_mtime;
434 imdata.mtime.tv_nsec = mdata_iatt->ia_mtime_nsec;
435
436 if (posix_compare_timespec(&imdata.ctime, &mdata->ctime) > 0) {
437 mdata->ctime = imdata.ctime;
438 }
439 if (posix_compare_timespec(&imdata.mtime, &mdata->mtime) > 0) {
440 mdata->mtime = imdata.mtime;
441 }
442 if (posix_compare_timespec(&imdata.atime, &mdata->atime) > 0) {
443 mdata->atime = imdata.atime;
444 }
445 }
446
447 ret = posix_store_mdata_xattr(this, realpath, -1, inode, mdata);
448 if (ret) {
449 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_STOREMDATA_FAILED,
450 "gfid: %s key:%s ", uuid_utoa(inode->gfid),
451 GF_XATTR_MDATA_KEY);
452 *op_errno = errno;
453 goto unlock;
454 }
455 }
456 unlock:
457 UNLOCK(&inode->lock);
458 out:
459 return ret;
460 }
461
462 /* posix_set_mdata_xattr updates the posix_mdata_t based on the flag
463 * in inode context and stores it on disk
464 */
465 static int
466 posix_set_mdata_xattr(xlator_t *this, const char *real_path, int fd,
467 inode_t *inode, struct timespec *time,
468 struct timespec *u_atime, struct timespec *u_mtime,
469 struct iatt *stbuf, posix_mdata_flag_t *flag,
470 gf_boolean_t update_utime)
471 {
472 uint64_t ctx;
473 posix_mdata_t *mdata = NULL;
474 int ret = -1;
475 int op_errno = 0;
476
477 GF_VALIDATE_OR_GOTO("posix", this, out);
478 GF_VALIDATE_OR_GOTO(this->name, inode, out);
479 GF_VALIDATE_OR_GOTO(this->name, time, out);
480
481 if (update_utime && (flag->atime && !u_atime) &&
482 (flag->mtime && !u_mtime)) {
483 goto out;
484 }
485
486 LOCK(&inode->lock);
487 {
488 ret = __inode_ctx_get1(inode, this, &ctx);
489 if (ret == 0) {
490 mdata = (posix_mdata_t *)(uintptr_t)ctx;
491 }
492 if (ret == -1 || !mdata) {
493 /*
494 * Do we need to fetch the data from xattr
495 * If we does we can compare the value and store
496 * the largest data in inode ctx.
497 */
498 mdata = GF_CALLOC(1, sizeof(posix_mdata_t), gf_posix_mt_mdata_attr);
499 if (!mdata) {
500 gf_msg(this->name, GF_LOG_ERROR, ENOMEM, P_MSG_NOMEM,
501 "Could not allocate mdata. file: %s: gfid: %s",
502 real_path ? real_path : "null", uuid_utoa(inode->gfid));
503 ret = -1;
504 goto unlock;
505 }
506
507 ret = posix_fetch_mdata_xattr(this, real_path, fd, inode,
508 (void *)mdata, &op_errno);
509 if (ret == 0) {
510 /* Got mdata from disk, set it in inode ctx. This case
511 * is hit when in-memory status is lost due to brick
512 * down scenario
513 */
514 ctx = (uint64_t)(uintptr_t)mdata;
515 __inode_ctx_set1(inode, this, &ctx);
516 } else {
517 /*
518 * This is the first time creating the time attr. This happens
519 * when you activate this feature. On this code path, only new
520 * files will create mdata xattr. The legacy files (files
521 * created before ctime enabled) will not have any xattr set.
522 * The xattr on legacy file will be set via lookup.
523 */
524
525 /* Don't create xattr with utimes/utimensat, only update if
526 * present. This otherwise causes issues during inservice
527 * upgrade. It causes inconsistent xattr values with in replica
528 * set. The scenario happens during upgrade where clients are
529 * older versions (without the ctime feature) and the server is
530 * upgraded to the new version (with the ctime feature which
531 * is enabled by default).
532 */
533
534 if (update_utime) {
535 UNLOCK(&inode->lock);
536 GF_FREE(mdata);
537 return 0;
538 }
539
540 mdata->version = 1;
541 mdata->flags = 0;
542 mdata->ctime.tv_sec = time->tv_sec;
543 mdata->ctime.tv_nsec = time->tv_nsec;
544 mdata->atime.tv_sec = time->tv_sec;
545 mdata->atime.tv_nsec = time->tv_nsec;
546 mdata->mtime.tv_sec = time->tv_sec;
547 mdata->mtime.tv_nsec = time->tv_nsec;
548
549 ctx = (uint64_t)(uintptr_t)mdata;
550 __inode_ctx_set1(inode, this, &ctx);
551 }
552 }
553
554 /* In distributed systems, there could be races with fops
555 * updating mtime/atime which could result in different
556 * mtime/atime for same file. So this makes sure, only the
557 * highest time is retained. If the mtime/atime update comes
558 * from the explicit utime syscall, it is allowed to set to
559 * previous or future time but the ctime is always set to
560 * current time.
561 */
562 if (update_utime) {
563 if (flag->ctime &&
564 posix_compare_timespec(time, &mdata->ctime) > 0) {
565 mdata->ctime = *time;
566 }
567 if (flag->mtime) {
568 mdata->mtime = *u_mtime;
569 }
570 if (flag->atime) {
571 mdata->atime = *u_atime;
572 }
573 } else {
574 if (flag->ctime &&
575 posix_compare_timespec(time, &mdata->ctime) > 0) {
576 mdata->ctime = *time;
577 }
578 if (flag->mtime &&
579 posix_compare_timespec(time, &mdata->mtime) > 0) {
580 mdata->mtime = *time;
581 }
582 if (flag->atime &&
583 posix_compare_timespec(time, &mdata->atime) > 0) {
584 mdata->atime = *time;
585 }
586 }
587
588 if (inode->ia_type == IA_INVAL) {
589 /*
590 * TODO: This is non-linked inode. So we have to sync the
591 * data into backend. Because inode_link may return
592 * a different inode.
593 */
594 /* ret = posix_store_mdata_xattr (this, loc, fd,
595 * mdata); */
596 }
597 /*
598 * With this patch set, we are setting the xattr for each update
599 * We should evaluate the performance, and based on that we can
600 * decide on asynchronous updation.
601 */
602 ret = posix_store_mdata_xattr(this, real_path, fd, inode, mdata);
603 if (ret) {
604 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_STOREMDATA_FAILED,
605 "file: %s: gfid: %s key:%s ", real_path ? real_path : "null",
606 uuid_utoa(inode->gfid), GF_XATTR_MDATA_KEY);
607 goto unlock;
608 }
609 }
610 unlock:
611 UNLOCK(&inode->lock);
612 out:
613 if (ret == 0 && stbuf) {
614 stbuf->ia_ctime = mdata->ctime.tv_sec;
615 stbuf->ia_ctime_nsec = mdata->ctime.tv_nsec;
616 stbuf->ia_mtime = mdata->mtime.tv_sec;
617 stbuf->ia_mtime_nsec = mdata->mtime.tv_nsec;
618 stbuf->ia_atime = mdata->atime.tv_sec;
619 stbuf->ia_atime_nsec = mdata->atime.tv_nsec;
620 }
621
622 return ret;
623 }
624
625 /* posix_update_utime_in_mdata updates the posix_mdata_t when mtime/atime
626 * is modified using syscall
627 */
628 void
629 posix_update_utime_in_mdata(xlator_t *this, const char *real_path, int fd,
630 inode_t *inode, struct timespec *ctime,
631 struct iatt *stbuf, int valid)
632 {
633 int32_t ret = 0;
634 #if defined(HAVE_UTIMENSAT)
635 struct timespec tv_atime = {
636 0,
637 };
638 struct timespec tv_mtime = {
639 0,
640 };
641 #else
642 struct timeval tv_atime = {
643 0,
644 };
645 struct timeval tv_mtime = {
646 0,
647 };
648 #endif
649 posix_mdata_flag_t flag = {
650 0,
651 };
652
653 struct posix_private *priv = NULL;
654
655 priv = this->private;
656
657 /* NOTE:
658 * This routine (utimes) is intentionally allowed for all internal and
659 * external clients even if ctime is not set. This is because AFR and
660 * WORM uses time attributes for it's internal operations
661 */
662 if (inode && priv->ctime) {
663 if ((valid & GF_SET_ATTR_ATIME) == GF_SET_ATTR_ATIME) {
664 tv_atime.tv_sec = stbuf->ia_atime;
665 SET_TIMESPEC_NSEC_OR_TIMEVAL_USEC(tv_atime, stbuf->ia_atime_nsec);
666
667 flag.ctime = 1;
668 flag.atime = 1;
669 }
670
671 if ((valid & GF_SET_ATTR_MTIME) == GF_SET_ATTR_MTIME) {
672 tv_mtime.tv_sec = stbuf->ia_mtime;
673 SET_TIMESPEC_NSEC_OR_TIMEVAL_USEC(tv_mtime, stbuf->ia_mtime_nsec);
674
675 flag.ctime = 1;
676 flag.mtime = 1;
677 }
678
679 if (flag.mtime || flag.atime) {
680 ret = posix_set_mdata_xattr(this, real_path, -1, inode, ctime,
681 &tv_atime, &tv_mtime, NULL, &flag,
682 _gf_true);
683 if (ret) {
684 gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_SETMDATA_FAILED,
685 "posix set mdata atime failed on file:"
686 " %s gfid:%s",
687 real_path, uuid_utoa(inode->gfid));
688 }
689 }
690 }
691 return;
692 }
693
694 /* posix_update_ctime_in_mdata updates the posix_mdata_t when ctime needs
695 * to be modified
696 */
697 void
698 posix_update_ctime_in_mdata(xlator_t *this, const char *real_path, int fd,
699 inode_t *inode, struct timespec *ctime,
700 struct iatt *stbuf, int valid)
701 {
702 int32_t ret = 0;
703 #if defined(HAVE_UTIMENSAT)
704 struct timespec tv_ctime = {
705 0,
706 };
707 #else
708 struct timeval tv_ctime = {
709 0,
710 };
711 #endif
712 posix_mdata_flag_t flag = {
713 0,
714 };
715
716 struct posix_private *priv = NULL;
717 priv = this->private;
718
719 if (inode && priv->ctime) {
720 tv_ctime.tv_sec = stbuf->ia_ctime;
721 SET_TIMESPEC_NSEC_OR_TIMEVAL_USEC(tv_ctime, stbuf->ia_ctime_nsec);
722 flag.ctime = 1;
723
724 ret = posix_set_mdata_xattr(this, real_path, -1, inode, &tv_ctime, NULL,
725 NULL, NULL, &flag, _gf_true);
726 if (ret) {
727 gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_SETMDATA_FAILED,
728 "posix set mdata atime failed on file:"
729 " %s gfid:%s",
730 real_path, uuid_utoa(inode->gfid));
731 }
732 }
733 return;
734 }
735
736 static void
737 posix_get_mdata_flag(uint64_t flags, posix_mdata_flag_t *flag)
738 {
739 if (!flag)
740 return;
741
742 flag->ctime = 0;
743 flag->atime = 0;
744 flag->mtime = 0;
745
746 if (flags & MDATA_CTIME)
747 flag->ctime = 1;
748 if (flags & MDATA_MTIME)
749 flag->mtime = 1;
750 if (flags & MDATA_ATIME)
751 flag->atime = 1;
752 }
753
754 static void
755 posix_get_parent_mdata_flag(uint64_t flags, posix_mdata_flag_t *flag)
756 {
757 if (!flag)
758 return;
759
760 flag->ctime = 0;
761 flag->atime = 0;
762 flag->mtime = 0;
763
764 if (flags & MDATA_PAR_CTIME)
765 flag->ctime = 1;
766 if (flags & MDATA_PAR_MTIME)
767 flag->mtime = 1;
768 if (flags & MDATA_PAR_ATIME)
769 flag->atime = 1;
770 }
771
772 void
773 posix_set_ctime(call_frame_t *frame, xlator_t *this, const char *real_path,
774 int fd, inode_t *inode, struct iatt *stbuf)
775 {
776 posix_mdata_flag_t flag = {
777 0,
778 };
779 int ret = 0;
780 struct posix_private *priv = NULL;
781
782 priv = this->private;
783
784 if (priv->ctime) {
785 (void)posix_get_mdata_flag(frame->root->flags, &flag);
786 if ((flag.ctime == 0) && (flag.mtime == 0) && (flag.atime == 0)) {
787 goto out;
788 }
789 ret = posix_set_mdata_xattr(this, real_path, fd, inode,
790 &frame->root->ctime, NULL, NULL, stbuf,
791 &flag, _gf_false);
792 if (ret) {
793 gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_SETMDATA_FAILED,
794 "posix set mdata failed on file: %s gfid:%s", real_path,
795 inode ? uuid_utoa(inode->gfid) : "No inode");
796 }
797 }
798 out:
799 return;
800 }
801
802 void
803 posix_set_parent_ctime(call_frame_t *frame, xlator_t *this,
804 const char *real_path, int fd, inode_t *inode,
805 struct iatt *stbuf)
806 {
807 posix_mdata_flag_t flag = {
808 0,
809 };
810 int ret = 0;
811 struct posix_private *priv = NULL;
812
813 priv = this->private;
814
815 if (inode && priv->ctime) {
816 (void)posix_get_parent_mdata_flag(frame->root->flags, &flag);
817 if ((flag.ctime == 0) && (flag.mtime == 0) && (flag.atime == 0)) {
818 goto out;
819 }
820 ret = posix_set_mdata_xattr(this, real_path, fd, inode,
821 &frame->root->ctime, NULL, NULL, stbuf,
822 &flag, _gf_false);
823 if (ret) {
824 gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_SETMDATA_FAILED,
825 "posix set mdata failed on file: %s gfid:%s", real_path,
826 uuid_utoa(inode->gfid));
827 }
828 }
829 out:
830 return;
831 }
832
833 void
834 posix_set_ctime_cfr(call_frame_t *frame, xlator_t *this,
835 const char *real_path_in, int fd_in, inode_t *inode_in,
836 struct iatt *stbuf_in, const char *real_path_out,
837 int fd_out, inode_t *inode_out, struct iatt *stbuf_out)
838 {
839 posix_mdata_flag_t flag = {
840 0,
841 };
842 posix_mdata_flag_t flag_dup = {
843 0,
844 };
845 int ret = 0;
846 struct posix_private *priv = NULL;
847 char in_uuid_str[64] = {0}, out_uuid_str[64] = {0};
848
849 priv = this->private;
850
851 if (priv->ctime) {
852 (void)posix_get_mdata_flag(frame->root->flags, &flag);
853 if ((flag.ctime == 0) && (flag.mtime == 0) && (flag.atime == 0)) {
854 goto out;
855 }
856
857 if (frame->root->ctime.tv_sec == 0) {
858 gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_SETMDATA_FAILED,
859 "posix set mdata failed, No ctime : in: %s gfid_in:%s "
860 "out: %s gfid_out:%s",
861 real_path_in,
862 (inode_in ? uuid_utoa_r(inode_in->gfid, in_uuid_str)
863 : "No inode"),
864 real_path_out,
865 (inode_out ? uuid_utoa_r(inode_out->gfid, out_uuid_str)
866 : "No inode"));
867 goto out;
868 }
869
870 flag_dup = flag;
871
872 /*
873 * For the destination file, no need to update atime.
874 * It got modified. Hence the things that need to be
875 * changed are mtime and ctime (provided the utime
876 * xlator from the client has set those flags, which
877 * are just copied to flag_dup).
878 */
879 if (flag.atime)
880 flag_dup.atime = 0;
881
882 ret = posix_set_mdata_xattr(this, real_path_out, fd_out, inode_out,
883 &frame->root->ctime, NULL, NULL, stbuf_out,
884 &flag_dup, _gf_false);
885 if (ret) {
886 gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_SETMDATA_FAILED,
887 "posix set mdata failed on file: %s gfid:%s", real_path_out,
888 inode_out ? uuid_utoa(inode_out->gfid) : "No inode");
889 }
890
891 /*
892 * For the source file, no need to change the mtime and ctime.
893 * For source file, it is only read operation. So, if at all
894 * anything needs to be updated, it is only the atime.
895 */
896 if (flag.atime)
897 flag_dup.atime = flag.atime;
898 flag_dup.mtime = 0;
899 flag_dup.ctime = 0;
900
901 ret = posix_set_mdata_xattr(this, real_path_in, fd_out, inode_out,
902 &frame->root->ctime, NULL, NULL, stbuf_out,
903 &flag_dup, _gf_false);
904 if (ret) {
905 gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_SETMDATA_FAILED,
906 "posix set mdata failed on file: %s gfid:%s", real_path_in,
907 inode_in ? uuid_utoa(inode_in->gfid) : "No inode");
908 }
909 }
910 out:
911 return;
912 }