|
Kmods SIG |
9e3ffb |
From 772b29cca528fcb21374bb3e597d848779938d16 Mon Sep 17 00:00:00 2001
|
|
Kmods SIG |
9e3ffb |
From: Namjae Jeon <namjae.jeon@samsung.com>
|
|
Kmods SIG |
9e3ffb |
Date: Mon, 2 Mar 2020 15:21:40 +0900
|
|
Kmods SIG |
9e3ffb |
Subject: [Backport 772b29cca528] exfat: add misc operations
|
|
Kmods SIG |
9e3ffb |
MIME-Version: 1.0
|
|
Kmods SIG |
9e3ffb |
Content-Type: text/plain; charset=UTF-8
|
|
Kmods SIG |
9e3ffb |
Content-Transfer-Encoding: 8bit
|
|
Kmods SIG |
9e3ffb |
|
|
Kmods SIG |
9e3ffb |
This adds the implementation of misc operations for exfat.
|
|
Kmods SIG |
9e3ffb |
|
|
Kmods SIG |
9e3ffb |
Signed-off-by: Namjae Jeon <namjae.jeon@samsung.com>
|
|
Kmods SIG |
9e3ffb |
Signed-off-by: Sungjong Seo <sj1557.seo@samsung.com>
|
|
Kmods SIG |
9e3ffb |
Reviewed-by: Arnd Bergmann <arnd@arndb.de>
|
|
Kmods SIG |
9e3ffb |
Reviewed-by: Pali Rohár <pali.rohar@gmail.com>
|
|
Kmods SIG |
9e3ffb |
Reviewed-by: Christoph Hellwig <hch@lst.de>
|
|
Kmods SIG |
9e3ffb |
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
|
|
Kmods SIG |
9e3ffb |
---
|
|
Kmods SIG |
9e3ffb |
src/misc.c | 163 ++++++++++++++++++++++++++++++++++++++++++++++++
|
|
Kmods SIG |
9e3ffb |
1 file changed, 163 insertions(+)
|
|
Kmods SIG |
9e3ffb |
create mode 100644 src/misc.c
|
|
Kmods SIG |
9e3ffb |
|
|
Kmods SIG |
9e3ffb |
diff --git a/src/misc.c b/src/misc.c
|
|
Kmods SIG |
9e3ffb |
new file mode 100644
|
|
Kmods SIG |
9e3ffb |
index 0000000000000000000000000000000000000000..14a3300848f6a0fb754b9d34bdacbc31b90f5d07
|
|
Kmods SIG |
9e3ffb |
--- /dev/null
|
|
Kmods SIG |
9e3ffb |
+++ b/src/misc.c
|
|
Kmods SIG |
9e3ffb |
@@ -0,0 +1,163 @@
|
|
Kmods SIG |
9e3ffb |
+// SPDX-License-Identifier: GPL-2.0-or-later
|
|
Kmods SIG |
9e3ffb |
+/*
|
|
Kmods SIG |
9e3ffb |
+ * Written 1992,1993 by Werner Almesberger
|
|
Kmods SIG |
9e3ffb |
+ * 22/11/2000 - Fixed fat_date_unix2dos for dates earlier than 01/01/1980
|
|
Kmods SIG |
9e3ffb |
+ * and date_dos2unix for date==0 by Igor Zhbanov(bsg@uniyar.ac.ru)
|
|
Kmods SIG |
9e3ffb |
+ * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
|
|
Kmods SIG |
9e3ffb |
+ */
|
|
Kmods SIG |
9e3ffb |
+
|
|
Kmods SIG |
9e3ffb |
+#include <linux/time.h>
|
|
Kmods SIG |
9e3ffb |
+#include <linux/fs.h>
|
|
Kmods SIG |
9e3ffb |
+#include <linux/slab.h>
|
|
Kmods SIG |
9e3ffb |
+#include <linux/buffer_head.h>
|
|
Kmods SIG |
9e3ffb |
+
|
|
Kmods SIG |
9e3ffb |
+#include "exfat_raw.h"
|
|
Kmods SIG |
9e3ffb |
+#include "exfat_fs.h"
|
|
Kmods SIG |
9e3ffb |
+
|
|
Kmods SIG |
9e3ffb |
+/*
|
|
Kmods SIG |
9e3ffb |
+ * exfat_fs_error reports a file system problem that might indicate fa data
|
|
Kmods SIG |
9e3ffb |
+ * corruption/inconsistency. Depending on 'errors' mount option the
|
|
Kmods SIG |
9e3ffb |
+ * panic() is called, or error message is printed FAT and nothing is done,
|
|
Kmods SIG |
9e3ffb |
+ * or filesystem is remounted read-only (default behavior).
|
|
Kmods SIG |
9e3ffb |
+ * In case the file system is remounted read-only, it can be made writable
|
|
Kmods SIG |
9e3ffb |
+ * again by remounting it.
|
|
Kmods SIG |
9e3ffb |
+ */
|
|
Kmods SIG |
9e3ffb |
+void __exfat_fs_error(struct super_block *sb, int report, const char *fmt, ...)
|
|
Kmods SIG |
9e3ffb |
+{
|
|
Kmods SIG |
9e3ffb |
+ struct exfat_mount_options *opts = &EXFAT_SB(sb)->options;
|
|
Kmods SIG |
9e3ffb |
+ va_list args;
|
|
Kmods SIG |
9e3ffb |
+ struct va_format vaf;
|
|
Kmods SIG |
9e3ffb |
+
|
|
Kmods SIG |
9e3ffb |
+ if (report) {
|
|
Kmods SIG |
9e3ffb |
+ va_start(args, fmt);
|
|
Kmods SIG |
9e3ffb |
+ vaf.fmt = fmt;
|
|
Kmods SIG |
9e3ffb |
+ vaf.va = &arg;;
|
|
Kmods SIG |
9e3ffb |
+ exfat_msg(sb, KERN_ERR, "error, %pV\n", &vaf;;
|
|
Kmods SIG |
9e3ffb |
+ va_end(args);
|
|
Kmods SIG |
9e3ffb |
+ }
|
|
Kmods SIG |
9e3ffb |
+
|
|
Kmods SIG |
9e3ffb |
+ if (opts->errors == EXFAT_ERRORS_PANIC) {
|
|
Kmods SIG |
9e3ffb |
+ panic("exFAT-fs (%s): fs panic from previous error\n",
|
|
Kmods SIG |
9e3ffb |
+ sb->s_id);
|
|
Kmods SIG |
9e3ffb |
+ } else if (opts->errors == EXFAT_ERRORS_RO && !sb_rdonly(sb)) {
|
|
Kmods SIG |
9e3ffb |
+ sb->s_flags |= SB_RDONLY;
|
|
Kmods SIG |
9e3ffb |
+ exfat_msg(sb, KERN_ERR, "Filesystem has been set read-only");
|
|
Kmods SIG |
9e3ffb |
+ }
|
|
Kmods SIG |
9e3ffb |
+}
|
|
Kmods SIG |
9e3ffb |
+
|
|
Kmods SIG |
9e3ffb |
+/*
|
|
Kmods SIG |
9e3ffb |
+ * exfat_msg() - print preformated EXFAT specific messages.
|
|
Kmods SIG |
9e3ffb |
+ * All logs except what uses exfat_fs_error() should be written by exfat_msg()
|
|
Kmods SIG |
9e3ffb |
+ */
|
|
Kmods SIG |
9e3ffb |
+void exfat_msg(struct super_block *sb, const char *level, const char *fmt, ...)
|
|
Kmods SIG |
9e3ffb |
+{
|
|
Kmods SIG |
9e3ffb |
+ struct va_format vaf;
|
|
Kmods SIG |
9e3ffb |
+ va_list args;
|
|
Kmods SIG |
9e3ffb |
+
|
|
Kmods SIG |
9e3ffb |
+ va_start(args, fmt);
|
|
Kmods SIG |
9e3ffb |
+ vaf.fmt = fmt;
|
|
Kmods SIG |
9e3ffb |
+ vaf.va = &arg;;
|
|
Kmods SIG |
9e3ffb |
+ /* level means KERN_ pacility level */
|
|
Kmods SIG |
9e3ffb |
+ printk("%sexFAT-fs (%s): %pV\n", level, sb->s_id, &vaf;;
|
|
Kmods SIG |
9e3ffb |
+ va_end(args);
|
|
Kmods SIG |
9e3ffb |
+}
|
|
Kmods SIG |
9e3ffb |
+
|
|
Kmods SIG |
9e3ffb |
+#define SECS_PER_MIN (60)
|
|
Kmods SIG |
9e3ffb |
+#define TIMEZONE_SEC(x) ((x) * 15 * SECS_PER_MIN)
|
|
Kmods SIG |
9e3ffb |
+
|
|
Kmods SIG |
9e3ffb |
+static void exfat_adjust_tz(struct timespec64 *ts, u8 tz_off)
|
|
Kmods SIG |
9e3ffb |
+{
|
|
Kmods SIG |
9e3ffb |
+ if (tz_off <= 0x3F)
|
|
Kmods SIG |
9e3ffb |
+ ts->tv_sec -= TIMEZONE_SEC(tz_off);
|
|
Kmods SIG |
9e3ffb |
+ else /* 0x40 <= (tz_off & 0x7F) <=0x7F */
|
|
Kmods SIG |
9e3ffb |
+ ts->tv_sec += TIMEZONE_SEC(0x80 - tz_off);
|
|
Kmods SIG |
9e3ffb |
+}
|
|
Kmods SIG |
9e3ffb |
+
|
|
Kmods SIG |
9e3ffb |
+/* Convert a EXFAT time/date pair to a UNIX date (seconds since 1 1 70). */
|
|
Kmods SIG |
9e3ffb |
+void exfat_get_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts,
|
|
Kmods SIG |
9e3ffb |
+ u8 tz, __le16 time, __le16 date, u8 time_ms)
|
|
Kmods SIG |
9e3ffb |
+{
|
|
Kmods SIG |
9e3ffb |
+ u16 t = le16_to_cpu(time);
|
|
Kmods SIG |
9e3ffb |
+ u16 d = le16_to_cpu(date);
|
|
Kmods SIG |
9e3ffb |
+
|
|
Kmods SIG |
9e3ffb |
+ ts->tv_sec = mktime64(1980 + (d >> 9), d >> 5 & 0x000F, d & 0x001F,
|
|
Kmods SIG |
9e3ffb |
+ t >> 11, (t >> 5) & 0x003F, (t & 0x001F) << 1);
|
|
Kmods SIG |
9e3ffb |
+
|
|
Kmods SIG |
9e3ffb |
+
|
|
Kmods SIG |
9e3ffb |
+ /* time_ms field represent 0 ~ 199(1990 ms) */
|
|
Kmods SIG |
9e3ffb |
+ if (time_ms) {
|
|
Kmods SIG |
9e3ffb |
+ ts->tv_sec += time_ms / 100;
|
|
Kmods SIG |
9e3ffb |
+ ts->tv_nsec = (time_ms % 100) * 10 * NSEC_PER_MSEC;
|
|
Kmods SIG |
9e3ffb |
+ }
|
|
Kmods SIG |
9e3ffb |
+
|
|
Kmods SIG |
9e3ffb |
+ if (tz & EXFAT_TZ_VALID)
|
|
Kmods SIG |
9e3ffb |
+ /* Adjust timezone to UTC0. */
|
|
Kmods SIG |
9e3ffb |
+ exfat_adjust_tz(ts, tz & ~EXFAT_TZ_VALID);
|
|
Kmods SIG |
9e3ffb |
+ else
|
|
Kmods SIG |
9e3ffb |
+ /* Convert from local time to UTC using time_offset. */
|
|
Kmods SIG |
9e3ffb |
+ ts->tv_sec -= sbi->options.time_offset * SECS_PER_MIN;
|
|
Kmods SIG |
9e3ffb |
+}
|
|
Kmods SIG |
9e3ffb |
+
|
|
Kmods SIG |
9e3ffb |
+/* Convert linear UNIX date to a EXFAT time/date pair. */
|
|
Kmods SIG |
9e3ffb |
+void exfat_set_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts,
|
|
Kmods SIG |
9e3ffb |
+ u8 *tz, __le16 *time, __le16 *date, u8 *time_ms)
|
|
Kmods SIG |
9e3ffb |
+{
|
|
Kmods SIG |
9e3ffb |
+ struct tm tm;
|
|
Kmods SIG |
9e3ffb |
+ u16 t, d;
|
|
Kmods SIG |
9e3ffb |
+
|
|
Kmods SIG |
9e3ffb |
+ time64_to_tm(ts->tv_sec, 0, &tm;;
|
|
Kmods SIG |
9e3ffb |
+ t = (tm.tm_hour << 11) | (tm.tm_min << 5) | (tm.tm_sec >> 1);
|
|
Kmods SIG |
9e3ffb |
+ d = ((tm.tm_year - 80) << 9) | ((tm.tm_mon + 1) << 5) | tm.tm_mday;
|
|
Kmods SIG |
9e3ffb |
+
|
|
Kmods SIG |
9e3ffb |
+ *time = cpu_to_le16(t);
|
|
Kmods SIG |
9e3ffb |
+ *date = cpu_to_le16(d);
|
|
Kmods SIG |
9e3ffb |
+
|
|
Kmods SIG |
9e3ffb |
+ /* time_ms field represent 0 ~ 199(1990 ms) */
|
|
Kmods SIG |
9e3ffb |
+ if (time_ms)
|
|
Kmods SIG |
9e3ffb |
+ *time_ms = (tm.tm_sec & 1) * 100 +
|
|
Kmods SIG |
9e3ffb |
+ ts->tv_nsec / (10 * NSEC_PER_MSEC);
|
|
Kmods SIG |
9e3ffb |
+
|
|
Kmods SIG |
9e3ffb |
+ /*
|
|
Kmods SIG |
9e3ffb |
+ * Record 00h value for OffsetFromUtc field and 1 value for OffsetValid
|
|
Kmods SIG |
9e3ffb |
+ * to indicate that local time and UTC are the same.
|
|
Kmods SIG |
9e3ffb |
+ */
|
|
Kmods SIG |
9e3ffb |
+ *tz = EXFAT_TZ_VALID;
|
|
Kmods SIG |
9e3ffb |
+}
|
|
Kmods SIG |
9e3ffb |
+
|
|
Kmods SIG |
9e3ffb |
+unsigned short exfat_calc_chksum_2byte(void *data, int len,
|
|
Kmods SIG |
9e3ffb |
+ unsigned short chksum, int type)
|
|
Kmods SIG |
9e3ffb |
+{
|
|
Kmods SIG |
9e3ffb |
+ int i;
|
|
Kmods SIG |
9e3ffb |
+ unsigned char *c = (unsigned char *)data;
|
|
Kmods SIG |
9e3ffb |
+
|
|
Kmods SIG |
9e3ffb |
+ for (i = 0; i < len; i++, c++) {
|
|
Kmods SIG |
9e3ffb |
+ if (((i == 2) || (i == 3)) && (type == CS_DIR_ENTRY))
|
|
Kmods SIG |
9e3ffb |
+ continue;
|
|
Kmods SIG |
9e3ffb |
+ chksum = (((chksum & 1) << 15) | ((chksum & 0xFFFE) >> 1)) +
|
|
Kmods SIG |
9e3ffb |
+ (unsigned short)*c;
|
|
Kmods SIG |
9e3ffb |
+ }
|
|
Kmods SIG |
9e3ffb |
+ return chksum;
|
|
Kmods SIG |
9e3ffb |
+}
|
|
Kmods SIG |
9e3ffb |
+
|
|
Kmods SIG |
9e3ffb |
+void exfat_update_bh(struct super_block *sb, struct buffer_head *bh, int sync)
|
|
Kmods SIG |
9e3ffb |
+{
|
|
Kmods SIG |
9e3ffb |
+ set_bit(EXFAT_SB_DIRTY, &EXFAT_SB(sb)->s_state);
|
|
Kmods SIG |
9e3ffb |
+ set_buffer_uptodate(bh);
|
|
Kmods SIG |
9e3ffb |
+ mark_buffer_dirty(bh);
|
|
Kmods SIG |
9e3ffb |
+
|
|
Kmods SIG |
9e3ffb |
+ if (sync)
|
|
Kmods SIG |
9e3ffb |
+ sync_dirty_buffer(bh);
|
|
Kmods SIG |
9e3ffb |
+}
|
|
Kmods SIG |
9e3ffb |
+
|
|
Kmods SIG |
9e3ffb |
+void exfat_chain_set(struct exfat_chain *ec, unsigned int dir,
|
|
Kmods SIG |
9e3ffb |
+ unsigned int size, unsigned char flags)
|
|
Kmods SIG |
9e3ffb |
+{
|
|
Kmods SIG |
9e3ffb |
+ ec->dir = dir;
|
|
Kmods SIG |
9e3ffb |
+ ec->size = size;
|
|
Kmods SIG |
9e3ffb |
+ ec->flags = flags;
|
|
Kmods SIG |
9e3ffb |
+}
|
|
Kmods SIG |
9e3ffb |
+
|
|
Kmods SIG |
9e3ffb |
+void exfat_chain_dup(struct exfat_chain *dup, struct exfat_chain *ec)
|
|
Kmods SIG |
9e3ffb |
+{
|
|
Kmods SIG |
9e3ffb |
+ return exfat_chain_set(dup, ec->dir, ec->size, ec->flags);
|
|
Kmods SIG |
9e3ffb |
+}
|
|
Kmods SIG |
9e3ffb |
--
|
|
Kmods SIG |
9e3ffb |
2.31.1
|
|
Kmods SIG |
9e3ffb |
|