Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Written 1992,1993 by Werner Almesberger
0004  *  22/11/2000 - Fixed fat_date_unix2dos for dates earlier than 01/01/1980
0005  *       and date_dos2unix for date==0 by Igor Zhbanov(bsg@uniyar.ac.ru)
0006  * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
0007  */
0008 
0009 #include <linux/time.h>
0010 #include <linux/fs.h>
0011 #include <linux/slab.h>
0012 #include <linux/buffer_head.h>
0013 #include <linux/blk_types.h>
0014 
0015 #include "exfat_raw.h"
0016 #include "exfat_fs.h"
0017 
0018 /*
0019  * exfat_fs_error reports a file system problem that might indicate fa data
0020  * corruption/inconsistency. Depending on 'errors' mount option the
0021  * panic() is called, or error message is printed FAT and nothing is done,
0022  * or filesystem is remounted read-only (default behavior).
0023  * In case the file system is remounted read-only, it can be made writable
0024  * again by remounting it.
0025  */
0026 void __exfat_fs_error(struct super_block *sb, int report, const char *fmt, ...)
0027 {
0028     struct exfat_mount_options *opts = &EXFAT_SB(sb)->options;
0029     va_list args;
0030     struct va_format vaf;
0031 
0032     if (report) {
0033         va_start(args, fmt);
0034         vaf.fmt = fmt;
0035         vaf.va = &args;
0036         exfat_err(sb, "error, %pV", &vaf);
0037         va_end(args);
0038     }
0039 
0040     if (opts->errors == EXFAT_ERRORS_PANIC) {
0041         panic("exFAT-fs (%s): fs panic from previous error\n",
0042             sb->s_id);
0043     } else if (opts->errors == EXFAT_ERRORS_RO && !sb_rdonly(sb)) {
0044         sb->s_flags |= SB_RDONLY;
0045         exfat_err(sb, "Filesystem has been set read-only");
0046     }
0047 }
0048 
0049 #define SECS_PER_MIN    (60)
0050 #define TIMEZONE_SEC(x) ((x) * 15 * SECS_PER_MIN)
0051 
0052 static void exfat_adjust_tz(struct timespec64 *ts, u8 tz_off)
0053 {
0054     if (tz_off <= 0x3F)
0055         ts->tv_sec -= TIMEZONE_SEC(tz_off);
0056     else /* 0x40 <= (tz_off & 0x7F) <=0x7F */
0057         ts->tv_sec += TIMEZONE_SEC(0x80 - tz_off);
0058 }
0059 
0060 static inline int exfat_tz_offset(struct exfat_sb_info *sbi)
0061 {
0062     if (sbi->options.sys_tz)
0063         return -sys_tz.tz_minuteswest;
0064     return sbi->options.time_offset;
0065 }
0066 
0067 /* Convert a EXFAT time/date pair to a UNIX date (seconds since 1 1 70). */
0068 void exfat_get_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts,
0069         u8 tz, __le16 time, __le16 date, u8 time_cs)
0070 {
0071     u16 t = le16_to_cpu(time);
0072     u16 d = le16_to_cpu(date);
0073 
0074     ts->tv_sec = mktime64(1980 + (d >> 9), d >> 5 & 0x000F, d & 0x001F,
0075                   t >> 11, (t >> 5) & 0x003F, (t & 0x001F) << 1);
0076 
0077 
0078     /* time_cs field represent 0 ~ 199cs(1990 ms) */
0079     if (time_cs) {
0080         ts->tv_sec += time_cs / 100;
0081         ts->tv_nsec = (time_cs % 100) * 10 * NSEC_PER_MSEC;
0082     } else
0083         ts->tv_nsec = 0;
0084 
0085     if (tz & EXFAT_TZ_VALID)
0086         /* Adjust timezone to UTC0. */
0087         exfat_adjust_tz(ts, tz & ~EXFAT_TZ_VALID);
0088     else
0089         ts->tv_sec -= exfat_tz_offset(sbi) * SECS_PER_MIN;
0090 }
0091 
0092 /* Convert linear UNIX date to a EXFAT time/date pair. */
0093 void exfat_set_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts,
0094         u8 *tz, __le16 *time, __le16 *date, u8 *time_cs)
0095 {
0096     struct tm tm;
0097     u16 t, d;
0098 
0099     time64_to_tm(ts->tv_sec, 0, &tm);
0100     t = (tm.tm_hour << 11) | (tm.tm_min << 5) | (tm.tm_sec >> 1);
0101     d = ((tm.tm_year - 80) <<  9) | ((tm.tm_mon + 1) << 5) | tm.tm_mday;
0102 
0103     *time = cpu_to_le16(t);
0104     *date = cpu_to_le16(d);
0105 
0106     /* time_cs field represent 0 ~ 199cs(1990 ms) */
0107     if (time_cs)
0108         *time_cs = (tm.tm_sec & 1) * 100 +
0109             ts->tv_nsec / (10 * NSEC_PER_MSEC);
0110 
0111     /*
0112      * Record 00h value for OffsetFromUtc field and 1 value for OffsetValid
0113      * to indicate that local time and UTC are the same.
0114      */
0115     *tz = EXFAT_TZ_VALID;
0116 }
0117 
0118 /*
0119  * The timestamp for access_time has double seconds granularity.
0120  * (There is no 10msIncrement field for access_time unlike create/modify_time)
0121  * atime also has only a 2-second resolution.
0122  */
0123 void exfat_truncate_atime(struct timespec64 *ts)
0124 {
0125     ts->tv_sec = round_down(ts->tv_sec, 2);
0126     ts->tv_nsec = 0;
0127 }
0128 
0129 u16 exfat_calc_chksum16(void *data, int len, u16 chksum, int type)
0130 {
0131     int i;
0132     u8 *c = (u8 *)data;
0133 
0134     for (i = 0; i < len; i++, c++) {
0135         if (unlikely(type == CS_DIR_ENTRY && (i == 2 || i == 3)))
0136             continue;
0137         chksum = ((chksum << 15) | (chksum >> 1)) + *c;
0138     }
0139     return chksum;
0140 }
0141 
0142 u32 exfat_calc_chksum32(void *data, int len, u32 chksum, int type)
0143 {
0144     int i;
0145     u8 *c = (u8 *)data;
0146 
0147     for (i = 0; i < len; i++, c++) {
0148         if (unlikely(type == CS_BOOT_SECTOR &&
0149                  (i == 106 || i == 107 || i == 112)))
0150             continue;
0151         chksum = ((chksum << 31) | (chksum >> 1)) + *c;
0152     }
0153     return chksum;
0154 }
0155 
0156 void exfat_update_bh(struct buffer_head *bh, int sync)
0157 {
0158     set_buffer_uptodate(bh);
0159     mark_buffer_dirty(bh);
0160 
0161     if (sync)
0162         sync_dirty_buffer(bh);
0163 }
0164 
0165 int exfat_update_bhs(struct buffer_head **bhs, int nr_bhs, int sync)
0166 {
0167     int i, err = 0;
0168 
0169     for (i = 0; i < nr_bhs; i++) {
0170         set_buffer_uptodate(bhs[i]);
0171         mark_buffer_dirty(bhs[i]);
0172         if (sync)
0173             write_dirty_buffer(bhs[i], REQ_SYNC);
0174     }
0175 
0176     for (i = 0; i < nr_bhs && sync; i++) {
0177         wait_on_buffer(bhs[i]);
0178         if (!err && !buffer_uptodate(bhs[i]))
0179             err = -EIO;
0180     }
0181     return err;
0182 }
0183 
0184 void exfat_chain_set(struct exfat_chain *ec, unsigned int dir,
0185         unsigned int size, unsigned char flags)
0186 {
0187     ec->dir = dir;
0188     ec->size = size;
0189     ec->flags = flags;
0190 }
0191 
0192 void exfat_chain_dup(struct exfat_chain *dup, struct exfat_chain *ec)
0193 {
0194     return exfat_chain_set(dup, ec->dir, ec->size, ec->flags);
0195 }