Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  linux/fs/vfat/namei.c
0004  *
0005  *  Written 1992,1993 by Werner Almesberger
0006  *
0007  *  Windows95/Windows NT compatible extended MSDOS filesystem
0008  *    by Gordon Chaffee Copyright (C) 1995.  Send bug reports for the
0009  *    VFAT filesystem to <chaffee@cs.berkeley.edu>.  Specify
0010  *    what file operation caused you trouble and if you can duplicate
0011  *    the problem, send a script that demonstrates it.
0012  *
0013  *  Short name translation 1999, 2001 by Wolfram Pienkoss <wp@bszh.de>
0014  *
0015  *  Support Multibyte characters and cleanup by
0016  *              OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
0017  */
0018 
0019 #include <linux/module.h>
0020 #include <linux/ctype.h>
0021 #include <linux/slab.h>
0022 #include <linux/namei.h>
0023 #include <linux/kernel.h>
0024 #include <linux/iversion.h>
0025 #include "fat.h"
0026 
0027 static inline unsigned long vfat_d_version(struct dentry *dentry)
0028 {
0029     return (unsigned long) dentry->d_fsdata;
0030 }
0031 
0032 static inline void vfat_d_version_set(struct dentry *dentry,
0033                       unsigned long version)
0034 {
0035     dentry->d_fsdata = (void *) version;
0036 }
0037 
0038 /*
0039  * If new entry was created in the parent, it could create the 8.3
0040  * alias (the shortname of logname).  So, the parent may have the
0041  * negative-dentry which matches the created 8.3 alias.
0042  *
0043  * If it happened, the negative dentry isn't actually negative
0044  * anymore.  So, drop it.
0045  */
0046 static int vfat_revalidate_shortname(struct dentry *dentry)
0047 {
0048     int ret = 1;
0049     spin_lock(&dentry->d_lock);
0050     if (!inode_eq_iversion(d_inode(dentry->d_parent), vfat_d_version(dentry)))
0051         ret = 0;
0052     spin_unlock(&dentry->d_lock);
0053     return ret;
0054 }
0055 
0056 static int vfat_revalidate(struct dentry *dentry, unsigned int flags)
0057 {
0058     if (flags & LOOKUP_RCU)
0059         return -ECHILD;
0060 
0061     /* This is not negative dentry. Always valid. */
0062     if (d_really_is_positive(dentry))
0063         return 1;
0064     return vfat_revalidate_shortname(dentry);
0065 }
0066 
0067 static int vfat_revalidate_ci(struct dentry *dentry, unsigned int flags)
0068 {
0069     if (flags & LOOKUP_RCU)
0070         return -ECHILD;
0071 
0072     /*
0073      * This is not negative dentry. Always valid.
0074      *
0075      * Note, rename() to existing directory entry will have ->d_inode,
0076      * and will use existing name which isn't specified name by user.
0077      *
0078      * We may be able to drop this positive dentry here. But dropping
0079      * positive dentry isn't good idea. So it's unsupported like
0080      * rename("filename", "FILENAME") for now.
0081      */
0082     if (d_really_is_positive(dentry))
0083         return 1;
0084 
0085     /*
0086      * This may be nfsd (or something), anyway, we can't see the
0087      * intent of this. So, since this can be for creation, drop it.
0088      */
0089     if (!flags)
0090         return 0;
0091 
0092     /*
0093      * Drop the negative dentry, in order to make sure to use the
0094      * case sensitive name which is specified by user if this is
0095      * for creation.
0096      */
0097     if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
0098         return 0;
0099 
0100     return vfat_revalidate_shortname(dentry);
0101 }
0102 
0103 /* returns the length of a struct qstr, ignoring trailing dots */
0104 static unsigned int __vfat_striptail_len(unsigned int len, const char *name)
0105 {
0106     while (len && name[len - 1] == '.')
0107         len--;
0108     return len;
0109 }
0110 
0111 static unsigned int vfat_striptail_len(const struct qstr *qstr)
0112 {
0113     return __vfat_striptail_len(qstr->len, qstr->name);
0114 }
0115 
0116 /*
0117  * Compute the hash for the vfat name corresponding to the dentry.
0118  * Note: if the name is invalid, we leave the hash code unchanged so
0119  * that the existing dentry can be used. The vfat fs routines will
0120  * return ENOENT or EINVAL as appropriate.
0121  */
0122 static int vfat_hash(const struct dentry *dentry, struct qstr *qstr)
0123 {
0124     qstr->hash = full_name_hash(dentry, qstr->name, vfat_striptail_len(qstr));
0125     return 0;
0126 }
0127 
0128 /*
0129  * Compute the hash for the vfat name corresponding to the dentry.
0130  * Note: if the name is invalid, we leave the hash code unchanged so
0131  * that the existing dentry can be used. The vfat fs routines will
0132  * return ENOENT or EINVAL as appropriate.
0133  */
0134 static int vfat_hashi(const struct dentry *dentry, struct qstr *qstr)
0135 {
0136     struct nls_table *t = MSDOS_SB(dentry->d_sb)->nls_io;
0137     const unsigned char *name;
0138     unsigned int len;
0139     unsigned long hash;
0140 
0141     name = qstr->name;
0142     len = vfat_striptail_len(qstr);
0143 
0144     hash = init_name_hash(dentry);
0145     while (len--)
0146         hash = partial_name_hash(nls_tolower(t, *name++), hash);
0147     qstr->hash = end_name_hash(hash);
0148 
0149     return 0;
0150 }
0151 
0152 /*
0153  * Case insensitive compare of two vfat names.
0154  */
0155 static int vfat_cmpi(const struct dentry *dentry,
0156         unsigned int len, const char *str, const struct qstr *name)
0157 {
0158     struct nls_table *t = MSDOS_SB(dentry->d_sb)->nls_io;
0159     unsigned int alen, blen;
0160 
0161     /* A filename cannot end in '.' or we treat it like it has none */
0162     alen = vfat_striptail_len(name);
0163     blen = __vfat_striptail_len(len, str);
0164     if (alen == blen) {
0165         if (nls_strnicmp(t, name->name, str, alen) == 0)
0166             return 0;
0167     }
0168     return 1;
0169 }
0170 
0171 /*
0172  * Case sensitive compare of two vfat names.
0173  */
0174 static int vfat_cmp(const struct dentry *dentry,
0175         unsigned int len, const char *str, const struct qstr *name)
0176 {
0177     unsigned int alen, blen;
0178 
0179     /* A filename cannot end in '.' or we treat it like it has none */
0180     alen = vfat_striptail_len(name);
0181     blen = __vfat_striptail_len(len, str);
0182     if (alen == blen) {
0183         if (strncmp(name->name, str, alen) == 0)
0184             return 0;
0185     }
0186     return 1;
0187 }
0188 
0189 static const struct dentry_operations vfat_ci_dentry_ops = {
0190     .d_revalidate   = vfat_revalidate_ci,
0191     .d_hash     = vfat_hashi,
0192     .d_compare  = vfat_cmpi,
0193 };
0194 
0195 static const struct dentry_operations vfat_dentry_ops = {
0196     .d_revalidate   = vfat_revalidate,
0197     .d_hash     = vfat_hash,
0198     .d_compare  = vfat_cmp,
0199 };
0200 
0201 /* Characters that are undesirable in an MS-DOS file name */
0202 
0203 static inline wchar_t vfat_bad_char(wchar_t w)
0204 {
0205     return (w < 0x0020)
0206         || (w == '*') || (w == '?') || (w == '<') || (w == '>')
0207         || (w == '|') || (w == '"') || (w == ':') || (w == '/')
0208         || (w == '\\');
0209 }
0210 
0211 static inline wchar_t vfat_replace_char(wchar_t w)
0212 {
0213     return (w == '[') || (w == ']') || (w == ';') || (w == ',')
0214         || (w == '+') || (w == '=');
0215 }
0216 
0217 static wchar_t vfat_skip_char(wchar_t w)
0218 {
0219     return (w == '.') || (w == ' ');
0220 }
0221 
0222 static inline int vfat_is_used_badchars(const wchar_t *s, int len)
0223 {
0224     int i;
0225 
0226     for (i = 0; i < len; i++)
0227         if (vfat_bad_char(s[i]))
0228             return -EINVAL;
0229 
0230     if (s[i - 1] == ' ') /* last character cannot be space */
0231         return -EINVAL;
0232 
0233     return 0;
0234 }
0235 
0236 static int vfat_find_form(struct inode *dir, unsigned char *name)
0237 {
0238     struct fat_slot_info sinfo;
0239     int err = fat_scan(dir, name, &sinfo);
0240     if (err)
0241         return -ENOENT;
0242     brelse(sinfo.bh);
0243     return 0;
0244 }
0245 
0246 /*
0247  * 1) Valid characters for the 8.3 format alias are any combination of
0248  * letters, uppercase alphabets, digits, any of the
0249  * following special characters:
0250  *     $ % ' ` - @ { } ~ ! # ( ) & _ ^
0251  * In this case Longfilename is not stored in disk.
0252  *
0253  * WinNT's Extension:
0254  * File name and extension name is contain uppercase/lowercase
0255  * only. And it is expressed by CASE_LOWER_BASE and CASE_LOWER_EXT.
0256  *
0257  * 2) File name is 8.3 format, but it contain the uppercase and
0258  * lowercase char, muliti bytes char, etc. In this case numtail is not
0259  * added, but Longfilename is stored.
0260  *
0261  * 3) When the one except for the above, or the following special
0262  * character are contained:
0263  *        .   [ ] ; , + =
0264  * numtail is added, and Longfilename must be stored in disk .
0265  */
0266 struct shortname_info {
0267     unsigned char lower:1,
0268               upper:1,
0269               valid:1;
0270 };
0271 #define INIT_SHORTNAME_INFO(x)  do {        \
0272     (x)->lower = 1;             \
0273     (x)->upper = 1;             \
0274     (x)->valid = 1;             \
0275 } while (0)
0276 
0277 static inline int to_shortname_char(struct nls_table *nls,
0278                     unsigned char *buf, int buf_size,
0279                     wchar_t *src, struct shortname_info *info)
0280 {
0281     int len;
0282 
0283     if (vfat_skip_char(*src)) {
0284         info->valid = 0;
0285         return 0;
0286     }
0287     if (vfat_replace_char(*src)) {
0288         info->valid = 0;
0289         buf[0] = '_';
0290         return 1;
0291     }
0292 
0293     len = nls->uni2char(*src, buf, buf_size);
0294     if (len <= 0) {
0295         info->valid = 0;
0296         buf[0] = '_';
0297         len = 1;
0298     } else if (len == 1) {
0299         unsigned char prev = buf[0];
0300 
0301         if (buf[0] >= 0x7F) {
0302             info->lower = 0;
0303             info->upper = 0;
0304         }
0305 
0306         buf[0] = nls_toupper(nls, buf[0]);
0307         if (isalpha(buf[0])) {
0308             if (buf[0] == prev)
0309                 info->lower = 0;
0310             else
0311                 info->upper = 0;
0312         }
0313     } else {
0314         info->lower = 0;
0315         info->upper = 0;
0316     }
0317 
0318     return len;
0319 }
0320 
0321 /*
0322  * Given a valid longname, create a unique shortname.  Make sure the
0323  * shortname does not exist
0324  * Returns negative number on error, 0 for a normal
0325  * return, and 1 for valid shortname
0326  */
0327 static int vfat_create_shortname(struct inode *dir, struct nls_table *nls,
0328                  wchar_t *uname, int ulen,
0329                  unsigned char *name_res, unsigned char *lcase)
0330 {
0331     struct fat_mount_options *opts = &MSDOS_SB(dir->i_sb)->options;
0332     wchar_t *ip, *ext_start, *end, *name_start;
0333     unsigned char base[9], ext[4], buf[5], *p;
0334     unsigned char charbuf[NLS_MAX_CHARSET_SIZE];
0335     int chl, chi;
0336     int sz = 0, extlen, baselen, i, numtail_baselen, numtail2_baselen;
0337     int is_shortname;
0338     struct shortname_info base_info, ext_info;
0339 
0340     is_shortname = 1;
0341     INIT_SHORTNAME_INFO(&base_info);
0342     INIT_SHORTNAME_INFO(&ext_info);
0343 
0344     /* Now, we need to create a shortname from the long name */
0345     ext_start = end = &uname[ulen];
0346     while (--ext_start >= uname) {
0347         if (*ext_start == 0x002E) { /* is `.' */
0348             if (ext_start == end - 1) {
0349                 sz = ulen;
0350                 ext_start = NULL;
0351             }
0352             break;
0353         }
0354     }
0355 
0356     if (ext_start == uname - 1) {
0357         sz = ulen;
0358         ext_start = NULL;
0359     } else if (ext_start) {
0360         /*
0361          * Names which start with a dot could be just
0362          * an extension eg. "...test".  In this case Win95
0363          * uses the extension as the name and sets no extension.
0364          */
0365         name_start = &uname[0];
0366         while (name_start < ext_start) {
0367             if (!vfat_skip_char(*name_start))
0368                 break;
0369             name_start++;
0370         }
0371         if (name_start != ext_start) {
0372             sz = ext_start - uname;
0373             ext_start++;
0374         } else {
0375             sz = ulen;
0376             ext_start = NULL;
0377         }
0378     }
0379 
0380     numtail_baselen = 6;
0381     numtail2_baselen = 2;
0382     for (baselen = i = 0, p = base, ip = uname; i < sz; i++, ip++) {
0383         chl = to_shortname_char(nls, charbuf, sizeof(charbuf),
0384                     ip, &base_info);
0385         if (chl == 0)
0386             continue;
0387 
0388         if (baselen < 2 && (baselen + chl) > 2)
0389             numtail2_baselen = baselen;
0390         if (baselen < 6 && (baselen + chl) > 6)
0391             numtail_baselen = baselen;
0392         for (chi = 0; chi < chl; chi++) {
0393             *p++ = charbuf[chi];
0394             baselen++;
0395             if (baselen >= 8)
0396                 break;
0397         }
0398         if (baselen >= 8) {
0399             if ((chi < chl - 1) || (ip + 1) - uname < sz)
0400                 is_shortname = 0;
0401             break;
0402         }
0403     }
0404     if (baselen == 0) {
0405         return -EINVAL;
0406     }
0407 
0408     extlen = 0;
0409     if (ext_start) {
0410         for (p = ext, ip = ext_start; extlen < 3 && ip < end; ip++) {
0411             chl = to_shortname_char(nls, charbuf, sizeof(charbuf),
0412                         ip, &ext_info);
0413             if (chl == 0)
0414                 continue;
0415 
0416             if ((extlen + chl) > 3) {
0417                 is_shortname = 0;
0418                 break;
0419             }
0420             for (chi = 0; chi < chl; chi++) {
0421                 *p++ = charbuf[chi];
0422                 extlen++;
0423             }
0424             if (extlen >= 3) {
0425                 if (ip + 1 != end)
0426                     is_shortname = 0;
0427                 break;
0428             }
0429         }
0430     }
0431     ext[extlen] = '\0';
0432     base[baselen] = '\0';
0433 
0434     /* Yes, it can happen. ".\xe5" would do it. */
0435     if (base[0] == DELETED_FLAG)
0436         base[0] = 0x05;
0437 
0438     /* OK, at this point we know that base is not longer than 8 symbols,
0439      * ext is not longer than 3, base is nonempty, both don't contain
0440      * any bad symbols (lowercase transformed to uppercase).
0441      */
0442 
0443     memset(name_res, ' ', MSDOS_NAME);
0444     memcpy(name_res, base, baselen);
0445     memcpy(name_res + 8, ext, extlen);
0446     *lcase = 0;
0447     if (is_shortname && base_info.valid && ext_info.valid) {
0448         if (vfat_find_form(dir, name_res) == 0)
0449             return -EEXIST;
0450 
0451         if (opts->shortname & VFAT_SFN_CREATE_WIN95) {
0452             return (base_info.upper && ext_info.upper);
0453         } else if (opts->shortname & VFAT_SFN_CREATE_WINNT) {
0454             if ((base_info.upper || base_info.lower) &&
0455                 (ext_info.upper || ext_info.lower)) {
0456                 if (!base_info.upper && base_info.lower)
0457                     *lcase |= CASE_LOWER_BASE;
0458                 if (!ext_info.upper && ext_info.lower)
0459                     *lcase |= CASE_LOWER_EXT;
0460                 return 1;
0461             }
0462             return 0;
0463         } else {
0464             BUG();
0465         }
0466     }
0467 
0468     if (opts->numtail == 0)
0469         if (vfat_find_form(dir, name_res) < 0)
0470             return 0;
0471 
0472     /*
0473      * Try to find a unique extension.  This used to
0474      * iterate through all possibilities sequentially,
0475      * but that gave extremely bad performance.  Windows
0476      * only tries a few cases before using random
0477      * values for part of the base.
0478      */
0479 
0480     if (baselen > 6) {
0481         baselen = numtail_baselen;
0482         name_res[7] = ' ';
0483     }
0484     name_res[baselen] = '~';
0485     for (i = 1; i < 10; i++) {
0486         name_res[baselen + 1] = i + '0';
0487         if (vfat_find_form(dir, name_res) < 0)
0488             return 0;
0489     }
0490 
0491     i = jiffies;
0492     sz = (jiffies >> 16) & 0x7;
0493     if (baselen > 2) {
0494         baselen = numtail2_baselen;
0495         name_res[7] = ' ';
0496     }
0497     name_res[baselen + 4] = '~';
0498     name_res[baselen + 5] = '1' + sz;
0499     while (1) {
0500         snprintf(buf, sizeof(buf), "%04X", i & 0xffff);
0501         memcpy(&name_res[baselen], buf, 4);
0502         if (vfat_find_form(dir, name_res) < 0)
0503             break;
0504         i -= 11;
0505     }
0506     return 0;
0507 }
0508 
0509 /* Translate a string, including coded sequences into Unicode */
0510 static int
0511 xlate_to_uni(const unsigned char *name, int len, unsigned char *outname,
0512          int *longlen, int *outlen, int escape, int utf8,
0513          struct nls_table *nls)
0514 {
0515     const unsigned char *ip;
0516     unsigned char *op;
0517     int i, fill;
0518     int charlen;
0519 
0520     if (utf8) {
0521         *outlen = utf8s_to_utf16s(name, len, UTF16_HOST_ENDIAN,
0522                 (wchar_t *) outname, FAT_LFN_LEN + 2);
0523         if (*outlen < 0)
0524             return *outlen;
0525         else if (*outlen > FAT_LFN_LEN)
0526             return -ENAMETOOLONG;
0527 
0528         op = &outname[*outlen * sizeof(wchar_t)];
0529     } else {
0530         for (i = 0, ip = name, op = outname, *outlen = 0;
0531              i < len && *outlen < FAT_LFN_LEN;
0532              *outlen += 1) {
0533             if (escape && (*ip == ':')) {
0534                 u8 uc[2];
0535 
0536                 if (i > len - 5)
0537                     return -EINVAL;
0538 
0539                 if (hex2bin(uc, ip + 1, 2) < 0)
0540                     return -EINVAL;
0541 
0542                 *(wchar_t *)op = uc[0] << 8 | uc[1];
0543 
0544                 op += 2;
0545                 ip += 5;
0546                 i += 5;
0547             } else {
0548                 charlen = nls->char2uni(ip, len - i,
0549                             (wchar_t *)op);
0550                 if (charlen < 0)
0551                     return -EINVAL;
0552                 ip += charlen;
0553                 i += charlen;
0554                 op += 2;
0555             }
0556         }
0557         if (i < len)
0558             return -ENAMETOOLONG;
0559     }
0560 
0561     *longlen = *outlen;
0562     if (*outlen % 13) {
0563         *op++ = 0;
0564         *op++ = 0;
0565         *outlen += 1;
0566         if (*outlen % 13) {
0567             fill = 13 - (*outlen % 13);
0568             for (i = 0; i < fill; i++) {
0569                 *op++ = 0xff;
0570                 *op++ = 0xff;
0571             }
0572             *outlen += fill;
0573         }
0574     }
0575 
0576     return 0;
0577 }
0578 
0579 static int vfat_build_slots(struct inode *dir, const unsigned char *name,
0580                 int len, int is_dir, int cluster,
0581                 struct timespec64 *ts,
0582                 struct msdos_dir_slot *slots, int *nr_slots)
0583 {
0584     struct msdos_sb_info *sbi = MSDOS_SB(dir->i_sb);
0585     struct fat_mount_options *opts = &sbi->options;
0586     struct msdos_dir_slot *ps;
0587     struct msdos_dir_entry *de;
0588     unsigned char cksum, lcase;
0589     unsigned char msdos_name[MSDOS_NAME];
0590     wchar_t *uname;
0591     __le16 time, date;
0592     u8 time_cs;
0593     int err, ulen, usize, i;
0594     loff_t offset;
0595 
0596     *nr_slots = 0;
0597 
0598     uname = __getname();
0599     if (!uname)
0600         return -ENOMEM;
0601 
0602     err = xlate_to_uni(name, len, (unsigned char *)uname, &ulen, &usize,
0603                opts->unicode_xlate, opts->utf8, sbi->nls_io);
0604     if (err)
0605         goto out_free;
0606 
0607     err = vfat_is_used_badchars(uname, ulen);
0608     if (err)
0609         goto out_free;
0610 
0611     err = vfat_create_shortname(dir, sbi->nls_disk, uname, ulen,
0612                     msdos_name, &lcase);
0613     if (err < 0)
0614         goto out_free;
0615     else if (err == 1) {
0616         de = (struct msdos_dir_entry *)slots;
0617         err = 0;
0618         goto shortname;
0619     }
0620 
0621     /* build the entry of long file name */
0622     cksum = fat_checksum(msdos_name);
0623 
0624     *nr_slots = usize / 13;
0625     for (ps = slots, i = *nr_slots; i > 0; i--, ps++) {
0626         ps->id = i;
0627         ps->attr = ATTR_EXT;
0628         ps->reserved = 0;
0629         ps->alias_checksum = cksum;
0630         ps->start = 0;
0631         offset = (i - 1) * 13;
0632         fatwchar_to16(ps->name0_4, uname + offset, 5);
0633         fatwchar_to16(ps->name5_10, uname + offset + 5, 6);
0634         fatwchar_to16(ps->name11_12, uname + offset + 11, 2);
0635     }
0636     slots[0].id |= 0x40;
0637     de = (struct msdos_dir_entry *)ps;
0638 
0639 shortname:
0640     /* build the entry of 8.3 alias name */
0641     (*nr_slots)++;
0642     memcpy(de->name, msdos_name, MSDOS_NAME);
0643     de->attr = is_dir ? ATTR_DIR : ATTR_ARCH;
0644     de->lcase = lcase;
0645     fat_time_unix2fat(sbi, ts, &time, &date, &time_cs);
0646     de->time = de->ctime = time;
0647     de->date = de->cdate = de->adate = date;
0648     de->ctime_cs = time_cs;
0649     fat_set_start(de, cluster);
0650     de->size = 0;
0651 out_free:
0652     __putname(uname);
0653     return err;
0654 }
0655 
0656 static int vfat_add_entry(struct inode *dir, const struct qstr *qname,
0657               int is_dir, int cluster, struct timespec64 *ts,
0658               struct fat_slot_info *sinfo)
0659 {
0660     struct msdos_dir_slot *slots;
0661     unsigned int len;
0662     int err, nr_slots;
0663 
0664     len = vfat_striptail_len(qname);
0665     if (len == 0)
0666         return -ENOENT;
0667 
0668     slots = kmalloc_array(MSDOS_SLOTS, sizeof(*slots), GFP_NOFS);
0669     if (slots == NULL)
0670         return -ENOMEM;
0671 
0672     err = vfat_build_slots(dir, qname->name, len, is_dir, cluster, ts,
0673                    slots, &nr_slots);
0674     if (err)
0675         goto cleanup;
0676 
0677     err = fat_add_entries(dir, slots, nr_slots, sinfo);
0678     if (err)
0679         goto cleanup;
0680 
0681     /* update timestamp */
0682     fat_truncate_time(dir, ts, S_CTIME|S_MTIME);
0683     if (IS_DIRSYNC(dir))
0684         (void)fat_sync_inode(dir);
0685     else
0686         mark_inode_dirty(dir);
0687 cleanup:
0688     kfree(slots);
0689     return err;
0690 }
0691 
0692 static int vfat_find(struct inode *dir, const struct qstr *qname,
0693              struct fat_slot_info *sinfo)
0694 {
0695     unsigned int len = vfat_striptail_len(qname);
0696     if (len == 0)
0697         return -ENOENT;
0698     return fat_search_long(dir, qname->name, len, sinfo);
0699 }
0700 
0701 static struct dentry *vfat_lookup(struct inode *dir, struct dentry *dentry,
0702                   unsigned int flags)
0703 {
0704     struct super_block *sb = dir->i_sb;
0705     struct fat_slot_info sinfo;
0706     struct inode *inode;
0707     struct dentry *alias;
0708     int err;
0709 
0710     mutex_lock(&MSDOS_SB(sb)->s_lock);
0711 
0712     err = vfat_find(dir, &dentry->d_name, &sinfo);
0713     if (err) {
0714         if (err == -ENOENT) {
0715             inode = NULL;
0716             goto out;
0717         }
0718         goto error;
0719     }
0720 
0721     inode = fat_build_inode(sb, sinfo.de, sinfo.i_pos);
0722     brelse(sinfo.bh);
0723     if (IS_ERR(inode)) {
0724         err = PTR_ERR(inode);
0725         goto error;
0726     }
0727 
0728     alias = d_find_alias(inode);
0729     /*
0730      * Checking "alias->d_parent == dentry->d_parent" to make sure
0731      * FS is not corrupted (especially double linked dir).
0732      */
0733     if (alias && alias->d_parent == dentry->d_parent) {
0734         /*
0735          * This inode has non anonymous-DCACHE_DISCONNECTED
0736          * dentry. This means, the user did ->lookup() by an
0737          * another name (longname vs 8.3 alias of it) in past.
0738          *
0739          * Switch to new one for reason of locality if possible.
0740          */
0741         if (!S_ISDIR(inode->i_mode))
0742             d_move(alias, dentry);
0743         iput(inode);
0744         mutex_unlock(&MSDOS_SB(sb)->s_lock);
0745         return alias;
0746     } else
0747         dput(alias);
0748 
0749 out:
0750     mutex_unlock(&MSDOS_SB(sb)->s_lock);
0751     if (!inode)
0752         vfat_d_version_set(dentry, inode_query_iversion(dir));
0753     return d_splice_alias(inode, dentry);
0754 error:
0755     mutex_unlock(&MSDOS_SB(sb)->s_lock);
0756     return ERR_PTR(err);
0757 }
0758 
0759 static int vfat_create(struct user_namespace *mnt_userns, struct inode *dir,
0760                struct dentry *dentry, umode_t mode, bool excl)
0761 {
0762     struct super_block *sb = dir->i_sb;
0763     struct inode *inode;
0764     struct fat_slot_info sinfo;
0765     struct timespec64 ts;
0766     int err;
0767 
0768     mutex_lock(&MSDOS_SB(sb)->s_lock);
0769 
0770     ts = current_time(dir);
0771     err = vfat_add_entry(dir, &dentry->d_name, 0, 0, &ts, &sinfo);
0772     if (err)
0773         goto out;
0774     inode_inc_iversion(dir);
0775 
0776     inode = fat_build_inode(sb, sinfo.de, sinfo.i_pos);
0777     brelse(sinfo.bh);
0778     if (IS_ERR(inode)) {
0779         err = PTR_ERR(inode);
0780         goto out;
0781     }
0782     inode_inc_iversion(inode);
0783 
0784     d_instantiate(dentry, inode);
0785 out:
0786     mutex_unlock(&MSDOS_SB(sb)->s_lock);
0787     return err;
0788 }
0789 
0790 static int vfat_rmdir(struct inode *dir, struct dentry *dentry)
0791 {
0792     struct inode *inode = d_inode(dentry);
0793     struct super_block *sb = dir->i_sb;
0794     struct fat_slot_info sinfo;
0795     int err;
0796 
0797     mutex_lock(&MSDOS_SB(sb)->s_lock);
0798 
0799     err = fat_dir_empty(inode);
0800     if (err)
0801         goto out;
0802     err = vfat_find(dir, &dentry->d_name, &sinfo);
0803     if (err)
0804         goto out;
0805 
0806     err = fat_remove_entries(dir, &sinfo);  /* and releases bh */
0807     if (err)
0808         goto out;
0809     drop_nlink(dir);
0810 
0811     clear_nlink(inode);
0812     fat_truncate_time(inode, NULL, S_ATIME|S_MTIME);
0813     fat_detach(inode);
0814     vfat_d_version_set(dentry, inode_query_iversion(dir));
0815 out:
0816     mutex_unlock(&MSDOS_SB(sb)->s_lock);
0817 
0818     return err;
0819 }
0820 
0821 static int vfat_unlink(struct inode *dir, struct dentry *dentry)
0822 {
0823     struct inode *inode = d_inode(dentry);
0824     struct super_block *sb = dir->i_sb;
0825     struct fat_slot_info sinfo;
0826     int err;
0827 
0828     mutex_lock(&MSDOS_SB(sb)->s_lock);
0829 
0830     err = vfat_find(dir, &dentry->d_name, &sinfo);
0831     if (err)
0832         goto out;
0833 
0834     err = fat_remove_entries(dir, &sinfo);  /* and releases bh */
0835     if (err)
0836         goto out;
0837     clear_nlink(inode);
0838     fat_truncate_time(inode, NULL, S_ATIME|S_MTIME);
0839     fat_detach(inode);
0840     vfat_d_version_set(dentry, inode_query_iversion(dir));
0841 out:
0842     mutex_unlock(&MSDOS_SB(sb)->s_lock);
0843 
0844     return err;
0845 }
0846 
0847 static int vfat_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
0848               struct dentry *dentry, umode_t mode)
0849 {
0850     struct super_block *sb = dir->i_sb;
0851     struct inode *inode;
0852     struct fat_slot_info sinfo;
0853     struct timespec64 ts;
0854     int err, cluster;
0855 
0856     mutex_lock(&MSDOS_SB(sb)->s_lock);
0857 
0858     ts = current_time(dir);
0859     cluster = fat_alloc_new_dir(dir, &ts);
0860     if (cluster < 0) {
0861         err = cluster;
0862         goto out;
0863     }
0864     err = vfat_add_entry(dir, &dentry->d_name, 1, cluster, &ts, &sinfo);
0865     if (err)
0866         goto out_free;
0867     inode_inc_iversion(dir);
0868     inc_nlink(dir);
0869 
0870     inode = fat_build_inode(sb, sinfo.de, sinfo.i_pos);
0871     brelse(sinfo.bh);
0872     if (IS_ERR(inode)) {
0873         err = PTR_ERR(inode);
0874         /* the directory was completed, just return a error */
0875         goto out;
0876     }
0877     inode_inc_iversion(inode);
0878     set_nlink(inode, 2);
0879 
0880     d_instantiate(dentry, inode);
0881 
0882     mutex_unlock(&MSDOS_SB(sb)->s_lock);
0883     return 0;
0884 
0885 out_free:
0886     fat_free_clusters(dir, cluster);
0887 out:
0888     mutex_unlock(&MSDOS_SB(sb)->s_lock);
0889     return err;
0890 }
0891 
0892 static int vfat_get_dotdot_de(struct inode *inode, struct buffer_head **bh,
0893                   struct msdos_dir_entry **de)
0894 {
0895     if (S_ISDIR(inode->i_mode)) {
0896         if (fat_get_dotdot_entry(inode, bh, de))
0897             return -EIO;
0898     }
0899     return 0;
0900 }
0901 
0902 static int vfat_sync_ipos(struct inode *dir, struct inode *inode)
0903 {
0904     if (IS_DIRSYNC(dir))
0905         return fat_sync_inode(inode);
0906     mark_inode_dirty(inode);
0907     return 0;
0908 }
0909 
0910 static int vfat_update_dotdot_de(struct inode *dir, struct inode *inode,
0911                  struct buffer_head *dotdot_bh,
0912                  struct msdos_dir_entry *dotdot_de)
0913 {
0914     fat_set_start(dotdot_de, MSDOS_I(dir)->i_logstart);
0915     mark_buffer_dirty_inode(dotdot_bh, inode);
0916     if (IS_DIRSYNC(dir))
0917         return sync_dirty_buffer(dotdot_bh);
0918     return 0;
0919 }
0920 
0921 static void vfat_update_dir_metadata(struct inode *dir, struct timespec64 *ts)
0922 {
0923     inode_inc_iversion(dir);
0924     fat_truncate_time(dir, ts, S_CTIME | S_MTIME);
0925     if (IS_DIRSYNC(dir))
0926         (void)fat_sync_inode(dir);
0927     else
0928         mark_inode_dirty(dir);
0929 }
0930 
0931 static int vfat_rename(struct inode *old_dir, struct dentry *old_dentry,
0932                struct inode *new_dir, struct dentry *new_dentry)
0933 {
0934     struct buffer_head *dotdot_bh;
0935     struct msdos_dir_entry *dotdot_de = NULL;
0936     struct inode *old_inode, *new_inode;
0937     struct fat_slot_info old_sinfo, sinfo;
0938     struct timespec64 ts;
0939     loff_t new_i_pos;
0940     int err, is_dir, corrupt = 0;
0941     struct super_block *sb = old_dir->i_sb;
0942 
0943     old_sinfo.bh = sinfo.bh = dotdot_bh = NULL;
0944     old_inode = d_inode(old_dentry);
0945     new_inode = d_inode(new_dentry);
0946     mutex_lock(&MSDOS_SB(sb)->s_lock);
0947     err = vfat_find(old_dir, &old_dentry->d_name, &old_sinfo);
0948     if (err)
0949         goto out;
0950 
0951     if (old_dir != new_dir) {
0952         err = vfat_get_dotdot_de(old_inode, &dotdot_bh, &dotdot_de);
0953         if (err)
0954             goto out;
0955     }
0956 
0957     is_dir = S_ISDIR(old_inode->i_mode);
0958     ts = current_time(old_dir);
0959     if (new_inode) {
0960         if (is_dir) {
0961             err = fat_dir_empty(new_inode);
0962             if (err)
0963                 goto out;
0964         }
0965         new_i_pos = MSDOS_I(new_inode)->i_pos;
0966         fat_detach(new_inode);
0967     } else {
0968         err = vfat_add_entry(new_dir, &new_dentry->d_name, is_dir, 0,
0969                      &ts, &sinfo);
0970         if (err)
0971             goto out;
0972         new_i_pos = sinfo.i_pos;
0973     }
0974     inode_inc_iversion(new_dir);
0975 
0976     fat_detach(old_inode);
0977     fat_attach(old_inode, new_i_pos);
0978     err = vfat_sync_ipos(new_dir, old_inode);
0979     if (err)
0980         goto error_inode;
0981 
0982     if (dotdot_de) {
0983         err = vfat_update_dotdot_de(new_dir, old_inode, dotdot_bh,
0984                         dotdot_de);
0985         if (err)
0986             goto error_dotdot;
0987         drop_nlink(old_dir);
0988         if (!new_inode)
0989             inc_nlink(new_dir);
0990     }
0991 
0992     err = fat_remove_entries(old_dir, &old_sinfo);  /* and releases bh */
0993     old_sinfo.bh = NULL;
0994     if (err)
0995         goto error_dotdot;
0996     vfat_update_dir_metadata(old_dir, &ts);
0997 
0998     if (new_inode) {
0999         drop_nlink(new_inode);
1000         if (is_dir)
1001             drop_nlink(new_inode);
1002         fat_truncate_time(new_inode, &ts, S_CTIME);
1003     }
1004 out:
1005     brelse(sinfo.bh);
1006     brelse(dotdot_bh);
1007     brelse(old_sinfo.bh);
1008     mutex_unlock(&MSDOS_SB(sb)->s_lock);
1009 
1010     return err;
1011 
1012 error_dotdot:
1013     /* data cluster is shared, serious corruption */
1014     corrupt = 1;
1015 
1016     if (dotdot_de) {
1017         corrupt |= vfat_update_dotdot_de(old_dir, old_inode, dotdot_bh,
1018                          dotdot_de);
1019     }
1020 error_inode:
1021     fat_detach(old_inode);
1022     fat_attach(old_inode, old_sinfo.i_pos);
1023     if (new_inode) {
1024         fat_attach(new_inode, new_i_pos);
1025         if (corrupt)
1026             corrupt |= fat_sync_inode(new_inode);
1027     } else {
1028         /*
1029          * If new entry was not sharing the data cluster, it
1030          * shouldn't be serious corruption.
1031          */
1032         int err2 = fat_remove_entries(new_dir, &sinfo);
1033         if (corrupt)
1034             corrupt |= err2;
1035         sinfo.bh = NULL;
1036     }
1037     if (corrupt < 0) {
1038         fat_fs_error(new_dir->i_sb,
1039                  "%s: Filesystem corrupted (i_pos %lld)",
1040                  __func__, sinfo.i_pos);
1041     }
1042     goto out;
1043 }
1044 
1045 static void vfat_exchange_ipos(struct inode *old_inode, struct inode *new_inode,
1046                    loff_t old_i_pos, loff_t new_i_pos)
1047 {
1048     fat_detach(old_inode);
1049     fat_detach(new_inode);
1050     fat_attach(old_inode, new_i_pos);
1051     fat_attach(new_inode, old_i_pos);
1052 }
1053 
1054 static void vfat_move_nlink(struct inode *src, struct inode *dst)
1055 {
1056     drop_nlink(src);
1057     inc_nlink(dst);
1058 }
1059 
1060 static int vfat_rename_exchange(struct inode *old_dir, struct dentry *old_dentry,
1061                 struct inode *new_dir, struct dentry *new_dentry)
1062 {
1063     struct buffer_head *old_dotdot_bh = NULL, *new_dotdot_bh = NULL;
1064     struct msdos_dir_entry *old_dotdot_de = NULL, *new_dotdot_de = NULL;
1065     struct inode *old_inode, *new_inode;
1066     struct timespec64 ts = current_time(old_dir);
1067     loff_t old_i_pos, new_i_pos;
1068     int err, corrupt = 0;
1069     struct super_block *sb = old_dir->i_sb;
1070 
1071     old_inode = d_inode(old_dentry);
1072     new_inode = d_inode(new_dentry);
1073 
1074     /* Acquire super block lock for the operation to be atomic */
1075     mutex_lock(&MSDOS_SB(sb)->s_lock);
1076 
1077     /* if directories are not the same, get ".." info to update */
1078     if (old_dir != new_dir) {
1079         err = vfat_get_dotdot_de(old_inode, &old_dotdot_bh,
1080                      &old_dotdot_de);
1081         if (err)
1082             goto out;
1083 
1084         err = vfat_get_dotdot_de(new_inode, &new_dotdot_bh,
1085                      &new_dotdot_de);
1086         if (err)
1087             goto out;
1088     }
1089 
1090     old_i_pos = MSDOS_I(old_inode)->i_pos;
1091     new_i_pos = MSDOS_I(new_inode)->i_pos;
1092 
1093     vfat_exchange_ipos(old_inode, new_inode, old_i_pos, new_i_pos);
1094 
1095     err = vfat_sync_ipos(old_dir, new_inode);
1096     if (err)
1097         goto error_exchange;
1098     err = vfat_sync_ipos(new_dir, old_inode);
1099     if (err)
1100         goto error_exchange;
1101 
1102     /* update ".." directory entry info */
1103     if (old_dotdot_de) {
1104         err = vfat_update_dotdot_de(new_dir, old_inode, old_dotdot_bh,
1105                         old_dotdot_de);
1106         if (err)
1107             goto error_old_dotdot;
1108     }
1109     if (new_dotdot_de) {
1110         err = vfat_update_dotdot_de(old_dir, new_inode, new_dotdot_bh,
1111                         new_dotdot_de);
1112         if (err)
1113             goto error_new_dotdot;
1114     }
1115 
1116     /* if cross directory and only one is a directory, adjust nlink */
1117     if (!old_dotdot_de != !new_dotdot_de) {
1118         if (old_dotdot_de)
1119             vfat_move_nlink(old_dir, new_dir);
1120         else
1121             vfat_move_nlink(new_dir, old_dir);
1122     }
1123 
1124     vfat_update_dir_metadata(old_dir, &ts);
1125     /* if directories are not the same, update new_dir as well */
1126     if (old_dir != new_dir)
1127         vfat_update_dir_metadata(new_dir, &ts);
1128 
1129 out:
1130     brelse(old_dotdot_bh);
1131     brelse(new_dotdot_bh);
1132     mutex_unlock(&MSDOS_SB(sb)->s_lock);
1133 
1134     return err;
1135 
1136 error_new_dotdot:
1137     if (new_dotdot_de) {
1138         corrupt |= vfat_update_dotdot_de(new_dir, new_inode,
1139                          new_dotdot_bh, new_dotdot_de);
1140     }
1141 
1142 error_old_dotdot:
1143     if (old_dotdot_de) {
1144         corrupt |= vfat_update_dotdot_de(old_dir, old_inode,
1145                          old_dotdot_bh, old_dotdot_de);
1146     }
1147 
1148 error_exchange:
1149     vfat_exchange_ipos(old_inode, new_inode, new_i_pos, old_i_pos);
1150     corrupt |= vfat_sync_ipos(new_dir, new_inode);
1151     corrupt |= vfat_sync_ipos(old_dir, old_inode);
1152 
1153     if (corrupt < 0) {
1154         fat_fs_error(new_dir->i_sb,
1155                  "%s: Filesystem corrupted (i_pos %lld, %lld)",
1156                  __func__, old_i_pos, new_i_pos);
1157     }
1158     goto out;
1159 }
1160 
1161 static int vfat_rename2(struct user_namespace *mnt_userns, struct inode *old_dir,
1162             struct dentry *old_dentry, struct inode *new_dir,
1163             struct dentry *new_dentry, unsigned int flags)
1164 {
1165     if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
1166         return -EINVAL;
1167 
1168     if (flags & RENAME_EXCHANGE) {
1169         return vfat_rename_exchange(old_dir, old_dentry,
1170                         new_dir, new_dentry);
1171     }
1172 
1173     /* VFS already handled RENAME_NOREPLACE, handle it as a normal rename */
1174     return vfat_rename(old_dir, old_dentry, new_dir, new_dentry);
1175 }
1176 
1177 static const struct inode_operations vfat_dir_inode_operations = {
1178     .create     = vfat_create,
1179     .lookup     = vfat_lookup,
1180     .unlink     = vfat_unlink,
1181     .mkdir      = vfat_mkdir,
1182     .rmdir      = vfat_rmdir,
1183     .rename     = vfat_rename2,
1184     .setattr    = fat_setattr,
1185     .getattr    = fat_getattr,
1186     .update_time    = fat_update_time,
1187 };
1188 
1189 static void setup(struct super_block *sb)
1190 {
1191     MSDOS_SB(sb)->dir_ops = &vfat_dir_inode_operations;
1192     if (MSDOS_SB(sb)->options.name_check != 's')
1193         sb->s_d_op = &vfat_ci_dentry_ops;
1194     else
1195         sb->s_d_op = &vfat_dentry_ops;
1196 }
1197 
1198 static int vfat_fill_super(struct super_block *sb, void *data, int silent)
1199 {
1200     return fat_fill_super(sb, data, silent, 1, setup);
1201 }
1202 
1203 static struct dentry *vfat_mount(struct file_system_type *fs_type,
1204                int flags, const char *dev_name,
1205                void *data)
1206 {
1207     return mount_bdev(fs_type, flags, dev_name, data, vfat_fill_super);
1208 }
1209 
1210 static struct file_system_type vfat_fs_type = {
1211     .owner      = THIS_MODULE,
1212     .name       = "vfat",
1213     .mount      = vfat_mount,
1214     .kill_sb    = kill_block_super,
1215     .fs_flags   = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
1216 };
1217 MODULE_ALIAS_FS("vfat");
1218 
1219 static int __init init_vfat_fs(void)
1220 {
1221     return register_filesystem(&vfat_fs_type);
1222 }
1223 
1224 static void __exit exit_vfat_fs(void)
1225 {
1226     unregister_filesystem(&vfat_fs_type);
1227 }
1228 
1229 MODULE_LICENSE("GPL");
1230 MODULE_DESCRIPTION("VFAT filesystem support");
1231 MODULE_AUTHOR("Gordon Chaffee");
1232 
1233 module_init(init_vfat_fs)
1234 module_exit(exit_vfat_fs)