Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/msdos/namei.c |
| 3 | * |
| 4 | * Written 1992,1993 by Werner Almesberger |
| 5 | * Hidden files 1995 by Albert Cahalan <albert@ccs.neu.edu> <adc@coe.neu.edu> |
| 6 | * Rewritten for constant inumbers 1999 by Al Viro |
| 7 | */ |
| 8 | |
| 9 | #include <linux/module.h> |
OGAWA Hirofumi | 9e975da | 2008-11-06 12:53:46 -0800 | [diff] [blame] | 10 | #include "fat.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | /* Characters that are undesirable in an MS-DOS file name */ |
| 13 | static unsigned char bad_chars[] = "*?<>|\""; |
Rene Scharfe | 7557bc6 | 2008-07-25 01:46:45 -0700 | [diff] [blame] | 14 | static unsigned char bad_if_strict[] = "+=,; "; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
| 16 | /***** Formats an MS-DOS file name. Rejects invalid names. */ |
| 17 | static int msdos_format_name(const unsigned char *name, int len, |
| 18 | unsigned char *res, struct fat_mount_options *opts) |
| 19 | /* |
| 20 | * name is the proposed name, len is its length, res is |
| 21 | * the resulting name, opts->name_check is either (r)elaxed, |
| 22 | * (n)ormal or (s)trict, opts->dotsOK allows dots at the |
| 23 | * beginning of name (for hidden files) |
| 24 | */ |
| 25 | { |
| 26 | unsigned char *walk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | unsigned char c; |
| 28 | int space; |
| 29 | |
| 30 | if (name[0] == '.') { /* dotfile because . and .. already done */ |
| 31 | if (opts->dotsOK) { |
| 32 | /* Get rid of dot - test for it elsewhere */ |
| 33 | name++; |
| 34 | len--; |
Rene Scharfe | 7557bc6 | 2008-07-25 01:46:45 -0700 | [diff] [blame] | 35 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | return -EINVAL; |
| 37 | } |
| 38 | /* |
Rene Scharfe | 7557bc6 | 2008-07-25 01:46:45 -0700 | [diff] [blame] | 39 | * disallow names that _really_ start with a dot |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | */ |
Rene Scharfe | 7557bc6 | 2008-07-25 01:46:45 -0700 | [diff] [blame] | 41 | space = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | c = 0; |
| 43 | for (walk = res; len && walk - res < 8; walk++) { |
| 44 | c = *name++; |
| 45 | len--; |
| 46 | if (opts->name_check != 'r' && strchr(bad_chars, c)) |
| 47 | return -EINVAL; |
Rene Scharfe | 7557bc6 | 2008-07-25 01:46:45 -0700 | [diff] [blame] | 48 | if (opts->name_check == 's' && strchr(bad_if_strict, c)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | return -EINVAL; |
| 50 | if (c >= 'A' && c <= 'Z' && opts->name_check == 's') |
| 51 | return -EINVAL; |
| 52 | if (c < ' ' || c == ':' || c == '\\') |
| 53 | return -EINVAL; |
| 54 | /* |
| 55 | * 0xE5 is legal as a first character, but we must substitute |
| 56 | * 0x05 because 0xE5 marks deleted files. Yes, DOS really |
| 57 | * does this. |
| 58 | * It seems that Microsoft hacked DOS to support non-US |
| 59 | * characters after the 0xE5 character was already in use to |
| 60 | * mark deleted files. |
| 61 | */ |
| 62 | if ((res == walk) && (c == 0xE5)) |
| 63 | c = 0x05; |
| 64 | if (c == '.') |
| 65 | break; |
| 66 | space = (c == ' '); |
| 67 | *walk = (!opts->nocase && c >= 'a' && c <= 'z') ? c - 32 : c; |
| 68 | } |
| 69 | if (space) |
| 70 | return -EINVAL; |
| 71 | if (opts->name_check == 's' && len && c != '.') { |
| 72 | c = *name++; |
| 73 | len--; |
| 74 | if (c != '.') |
| 75 | return -EINVAL; |
| 76 | } |
| 77 | while (c != '.' && len--) |
| 78 | c = *name++; |
| 79 | if (c == '.') { |
| 80 | while (walk - res < 8) |
| 81 | *walk++ = ' '; |
| 82 | while (len > 0 && walk - res < MSDOS_NAME) { |
| 83 | c = *name++; |
| 84 | len--; |
| 85 | if (opts->name_check != 'r' && strchr(bad_chars, c)) |
| 86 | return -EINVAL; |
| 87 | if (opts->name_check == 's' && |
Rene Scharfe | 7557bc6 | 2008-07-25 01:46:45 -0700 | [diff] [blame] | 88 | strchr(bad_if_strict, c)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | return -EINVAL; |
| 90 | if (c < ' ' || c == ':' || c == '\\') |
| 91 | return -EINVAL; |
| 92 | if (c == '.') { |
| 93 | if (opts->name_check == 's') |
| 94 | return -EINVAL; |
| 95 | break; |
| 96 | } |
| 97 | if (c >= 'A' && c <= 'Z' && opts->name_check == 's') |
| 98 | return -EINVAL; |
| 99 | space = c == ' '; |
| 100 | if (!opts->nocase && c >= 'a' && c <= 'z') |
| 101 | *walk++ = c - 32; |
| 102 | else |
| 103 | *walk++ = c; |
| 104 | } |
| 105 | if (space) |
| 106 | return -EINVAL; |
| 107 | if (opts->name_check == 's' && len) |
| 108 | return -EINVAL; |
| 109 | } |
| 110 | while (walk - res < MSDOS_NAME) |
| 111 | *walk++ = ' '; |
OGAWA Hirofumi | 094e320 | 2006-03-31 02:30:53 -0800 | [diff] [blame] | 112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | /***** Locates a directory entry. Uses unformatted name. */ |
| 117 | static int msdos_find(struct inode *dir, const unsigned char *name, int len, |
| 118 | struct fat_slot_info *sinfo) |
| 119 | { |
| 120 | struct msdos_sb_info *sbi = MSDOS_SB(dir->i_sb); |
| 121 | unsigned char msdos_name[MSDOS_NAME]; |
| 122 | int err; |
| 123 | |
| 124 | err = msdos_format_name(name, len, msdos_name, &sbi->options); |
| 125 | if (err) |
| 126 | return -ENOENT; |
| 127 | |
| 128 | err = fat_scan(dir, msdos_name, sinfo); |
| 129 | if (!err && sbi->options.dotsOK) { |
| 130 | if (name[0] == '.') { |
| 131 | if (!(sinfo->de->attr & ATTR_HIDDEN)) |
| 132 | err = -ENOENT; |
| 133 | } else { |
| 134 | if (sinfo->de->attr & ATTR_HIDDEN) |
| 135 | err = -ENOENT; |
| 136 | } |
| 137 | if (err) |
| 138 | brelse(sinfo->bh); |
| 139 | } |
| 140 | return err; |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * Compute the hash for the msdos name corresponding to the dentry. |
| 145 | * Note: if the name is invalid, we leave the hash code unchanged so |
| 146 | * that the existing dentry can be used. The msdos fs routines will |
| 147 | * return ENOENT or EINVAL as appropriate. |
| 148 | */ |
Linus Torvalds | da53be1 | 2013-05-21 15:22:44 -0700 | [diff] [blame] | 149 | static int msdos_hash(const struct dentry *dentry, struct qstr *qstr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | { |
| 151 | struct fat_mount_options *options = &MSDOS_SB(dentry->d_sb)->options; |
| 152 | unsigned char msdos_name[MSDOS_NAME]; |
| 153 | int error; |
| 154 | |
| 155 | error = msdos_format_name(qstr->name, qstr->len, msdos_name, options); |
| 156 | if (!error) |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 157 | qstr->hash = full_name_hash(dentry, msdos_name, MSDOS_NAME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | * Compare two msdos names. If either of the names are invalid, |
| 163 | * we fall back to doing the standard name comparison. |
| 164 | */ |
Al Viro | 6fa67e7 | 2016-07-31 16:37:25 -0400 | [diff] [blame] | 165 | static int msdos_cmp(const struct dentry *dentry, |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 166 | unsigned int len, const char *str, const struct qstr *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | { |
Al Viro | d3fe198 | 2016-07-29 18:23:59 -0400 | [diff] [blame] | 168 | struct fat_mount_options *options = &MSDOS_SB(dentry->d_sb)->options; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | unsigned char a_msdos_name[MSDOS_NAME], b_msdos_name[MSDOS_NAME]; |
| 170 | int error; |
| 171 | |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 172 | error = msdos_format_name(name->name, name->len, a_msdos_name, options); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | if (error) |
| 174 | goto old_compare; |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 175 | error = msdos_format_name(str, len, b_msdos_name, options); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | if (error) |
| 177 | goto old_compare; |
| 178 | error = memcmp(a_msdos_name, b_msdos_name, MSDOS_NAME); |
| 179 | out: |
| 180 | return error; |
| 181 | |
| 182 | old_compare: |
| 183 | error = 1; |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 184 | if (name->len == len) |
| 185 | error = memcmp(name->name, str, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | goto out; |
| 187 | } |
| 188 | |
Al Viro | ce6cdc4 | 2009-02-20 05:59:46 +0000 | [diff] [blame] | 189 | static const struct dentry_operations msdos_dentry_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | .d_hash = msdos_hash, |
| 191 | .d_compare = msdos_cmp, |
| 192 | }; |
| 193 | |
| 194 | /* |
| 195 | * AV. Wrappers for FAT sb operations. Is it wise? |
| 196 | */ |
| 197 | |
| 198 | /***** Get inode using directory and name */ |
| 199 | static struct dentry *msdos_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 200 | unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | { |
| 202 | struct super_block *sb = dir->i_sb; |
| 203 | struct fat_slot_info sinfo; |
OGAWA Hirofumi | 45cfbe3 | 2008-11-06 12:53:53 -0800 | [diff] [blame] | 204 | struct inode *inode; |
| 205 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
Marco Stornelli | e40b34c | 2012-10-06 12:40:03 +0200 | [diff] [blame] | 207 | mutex_lock(&MSDOS_SB(sb)->s_lock); |
OGAWA Hirofumi | 45cfbe3 | 2008-11-06 12:53:53 -0800 | [diff] [blame] | 208 | err = msdos_find(dir, dentry->d_name.name, dentry->d_name.len, &sinfo); |
Al Viro | a904937 | 2011-07-08 21:20:11 -0400 | [diff] [blame] | 209 | switch (err) { |
| 210 | case -ENOENT: |
| 211 | inode = NULL; |
| 212 | break; |
| 213 | case 0: |
| 214 | inode = fat_build_inode(sb, sinfo.de, sinfo.i_pos); |
| 215 | brelse(sinfo.bh); |
| 216 | break; |
| 217 | default: |
| 218 | inode = ERR_PTR(err); |
OGAWA Hirofumi | 45cfbe3 | 2008-11-06 12:53:53 -0800 | [diff] [blame] | 219 | } |
Marco Stornelli | e40b34c | 2012-10-06 12:40:03 +0200 | [diff] [blame] | 220 | mutex_unlock(&MSDOS_SB(sb)->s_lock); |
Al Viro | 3d23985 | 2010-12-18 10:44:00 -0500 | [diff] [blame] | 221 | return d_splice_alias(inode, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /***** Creates a directory entry (name is already formatted). */ |
| 225 | static int msdos_add_entry(struct inode *dir, const unsigned char *name, |
| 226 | int is_dir, int is_hid, int cluster, |
| 227 | struct timespec *ts, struct fat_slot_info *sinfo) |
| 228 | { |
Joe Peterson | b271e06 | 2008-07-25 01:46:47 -0700 | [diff] [blame] | 229 | struct msdos_sb_info *sbi = MSDOS_SB(dir->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | struct msdos_dir_entry de; |
| 231 | __le16 time, date; |
| 232 | int err; |
| 233 | |
| 234 | memcpy(de.name, name, MSDOS_NAME); |
| 235 | de.attr = is_dir ? ATTR_DIR : ATTR_ARCH; |
| 236 | if (is_hid) |
| 237 | de.attr |= ATTR_HIDDEN; |
| 238 | de.lcase = 0; |
OGAWA Hirofumi | 7decd1c | 2008-11-06 12:53:47 -0800 | [diff] [blame] | 239 | fat_time_unix2fat(sbi, ts, &time, &date, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | de.cdate = de.adate = 0; |
| 241 | de.ctime = 0; |
| 242 | de.ctime_cs = 0; |
| 243 | de.time = time; |
| 244 | de.date = date; |
Steven J. Magnani | a943ed7 | 2012-07-30 14:42:13 -0700 | [diff] [blame] | 245 | fat_set_start(&de, cluster); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | de.size = 0; |
| 247 | |
| 248 | err = fat_add_entries(dir, &de, 1, sinfo); |
| 249 | if (err) |
| 250 | return err; |
| 251 | |
| 252 | dir->i_ctime = dir->i_mtime = *ts; |
| 253 | if (IS_DIRSYNC(dir)) |
| 254 | (void)fat_sync_inode(dir); |
| 255 | else |
| 256 | mark_inode_dirty(dir); |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | /***** Create a file */ |
Al Viro | 4acdaf2 | 2011-07-26 01:42:34 -0400 | [diff] [blame] | 262 | static int msdos_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 263 | bool excl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | { |
| 265 | struct super_block *sb = dir->i_sb; |
Chris Mason | ae78bf9 | 2006-09-29 02:00:03 -0700 | [diff] [blame] | 266 | struct inode *inode = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | struct fat_slot_info sinfo; |
| 268 | struct timespec ts; |
| 269 | unsigned char msdos_name[MSDOS_NAME]; |
| 270 | int err, is_hid; |
| 271 | |
Marco Stornelli | e40b34c | 2012-10-06 12:40:03 +0200 | [diff] [blame] | 272 | mutex_lock(&MSDOS_SB(sb)->s_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | |
| 274 | err = msdos_format_name(dentry->d_name.name, dentry->d_name.len, |
| 275 | msdos_name, &MSDOS_SB(sb)->options); |
| 276 | if (err) |
| 277 | goto out; |
| 278 | is_hid = (dentry->d_name.name[0] == '.') && (msdos_name[0] != '.'); |
| 279 | /* Have to do it due to foo vs. .foo conflicts */ |
| 280 | if (!fat_scan(dir, msdos_name, &sinfo)) { |
| 281 | brelse(sinfo.bh); |
| 282 | err = -EINVAL; |
| 283 | goto out; |
| 284 | } |
| 285 | |
| 286 | ts = CURRENT_TIME_SEC; |
| 287 | err = msdos_add_entry(dir, msdos_name, 0, is_hid, 0, &ts, &sinfo); |
| 288 | if (err) |
| 289 | goto out; |
| 290 | inode = fat_build_inode(sb, sinfo.de, sinfo.i_pos); |
| 291 | brelse(sinfo.bh); |
| 292 | if (IS_ERR(inode)) { |
| 293 | err = PTR_ERR(inode); |
| 294 | goto out; |
| 295 | } |
| 296 | inode->i_mtime = inode->i_atime = inode->i_ctime = ts; |
| 297 | /* timestamp is already written, so mark_inode_dirty() is unneeded. */ |
| 298 | |
| 299 | d_instantiate(dentry, inode); |
| 300 | out: |
Marco Stornelli | e40b34c | 2012-10-06 12:40:03 +0200 | [diff] [blame] | 301 | mutex_unlock(&MSDOS_SB(sb)->s_lock); |
Chris Mason | ae78bf9 | 2006-09-29 02:00:03 -0700 | [diff] [blame] | 302 | if (!err) |
| 303 | err = fat_flush_inodes(sb, dir, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | return err; |
| 305 | } |
| 306 | |
| 307 | /***** Remove a directory */ |
| 308 | static int msdos_rmdir(struct inode *dir, struct dentry *dentry) |
| 309 | { |
Linus Torvalds | 8f59342 | 2008-05-19 19:53:01 -0700 | [diff] [blame] | 310 | struct super_block *sb = dir->i_sb; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 311 | struct inode *inode = d_inode(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | struct fat_slot_info sinfo; |
| 313 | int err; |
| 314 | |
Marco Stornelli | e40b34c | 2012-10-06 12:40:03 +0200 | [diff] [blame] | 315 | mutex_lock(&MSDOS_SB(sb)->s_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | /* |
| 317 | * Check whether the directory is not in use, then check |
| 318 | * whether it is empty. |
| 319 | */ |
| 320 | err = fat_dir_empty(inode); |
| 321 | if (err) |
| 322 | goto out; |
| 323 | err = msdos_find(dir, dentry->d_name.name, dentry->d_name.len, &sinfo); |
| 324 | if (err) |
| 325 | goto out; |
| 326 | |
| 327 | err = fat_remove_entries(dir, &sinfo); /* and releases bh */ |
| 328 | if (err) |
| 329 | goto out; |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 330 | drop_nlink(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | |
Dave Hansen | ce71ec3 | 2006-09-30 23:29:06 -0700 | [diff] [blame] | 332 | clear_nlink(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | inode->i_ctime = CURRENT_TIME_SEC; |
| 334 | fat_detach(inode); |
| 335 | out: |
Marco Stornelli | e40b34c | 2012-10-06 12:40:03 +0200 | [diff] [blame] | 336 | mutex_unlock(&MSDOS_SB(sb)->s_lock); |
Chris Mason | ae78bf9 | 2006-09-29 02:00:03 -0700 | [diff] [blame] | 337 | if (!err) |
Linus Torvalds | 8f59342 | 2008-05-19 19:53:01 -0700 | [diff] [blame] | 338 | err = fat_flush_inodes(sb, dir, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | |
| 340 | return err; |
| 341 | } |
| 342 | |
| 343 | /***** Make a directory */ |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 344 | static int msdos_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | { |
| 346 | struct super_block *sb = dir->i_sb; |
| 347 | struct fat_slot_info sinfo; |
| 348 | struct inode *inode; |
| 349 | unsigned char msdos_name[MSDOS_NAME]; |
| 350 | struct timespec ts; |
| 351 | int err, is_hid, cluster; |
| 352 | |
Marco Stornelli | e40b34c | 2012-10-06 12:40:03 +0200 | [diff] [blame] | 353 | mutex_lock(&MSDOS_SB(sb)->s_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | |
| 355 | err = msdos_format_name(dentry->d_name.name, dentry->d_name.len, |
| 356 | msdos_name, &MSDOS_SB(sb)->options); |
| 357 | if (err) |
| 358 | goto out; |
| 359 | is_hid = (dentry->d_name.name[0] == '.') && (msdos_name[0] != '.'); |
| 360 | /* foo vs .foo situation */ |
| 361 | if (!fat_scan(dir, msdos_name, &sinfo)) { |
| 362 | brelse(sinfo.bh); |
| 363 | err = -EINVAL; |
| 364 | goto out; |
| 365 | } |
| 366 | |
| 367 | ts = CURRENT_TIME_SEC; |
| 368 | cluster = fat_alloc_new_dir(dir, &ts); |
| 369 | if (cluster < 0) { |
| 370 | err = cluster; |
| 371 | goto out; |
| 372 | } |
| 373 | err = msdos_add_entry(dir, msdos_name, 1, is_hid, cluster, &ts, &sinfo); |
| 374 | if (err) |
| 375 | goto out_free; |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 376 | inc_nlink(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | |
| 378 | inode = fat_build_inode(sb, sinfo.de, sinfo.i_pos); |
| 379 | brelse(sinfo.bh); |
| 380 | if (IS_ERR(inode)) { |
| 381 | err = PTR_ERR(inode); |
| 382 | /* the directory was completed, just return a error */ |
| 383 | goto out; |
| 384 | } |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 385 | set_nlink(inode, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | inode->i_mtime = inode->i_atime = inode->i_ctime = ts; |
| 387 | /* timestamp is already written, so mark_inode_dirty() is unneeded. */ |
| 388 | |
| 389 | d_instantiate(dentry, inode); |
| 390 | |
Marco Stornelli | e40b34c | 2012-10-06 12:40:03 +0200 | [diff] [blame] | 391 | mutex_unlock(&MSDOS_SB(sb)->s_lock); |
Chris Mason | ae78bf9 | 2006-09-29 02:00:03 -0700 | [diff] [blame] | 392 | fat_flush_inodes(sb, dir, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | return 0; |
| 394 | |
| 395 | out_free: |
| 396 | fat_free_clusters(dir, cluster); |
| 397 | out: |
Marco Stornelli | e40b34c | 2012-10-06 12:40:03 +0200 | [diff] [blame] | 398 | mutex_unlock(&MSDOS_SB(sb)->s_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | return err; |
| 400 | } |
| 401 | |
| 402 | /***** Unlink a file */ |
| 403 | static int msdos_unlink(struct inode *dir, struct dentry *dentry) |
| 404 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 405 | struct inode *inode = d_inode(dentry); |
Cruz Julian Bishop | 85cb9bf | 2012-10-04 17:14:47 -0700 | [diff] [blame] | 406 | struct super_block *sb = inode->i_sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | struct fat_slot_info sinfo; |
| 408 | int err; |
| 409 | |
Marco Stornelli | e40b34c | 2012-10-06 12:40:03 +0200 | [diff] [blame] | 410 | mutex_lock(&MSDOS_SB(sb)->s_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | err = msdos_find(dir, dentry->d_name.name, dentry->d_name.len, &sinfo); |
| 412 | if (err) |
| 413 | goto out; |
| 414 | |
| 415 | err = fat_remove_entries(dir, &sinfo); /* and releases bh */ |
| 416 | if (err) |
| 417 | goto out; |
Dave Hansen | ce71ec3 | 2006-09-30 23:29:06 -0700 | [diff] [blame] | 418 | clear_nlink(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | inode->i_ctime = CURRENT_TIME_SEC; |
| 420 | fat_detach(inode); |
| 421 | out: |
Marco Stornelli | e40b34c | 2012-10-06 12:40:03 +0200 | [diff] [blame] | 422 | mutex_unlock(&MSDOS_SB(sb)->s_lock); |
Chris Mason | ae78bf9 | 2006-09-29 02:00:03 -0700 | [diff] [blame] | 423 | if (!err) |
Linus Torvalds | 8f59342 | 2008-05-19 19:53:01 -0700 | [diff] [blame] | 424 | err = fat_flush_inodes(sb, dir, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | |
| 426 | return err; |
| 427 | } |
| 428 | |
| 429 | static int do_msdos_rename(struct inode *old_dir, unsigned char *old_name, |
| 430 | struct dentry *old_dentry, |
| 431 | struct inode *new_dir, unsigned char *new_name, |
| 432 | struct dentry *new_dentry, int is_hid) |
| 433 | { |
| 434 | struct buffer_head *dotdot_bh; |
| 435 | struct msdos_dir_entry *dotdot_de; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | struct inode *old_inode, *new_inode; |
| 437 | struct fat_slot_info old_sinfo, sinfo; |
| 438 | struct timespec ts; |
Steven J. Magnani | 7669e8f | 2012-10-04 17:14:45 -0700 | [diff] [blame] | 439 | loff_t new_i_pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | int err, old_attrs, is_dir, update_dotdot, corrupt = 0; |
| 441 | |
| 442 | old_sinfo.bh = sinfo.bh = dotdot_bh = NULL; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 443 | old_inode = d_inode(old_dentry); |
| 444 | new_inode = d_inode(new_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | |
| 446 | err = fat_scan(old_dir, old_name, &old_sinfo); |
| 447 | if (err) { |
| 448 | err = -EIO; |
| 449 | goto out; |
| 450 | } |
| 451 | |
| 452 | is_dir = S_ISDIR(old_inode->i_mode); |
| 453 | update_dotdot = (is_dir && old_dir != new_dir); |
| 454 | if (update_dotdot) { |
Steven J. Magnani | 7669e8f | 2012-10-04 17:14:45 -0700 | [diff] [blame] | 455 | if (fat_get_dotdot_entry(old_inode, &dotdot_bh, &dotdot_de)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | err = -EIO; |
| 457 | goto out; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | old_attrs = MSDOS_I(old_inode)->i_attrs; |
| 462 | err = fat_scan(new_dir, new_name, &sinfo); |
| 463 | if (!err) { |
| 464 | if (!new_inode) { |
| 465 | /* "foo" -> ".foo" case. just change the ATTR_HIDDEN */ |
| 466 | if (sinfo.de != old_sinfo.de) { |
| 467 | err = -EINVAL; |
| 468 | goto out; |
| 469 | } |
| 470 | if (is_hid) |
| 471 | MSDOS_I(old_inode)->i_attrs |= ATTR_HIDDEN; |
| 472 | else |
| 473 | MSDOS_I(old_inode)->i_attrs &= ~ATTR_HIDDEN; |
| 474 | if (IS_DIRSYNC(old_dir)) { |
| 475 | err = fat_sync_inode(old_inode); |
| 476 | if (err) { |
| 477 | MSDOS_I(old_inode)->i_attrs = old_attrs; |
| 478 | goto out; |
| 479 | } |
| 480 | } else |
| 481 | mark_inode_dirty(old_inode); |
| 482 | |
| 483 | old_dir->i_version++; |
| 484 | old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME_SEC; |
| 485 | if (IS_DIRSYNC(old_dir)) |
| 486 | (void)fat_sync_inode(old_dir); |
| 487 | else |
| 488 | mark_inode_dirty(old_dir); |
| 489 | goto out; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | ts = CURRENT_TIME_SEC; |
| 494 | if (new_inode) { |
| 495 | if (err) |
| 496 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | if (is_dir) { |
| 498 | err = fat_dir_empty(new_inode); |
| 499 | if (err) |
| 500 | goto out; |
| 501 | } |
OGAWA Hirofumi | 9131dd4 | 2005-10-30 15:03:50 -0800 | [diff] [blame] | 502 | new_i_pos = MSDOS_I(new_inode)->i_pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | fat_detach(new_inode); |
| 504 | } else { |
| 505 | err = msdos_add_entry(new_dir, new_name, is_dir, is_hid, 0, |
| 506 | &ts, &sinfo); |
| 507 | if (err) |
| 508 | goto out; |
OGAWA Hirofumi | 9131dd4 | 2005-10-30 15:03:50 -0800 | [diff] [blame] | 509 | new_i_pos = sinfo.i_pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | } |
| 511 | new_dir->i_version++; |
| 512 | |
| 513 | fat_detach(old_inode); |
OGAWA Hirofumi | 9131dd4 | 2005-10-30 15:03:50 -0800 | [diff] [blame] | 514 | fat_attach(old_inode, new_i_pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | if (is_hid) |
| 516 | MSDOS_I(old_inode)->i_attrs |= ATTR_HIDDEN; |
| 517 | else |
| 518 | MSDOS_I(old_inode)->i_attrs &= ~ATTR_HIDDEN; |
| 519 | if (IS_DIRSYNC(new_dir)) { |
| 520 | err = fat_sync_inode(old_inode); |
| 521 | if (err) |
| 522 | goto error_inode; |
| 523 | } else |
| 524 | mark_inode_dirty(old_inode); |
| 525 | |
| 526 | if (update_dotdot) { |
Steven J. Magnani | a943ed7 | 2012-07-30 14:42:13 -0700 | [diff] [blame] | 527 | fat_set_start(dotdot_de, MSDOS_I(new_dir)->i_logstart); |
Al Viro | b522412 | 2009-06-07 13:44:36 -0400 | [diff] [blame] | 528 | mark_buffer_dirty_inode(dotdot_bh, old_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | if (IS_DIRSYNC(new_dir)) { |
| 530 | err = sync_dirty_buffer(dotdot_bh); |
| 531 | if (err) |
| 532 | goto error_dotdot; |
| 533 | } |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 534 | drop_nlink(old_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | if (!new_inode) |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 536 | inc_nlink(new_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | err = fat_remove_entries(old_dir, &old_sinfo); /* and releases bh */ |
| 540 | old_sinfo.bh = NULL; |
| 541 | if (err) |
| 542 | goto error_dotdot; |
| 543 | old_dir->i_version++; |
| 544 | old_dir->i_ctime = old_dir->i_mtime = ts; |
| 545 | if (IS_DIRSYNC(old_dir)) |
| 546 | (void)fat_sync_inode(old_dir); |
| 547 | else |
| 548 | mark_inode_dirty(old_dir); |
| 549 | |
| 550 | if (new_inode) { |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 551 | drop_nlink(new_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | if (is_dir) |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 553 | drop_nlink(new_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | new_inode->i_ctime = ts; |
| 555 | } |
| 556 | out: |
| 557 | brelse(sinfo.bh); |
| 558 | brelse(dotdot_bh); |
| 559 | brelse(old_sinfo.bh); |
| 560 | return err; |
| 561 | |
| 562 | error_dotdot: |
| 563 | /* data cluster is shared, serious corruption */ |
| 564 | corrupt = 1; |
| 565 | |
| 566 | if (update_dotdot) { |
Steven J. Magnani | a943ed7 | 2012-07-30 14:42:13 -0700 | [diff] [blame] | 567 | fat_set_start(dotdot_de, MSDOS_I(old_dir)->i_logstart); |
Al Viro | b522412 | 2009-06-07 13:44:36 -0400 | [diff] [blame] | 568 | mark_buffer_dirty_inode(dotdot_bh, old_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | corrupt |= sync_dirty_buffer(dotdot_bh); |
| 570 | } |
| 571 | error_inode: |
| 572 | fat_detach(old_inode); |
| 573 | fat_attach(old_inode, old_sinfo.i_pos); |
| 574 | MSDOS_I(old_inode)->i_attrs = old_attrs; |
| 575 | if (new_inode) { |
OGAWA Hirofumi | 9131dd4 | 2005-10-30 15:03:50 -0800 | [diff] [blame] | 576 | fat_attach(new_inode, new_i_pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | if (corrupt) |
| 578 | corrupt |= fat_sync_inode(new_inode); |
| 579 | } else { |
| 580 | /* |
| 581 | * If new entry was not sharing the data cluster, it |
| 582 | * shouldn't be serious corruption. |
| 583 | */ |
| 584 | int err2 = fat_remove_entries(new_dir, &sinfo); |
| 585 | if (corrupt) |
| 586 | corrupt |= err2; |
| 587 | sinfo.bh = NULL; |
| 588 | } |
| 589 | if (corrupt < 0) { |
Denis Karpov | 85c7859 | 2009-06-04 02:34:22 +0900 | [diff] [blame] | 590 | fat_fs_error(new_dir->i_sb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | "%s: Filesystem corrupted (i_pos %lld)", |
Harvey Harrison | 8e24eea | 2008-04-30 00:55:09 -0700 | [diff] [blame] | 592 | __func__, sinfo.i_pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | } |
| 594 | goto out; |
| 595 | } |
| 596 | |
| 597 | /***** Rename, a wrapper for rename_same_dir & rename_diff_dir */ |
| 598 | static int msdos_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 599 | struct inode *new_dir, struct dentry *new_dentry) |
| 600 | { |
Linus Torvalds | 8f59342 | 2008-05-19 19:53:01 -0700 | [diff] [blame] | 601 | struct super_block *sb = old_dir->i_sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | unsigned char old_msdos_name[MSDOS_NAME], new_msdos_name[MSDOS_NAME]; |
| 603 | int err, is_hid; |
| 604 | |
Marco Stornelli | e40b34c | 2012-10-06 12:40:03 +0200 | [diff] [blame] | 605 | mutex_lock(&MSDOS_SB(sb)->s_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | |
| 607 | err = msdos_format_name(old_dentry->d_name.name, |
| 608 | old_dentry->d_name.len, old_msdos_name, |
| 609 | &MSDOS_SB(old_dir->i_sb)->options); |
| 610 | if (err) |
| 611 | goto out; |
| 612 | err = msdos_format_name(new_dentry->d_name.name, |
| 613 | new_dentry->d_name.len, new_msdos_name, |
| 614 | &MSDOS_SB(new_dir->i_sb)->options); |
| 615 | if (err) |
| 616 | goto out; |
| 617 | |
| 618 | is_hid = |
| 619 | (new_dentry->d_name.name[0] == '.') && (new_msdos_name[0] != '.'); |
| 620 | |
| 621 | err = do_msdos_rename(old_dir, old_msdos_name, old_dentry, |
| 622 | new_dir, new_msdos_name, new_dentry, is_hid); |
| 623 | out: |
Marco Stornelli | e40b34c | 2012-10-06 12:40:03 +0200 | [diff] [blame] | 624 | mutex_unlock(&MSDOS_SB(sb)->s_lock); |
Chris Mason | ae78bf9 | 2006-09-29 02:00:03 -0700 | [diff] [blame] | 625 | if (!err) |
Linus Torvalds | 8f59342 | 2008-05-19 19:53:01 -0700 | [diff] [blame] | 626 | err = fat_flush_inodes(sb, old_dir, new_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | return err; |
| 628 | } |
| 629 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 630 | static const struct inode_operations msdos_dir_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | .create = msdos_create, |
| 632 | .lookup = msdos_lookup, |
| 633 | .unlink = msdos_unlink, |
| 634 | .mkdir = msdos_mkdir, |
| 635 | .rmdir = msdos_rmdir, |
| 636 | .rename = msdos_rename, |
OGAWA Hirofumi | 1278fdd | 2008-04-28 02:16:25 -0700 | [diff] [blame] | 637 | .setattr = fat_setattr, |
OGAWA Hirofumi | da63fc7 | 2006-11-16 01:19:28 -0800 | [diff] [blame] | 638 | .getattr = fat_getattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | }; |
| 640 | |
Al Viro | 3d23985 | 2010-12-18 10:44:00 -0500 | [diff] [blame] | 641 | static void setup(struct super_block *sb) |
| 642 | { |
OGAWA Hirofumi | 384f5c9 | 2011-04-12 21:08:37 +0900 | [diff] [blame] | 643 | MSDOS_SB(sb)->dir_ops = &msdos_dir_inode_operations; |
Al Viro | 3d23985 | 2010-12-18 10:44:00 -0500 | [diff] [blame] | 644 | sb->s_d_op = &msdos_dentry_operations; |
| 645 | sb->s_flags |= MS_NOATIME; |
| 646 | } |
| 647 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | static int msdos_fill_super(struct super_block *sb, void *data, int silent) |
| 649 | { |
OGAWA Hirofumi | 384f5c9 | 2011-04-12 21:08:37 +0900 | [diff] [blame] | 650 | return fat_fill_super(sb, data, silent, 0, setup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | } |
| 652 | |
Al Viro | 152a083 | 2010-07-25 00:46:55 +0400 | [diff] [blame] | 653 | static struct dentry *msdos_mount(struct file_system_type *fs_type, |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 654 | int flags, const char *dev_name, |
Al Viro | 152a083 | 2010-07-25 00:46:55 +0400 | [diff] [blame] | 655 | void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | { |
Al Viro | 152a083 | 2010-07-25 00:46:55 +0400 | [diff] [blame] | 657 | return mount_bdev(fs_type, flags, dev_name, data, msdos_fill_super); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | static struct file_system_type msdos_fs_type = { |
| 661 | .owner = THIS_MODULE, |
| 662 | .name = "msdos", |
Al Viro | 152a083 | 2010-07-25 00:46:55 +0400 | [diff] [blame] | 663 | .mount = msdos_mount, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | .kill_sb = kill_block_super, |
| 665 | .fs_flags = FS_REQUIRES_DEV, |
| 666 | }; |
Eric W. Biederman | 7f78e03 | 2013-03-02 19:39:14 -0800 | [diff] [blame] | 667 | MODULE_ALIAS_FS("msdos"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | |
| 669 | static int __init init_msdos_fs(void) |
| 670 | { |
| 671 | return register_filesystem(&msdos_fs_type); |
| 672 | } |
| 673 | |
| 674 | static void __exit exit_msdos_fs(void) |
| 675 | { |
| 676 | unregister_filesystem(&msdos_fs_type); |
| 677 | } |
| 678 | |
| 679 | MODULE_LICENSE("GPL"); |
| 680 | MODULE_AUTHOR("Werner Almesberger"); |
| 681 | MODULE_DESCRIPTION("MS-DOS filesystem support"); |
| 682 | |
| 683 | module_init(init_msdos_fs) |
| 684 | module_exit(exit_msdos_fs) |