Skip to content

Commit

Permalink
Linux 4.18 compat: inode timespec -> timespec64
Browse files Browse the repository at this point in the history
Commit torvalds/linux@95582b0 changes the inode i_atime, i_mtime,
and i_ctime members form timespec's to timespec64's to make them
2038 safe.  As part of this change the current_time() function was
also updated to return the timespec64 type.

Resolve this issue by introducing a new inode_timespec_t type which
is defined to match the timespec type used by the inode.  It should
be used when working with inode timestamps to ensure matching types.

The timestruc_t type under Illumos was used in a similar fashion but
was specified to always be a timespec_t.  Rather than incorrectly
define this type all timespec_t types have been replaced by the new
inode_timespec_t type.

Finally, the kernel and user space 'sys/time.h' headers were aligned
with each other.  They define as appropriate for the context several
constants as macros and include static inline implementation of
gethrestime(), gethrestime_sec(), and gethrtime().

Reviewed-by: Chunwei Chen <tuxoko@gmail.com>
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Closes openzfs#7643
  • Loading branch information
behlendorf authored Jun 20, 2018
1 parent aeb39df commit 6413c95
Show file tree
Hide file tree
Showing 27 changed files with 136 additions and 145 deletions.
7 changes: 3 additions & 4 deletions config/kernel-current-time.m4
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
dnl #
dnl # 4.9, current_time() added
dnl # 4.18, return type changed from timespec to timespec64
dnl #
AC_DEFUN([ZFS_AC_KERNEL_CURRENT_TIME],
[AC_MSG_CHECKING([whether current_time() exists])
ZFS_LINUX_TRY_COMPILE_SYMBOL([
#include <linux/fs.h>
], [
struct inode ip;
struct timespec now __attribute__ ((unused));
now = current_time(&ip);
struct inode ip __attribute__ ((unused));
ip.i_atime = current_time(&ip);
], [current_time], [fs/inode.c], [
AC_MSG_RESULT(yes)
AC_DEFINE(HAVE_CURRENT_TIME, 1, [current_time() exists])
Expand Down
25 changes: 25 additions & 0 deletions config/kernel-inode-times.m4
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
dnl #
dnl # 4.18 API change
dnl # i_atime, i_mtime, and i_ctime changed from timespec to timespec64.
dnl #
AC_DEFUN([ZFS_AC_KERNEL_INODE_TIMES], [
AC_MSG_CHECKING([whether inode->i_*time's are timespec64])
tmp_flags="$EXTRA_KCFLAGS"
EXTRA_KCFLAGS="-Werror"
ZFS_LINUX_TRY_COMPILE([
#include <linux/fs.h>
],[
struct inode ip;
struct timespec ts;
memset(&ip, 0, sizeof(ip));
ts = ip.i_mtime;
],[
AC_MSG_RESULT(no)
],[
AC_MSG_RESULT(yes)
AC_DEFINE(HAVE_INODE_TIMESPEC64_TIMES, 1,
[inode->i_*time's are timespec64])
])
EXTRA_KCFLAGS="$tmp_flags"
])
1 change: 1 addition & 0 deletions config/kernel.m4
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ AC_DEFUN([ZFS_AC_CONFIG_KERNEL], [
ZFS_AC_KERNEL_WAIT_ON_BIT
ZFS_AC_KERNEL_WAIT_QUEUE_ENTRY_T
ZFS_AC_KERNEL_WAIT_QUEUE_HEAD_ENTRY
ZFS_AC_KERNEL_INODE_TIMES
ZFS_AC_KERNEL_INODE_LOCK
ZFS_AC_KERNEL_GROUP_INFO_GID
ZFS_AC_KERNEL_WRITE
Expand Down
1 change: 1 addition & 0 deletions include/spl/sys/condvar.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <sys/mutex.h>
#include <sys/callo.h>
#include <sys/wait.h>
#include <sys/time.h>

/*
* The kcondvar_t struct is protected by mutex taken externally before
Expand Down
23 changes: 18 additions & 5 deletions include/spl/sys/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,26 @@

static const int hz = HZ;

typedef longlong_t hrtime_t;
typedef struct timespec timespec_t;

#define TIMESPEC_OVERFLOW(ts) \
((ts)->tv_sec < TIME_MIN || (ts)->tv_sec > TIME_MAX)

#if defined(HAVE_INODE_TIMESPEC64_TIMES)
typedef struct timespec64 inode_timespec_t;
#else
typedef struct timespec inode_timespec_t;
#endif

static inline void
gethrestime(timestruc_t *now)
gethrestime(inode_timespec_t *ts)
{
*now = current_kernel_time();
#if defined(HAVE_INODE_TIMESPEC64_TIMES)
*ts = current_kernel_time64();
#else
*ts = current_kernel_time();
#endif
}

static inline time_t
Expand All @@ -74,9 +87,9 @@ gethrestime_sec(void)
static inline hrtime_t
gethrtime(void)
{
struct timespec now;
getrawmonotonic(&now);
return (((hrtime_t)now.tv_sec * NSEC_PER_SEC) + now.tv_nsec);
struct timespec ts;
getrawmonotonic(&ts);
return (((hrtime_t)ts.tv_sec * NSEC_PER_SEC) + ts.tv_nsec);
}

#endif /* _SPL_TIME_H */
4 changes: 0 additions & 4 deletions include/spl/sys/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ typedef unsigned long long rlim64_t;
typedef struct task_struct kthread_t;
typedef struct task_struct proc_t;

typedef struct timespec timestruc_t;
typedef struct timespec timespec_t;
typedef longlong_t hrtime_t;

typedef int id_t;
typedef short pri_t;
typedef short index_t;
Expand Down
6 changes: 3 additions & 3 deletions include/spl/sys/vnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ typedef struct vattr {
long va_nodeid; /* node # */
uint32_t va_nlink; /* # links */
uint64_t va_size; /* file size */
struct timespec va_atime; /* last acc */
struct timespec va_mtime; /* last mod */
struct timespec va_ctime; /* last chg */
inode_timespec_t va_atime; /* last acc */
inode_timespec_t va_mtime; /* last mod */
inode_timespec_t va_ctime; /* last chg */
dev_t va_rdev; /* dev */
uint64_t va_nblocks; /* space used */
uint32_t va_blksize; /* block size */
Expand Down
2 changes: 1 addition & 1 deletion include/sys/dmu.h
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,7 @@ uint64_t dmu_objset_fsid_guid(objset_t *os);
/*
* Get the [cm]time for an objset's snapshot dir
*/
timestruc_t dmu_objset_snap_cmtime(objset_t *os);
inode_timespec_t dmu_objset_snap_cmtime(objset_t *os);

int dmu_objset_is_snapshot(objset_t *os);

Expand Down
2 changes: 1 addition & 1 deletion include/sys/dmu_objset.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ int dmu_objset_find_dp(struct dsl_pool *dp, uint64_t ddobj,
int func(struct dsl_pool *, struct dsl_dataset *, void *),
void *arg, int flags);
void dmu_objset_evict_dbufs(objset_t *os);
timestruc_t dmu_objset_snap_cmtime(objset_t *os);
inode_timespec_t dmu_objset_snap_cmtime(objset_t *os);

/* called from dsl */
void dmu_objset_sync(objset_t *os, zio_t *zio, dmu_tx_t *tx);
Expand Down
4 changes: 2 additions & 2 deletions include/sys/dsl_dir.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ struct dsl_dir {
/* Protected by dd_lock */
kmutex_t dd_lock;
list_t dd_props; /* list of dsl_prop_record_t's */
timestruc_t dd_snap_cmtime; /* last time snapshot namespace changed */
inode_timespec_t dd_snap_cmtime; /* last snapshot namespace change */
uint64_t dd_origin_txg;

/* gross estimate of space used by in-flight tx's */
Expand Down Expand Up @@ -180,7 +180,7 @@ boolean_t dsl_dir_is_clone(dsl_dir_t *dd);
void dsl_dir_new_refreservation(dsl_dir_t *dd, struct dsl_dataset *ds,
uint64_t reservation, cred_t *cr, dmu_tx_t *tx);
void dsl_dir_snap_cmtime_update(dsl_dir_t *dd);
timestruc_t dsl_dir_snap_cmtime(dsl_dir_t *dd);
inode_timespec_t dsl_dir_snap_cmtime(dsl_dir_t *dd);
void dsl_dir_set_reservation_sync_impl(dsl_dir_t *dd, uint64_t value,
dmu_tx_t *tx);
void dsl_dir_zapify(dsl_dir_t *dd, dmu_tx_t *tx);
Expand Down
2 changes: 1 addition & 1 deletion include/sys/spa_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ struct spa {
uint64_t spa_freeze_txg; /* freeze pool at this txg */
uint64_t spa_load_max_txg; /* best initial ub_txg */
uint64_t spa_claim_max_txg; /* highest claimed birth txg */
timespec_t spa_loaded_ts; /* 1st successful open time */
inode_timespec_t spa_loaded_ts; /* 1st successful open time */
objset_t *spa_meta_objset; /* copy of dp->dp_meta_objset */
kmutex_t spa_evicting_os_lock; /* Evicting objset list lock */
list_t spa_evicting_os_list; /* Objsets being evicted. */
Expand Down
2 changes: 1 addition & 1 deletion include/sys/xvattr.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
* Structure of all optional attributes.
*/
typedef struct xoptattr {
timestruc_t xoa_createtime; /* Create time of file */
inode_timespec_t xoa_createtime; /* Create time of file */
uint8_t xoa_archive;
uint8_t xoa_system;
uint8_t xoa_readonly;
Expand Down
9 changes: 1 addition & 8 deletions include/sys/zfs_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ extern char *vn_dumpdir;
#define AV_SCANSTAMP_SZ 32 /* length of anti-virus scanstamp */

typedef struct xoptattr {
timestruc_t xoa_createtime; /* Create time of file */
inode_timespec_t xoa_createtime; /* Create time of file */
uint8_t xoa_archive;
uint8_t xoa_system;
uint8_t xoa_readonly;
Expand Down Expand Up @@ -596,13 +596,6 @@ extern void delay(clock_t ticks);
#define USEC_TO_TICK(usec) ((usec) / (MICROSEC / hz))
#define NSEC_TO_TICK(usec) ((usec) / (NANOSEC / hz))

#define gethrestime_sec() time(NULL)
#define gethrestime(t) \
do {\
(t)->tv_sec = gethrestime_sec();\
(t)->tv_nsec = 0;\
} while (0);

#define max_ncpus 64
#define boot_ncpus (sysconf(_SC_NPROCESSORS_ONLN))

Expand Down
33 changes: 25 additions & 8 deletions include/sys/zfs_znode.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,19 +291,36 @@ zfs_inherit_projid(znode_t *dzp)

extern unsigned int zfs_object_mutex_size;

/* Encode ZFS stored time values from a struct timespec */
/*
* Encode ZFS stored time values from a struct timespec / struct timespec64.
*/
#define ZFS_TIME_ENCODE(tp, stmp) \
{ \
do { \
(stmp)[0] = (uint64_t)(tp)->tv_sec; \
(stmp)[1] = (uint64_t)(tp)->tv_nsec; \
}
} while (0)

/* Decode ZFS stored time values to a struct timespec */
#if defined(HAVE_INODE_TIMESPEC64_TIMES)
/*
* Decode ZFS stored time values to a struct timespec64
* 4.18 and newer kernels.
*/
#define ZFS_TIME_DECODE(tp, stmp) \
{ \
(tp)->tv_sec = (time_t)(stmp)[0]; \
(tp)->tv_nsec = (long)(stmp)[1]; \
}
do { \
(tp)->tv_sec = (time64_t)(stmp)[0]; \
(tp)->tv_nsec = (long)(stmp)[1]; \
} while (0)
#else
/*
* Decode ZFS stored time values to a struct timespec
* 4.17 and older kernels.
*/
#define ZFS_TIME_DECODE(tp, stmp) \
do { \
(tp)->tv_sec = (time_t)(stmp)[0]; \
(tp)->tv_nsec = (long)(stmp)[1]; \
} while (0)
#endif /* HAVE_INODE_TIMESPEC64_TIMES */

/*
* Timestamp defines
Expand Down
9 changes: 9 additions & 0 deletions include/sys/zpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,13 @@ zpl_dir_emit_dots(struct file *file, zpl_dir_context_t *ctx)
}
#endif /* HAVE_VFS_ITERATE */

/*
* Linux 4.18, inode times converted from timespec to timespec64.
*/
#if defined(HAVE_INODE_TIMESPEC64_TIMES)
#define zpl_inode_timespec_trunc(ts, gran) timespec64_trunc(ts, gran)
#else
#define zpl_inode_timespec_trunc(ts, gran) timespec_trunc(ts, gran)
#endif

#endif /* _SYS_ZPL_H */
2 changes: 0 additions & 2 deletions lib/libspl/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ noinst_LTLIBRARIES = libspl.la
USER_C = \
getexecname.c \
gethostid.c \
gethrtime.c \
gethrestime.c \
getmntany.c \
list.c \
mkdirp.c \
Expand Down
38 changes: 0 additions & 38 deletions lib/libspl/gethrestime.c

This file was deleted.

45 changes: 0 additions & 45 deletions lib/libspl/gethrtime.c

This file was deleted.

Loading

0 comments on commit 6413c95

Please sign in to comment.