blob: 7d6a105d601b52d3d5b356e94c0c7ab87a818263 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Hirofumi9e975da2008-11-06 12:53:46 -080010#include "fat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
Linus Torvalds1da177e2005-04-16 15:20:36 -070012/* Characters that are undesirable in an MS-DOS file name */
13static unsigned char bad_chars[] = "*?<>|\"";
Rene Scharfe7557bc62008-07-25 01:46:45 -070014static unsigned char bad_if_strict[] = "+=,; ";
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16/***** Formats an MS-DOS file name. Rejects invalid names. */
17static 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 Torvalds1da177e2005-04-16 15:20:36 -070027 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 Scharfe7557bc62008-07-25 01:46:45 -070035 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 return -EINVAL;
37 }
38 /*
Rene Scharfe7557bc62008-07-25 01:46:45 -070039 * disallow names that _really_ start with a dot
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 */
Rene Scharfe7557bc62008-07-25 01:46:45 -070041 space = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 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 Scharfe7557bc62008-07-25 01:46:45 -070048 if (opts->name_check == 's' && strchr(bad_if_strict, c))
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 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 Scharfe7557bc62008-07-25 01:46:45 -070088 strchr(bad_if_strict, c))
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 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 Hirofumi094e3202006-03-31 02:30:53 -0800112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return 0;
114}
115
116/***** Locates a directory entry. Uses unformatted name. */
117static 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 Torvaldsda53be12013-05-21 15:22:44 -0700149static int msdos_hash(const struct dentry *dentry, struct qstr *qstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
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 Torvalds8387ff22016-06-10 07:51:30 -0700157 qstr->hash = full_name_hash(dentry, msdos_name, MSDOS_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 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 Viro6fa67e72016-07-31 16:37:25 -0400165static int msdos_cmp(const struct dentry *dentry,
Nick Piggin621e1552011-01-07 17:49:27 +1100166 unsigned int len, const char *str, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Al Virod3fe1982016-07-29 18:23:59 -0400168 struct fat_mount_options *options = &MSDOS_SB(dentry->d_sb)->options;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 unsigned char a_msdos_name[MSDOS_NAME], b_msdos_name[MSDOS_NAME];
170 int error;
171
Nick Piggin621e1552011-01-07 17:49:27 +1100172 error = msdos_format_name(name->name, name->len, a_msdos_name, options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (error)
174 goto old_compare;
Nick Piggin621e1552011-01-07 17:49:27 +1100175 error = msdos_format_name(str, len, b_msdos_name, options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (error)
177 goto old_compare;
178 error = memcmp(a_msdos_name, b_msdos_name, MSDOS_NAME);
179out:
180 return error;
181
182old_compare:
183 error = 1;
Nick Piggin621e1552011-01-07 17:49:27 +1100184 if (name->len == len)
185 error = memcmp(name->name, str, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 goto out;
187}
188
Al Viroce6cdc42009-02-20 05:59:46 +0000189static const struct dentry_operations msdos_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 .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 */
199static struct dentry *msdos_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400200 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 struct super_block *sb = dir->i_sb;
203 struct fat_slot_info sinfo;
OGAWA Hirofumi45cfbe32008-11-06 12:53:53 -0800204 struct inode *inode;
205 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Marco Stornellie40b34c2012-10-06 12:40:03 +0200207 mutex_lock(&MSDOS_SB(sb)->s_lock);
OGAWA Hirofumi45cfbe32008-11-06 12:53:53 -0800208 err = msdos_find(dir, dentry->d_name.name, dentry->d_name.len, &sinfo);
Al Viroa9049372011-07-08 21:20:11 -0400209 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 Hirofumi45cfbe32008-11-06 12:53:53 -0800219 }
Marco Stornellie40b34c2012-10-06 12:40:03 +0200220 mutex_unlock(&MSDOS_SB(sb)->s_lock);
Al Viro3d239852010-12-18 10:44:00 -0500221 return d_splice_alias(inode, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
224/***** Creates a directory entry (name is already formatted). */
225static 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 Petersonb271e062008-07-25 01:46:47 -0700229 struct msdos_sb_info *sbi = MSDOS_SB(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 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 Hirofumi7decd1c2008-11-06 12:53:47 -0800239 fat_time_unix2fat(sbi, ts, &time, &date, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 de.cdate = de.adate = 0;
241 de.ctime = 0;
242 de.ctime_cs = 0;
243 de.time = time;
244 de.date = date;
Steven J. Magnania943ed72012-07-30 14:42:13 -0700245 fat_set_start(&de, cluster);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 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 Viro4acdaf22011-07-26 01:42:34 -0400262static int msdos_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -0400263 bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 struct super_block *sb = dir->i_sb;
Chris Masonae78bf92006-09-29 02:00:03 -0700266 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 struct fat_slot_info sinfo;
268 struct timespec ts;
269 unsigned char msdos_name[MSDOS_NAME];
270 int err, is_hid;
271
Marco Stornellie40b34c2012-10-06 12:40:03 +0200272 mutex_lock(&MSDOS_SB(sb)->s_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
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
Deepa Dinamani02027d42016-09-14 07:48:05 -0700286 ts = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 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);
300out:
Marco Stornellie40b34c2012-10-06 12:40:03 +0200301 mutex_unlock(&MSDOS_SB(sb)->s_lock);
Chris Masonae78bf92006-09-29 02:00:03 -0700302 if (!err)
303 err = fat_flush_inodes(sb, dir, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return err;
305}
306
307/***** Remove a directory */
308static int msdos_rmdir(struct inode *dir, struct dentry *dentry)
309{
Linus Torvalds8f593422008-05-19 19:53:01 -0700310 struct super_block *sb = dir->i_sb;
David Howells2b0143b2015-03-17 22:25:59 +0000311 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 struct fat_slot_info sinfo;
313 int err;
314
Marco Stornellie40b34c2012-10-06 12:40:03 +0200315 mutex_lock(&MSDOS_SB(sb)->s_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 /*
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 Hansen9a53c3a2006-09-30 23:29:03 -0700330 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Dave Hansence71ec32006-09-30 23:29:06 -0700332 clear_nlink(inode);
Deepa Dinamani02027d42016-09-14 07:48:05 -0700333 inode->i_ctime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 fat_detach(inode);
335out:
Marco Stornellie40b34c2012-10-06 12:40:03 +0200336 mutex_unlock(&MSDOS_SB(sb)->s_lock);
Chris Masonae78bf92006-09-29 02:00:03 -0700337 if (!err)
Linus Torvalds8f593422008-05-19 19:53:01 -0700338 err = fat_flush_inodes(sb, dir, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 return err;
341}
342
343/***** Make a directory */
Al Viro18bb1db2011-07-26 01:41:39 -0400344static int msdos_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
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 Stornellie40b34c2012-10-06 12:40:03 +0200353 mutex_lock(&MSDOS_SB(sb)->s_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
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
Deepa Dinamani02027d42016-09-14 07:48:05 -0700367 ts = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 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 Hansend8c76e62006-09-30 23:29:04 -0700376 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
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 Szeredibfe86842011-10-28 14:13:29 +0200385 set_nlink(inode, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 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 Stornellie40b34c2012-10-06 12:40:03 +0200391 mutex_unlock(&MSDOS_SB(sb)->s_lock);
Chris Masonae78bf92006-09-29 02:00:03 -0700392 fat_flush_inodes(sb, dir, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return 0;
394
395out_free:
396 fat_free_clusters(dir, cluster);
397out:
Marco Stornellie40b34c2012-10-06 12:40:03 +0200398 mutex_unlock(&MSDOS_SB(sb)->s_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return err;
400}
401
402/***** Unlink a file */
403static int msdos_unlink(struct inode *dir, struct dentry *dentry)
404{
David Howells2b0143b2015-03-17 22:25:59 +0000405 struct inode *inode = d_inode(dentry);
Cruz Julian Bishop85cb9bf2012-10-04 17:14:47 -0700406 struct super_block *sb = inode->i_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 struct fat_slot_info sinfo;
408 int err;
409
Marco Stornellie40b34c2012-10-06 12:40:03 +0200410 mutex_lock(&MSDOS_SB(sb)->s_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 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 Hansence71ec32006-09-30 23:29:06 -0700418 clear_nlink(inode);
Deepa Dinamani02027d42016-09-14 07:48:05 -0700419 inode->i_ctime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 fat_detach(inode);
421out:
Marco Stornellie40b34c2012-10-06 12:40:03 +0200422 mutex_unlock(&MSDOS_SB(sb)->s_lock);
Chris Masonae78bf92006-09-29 02:00:03 -0700423 if (!err)
Linus Torvalds8f593422008-05-19 19:53:01 -0700424 err = fat_flush_inodes(sb, dir, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 return err;
427}
428
429static 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 Torvalds1da177e2005-04-16 15:20:36 -0700436 struct inode *old_inode, *new_inode;
437 struct fat_slot_info old_sinfo, sinfo;
438 struct timespec ts;
Steven J. Magnani7669e8f2012-10-04 17:14:45 -0700439 loff_t new_i_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 int err, old_attrs, is_dir, update_dotdot, corrupt = 0;
441
442 old_sinfo.bh = sinfo.bh = dotdot_bh = NULL;
David Howells2b0143b2015-03-17 22:25:59 +0000443 old_inode = d_inode(old_dentry);
444 new_inode = d_inode(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
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. Magnani7669e8f2012-10-04 17:14:45 -0700455 if (fat_get_dotdot_entry(old_inode, &dotdot_bh, &dotdot_de)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 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++;
Deepa Dinamani02027d42016-09-14 07:48:05 -0700484 old_dir->i_ctime = old_dir->i_mtime = current_time(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 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
Deepa Dinamani02027d42016-09-14 07:48:05 -0700493 ts = current_time(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (new_inode) {
495 if (err)
496 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (is_dir) {
498 err = fat_dir_empty(new_inode);
499 if (err)
500 goto out;
501 }
OGAWA Hirofumi9131dd42005-10-30 15:03:50 -0800502 new_i_pos = MSDOS_I(new_inode)->i_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 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 Hirofumi9131dd42005-10-30 15:03:50 -0800509 new_i_pos = sinfo.i_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
511 new_dir->i_version++;
512
513 fat_detach(old_inode);
OGAWA Hirofumi9131dd42005-10-30 15:03:50 -0800514 fat_attach(old_inode, new_i_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 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. Magnania943ed72012-07-30 14:42:13 -0700527 fat_set_start(dotdot_de, MSDOS_I(new_dir)->i_logstart);
Al Virob5224122009-06-07 13:44:36 -0400528 mark_buffer_dirty_inode(dotdot_bh, old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (IS_DIRSYNC(new_dir)) {
530 err = sync_dirty_buffer(dotdot_bh);
531 if (err)
532 goto error_dotdot;
533 }
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700534 drop_nlink(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (!new_inode)
Dave Hansend8c76e62006-09-30 23:29:04 -0700536 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
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 Hansen9a53c3a2006-09-30 23:29:03 -0700551 drop_nlink(new_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (is_dir)
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700553 drop_nlink(new_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 new_inode->i_ctime = ts;
555 }
556out:
557 brelse(sinfo.bh);
558 brelse(dotdot_bh);
559 brelse(old_sinfo.bh);
560 return err;
561
562error_dotdot:
563 /* data cluster is shared, serious corruption */
564 corrupt = 1;
565
566 if (update_dotdot) {
Steven J. Magnania943ed72012-07-30 14:42:13 -0700567 fat_set_start(dotdot_de, MSDOS_I(old_dir)->i_logstart);
Al Virob5224122009-06-07 13:44:36 -0400568 mark_buffer_dirty_inode(dotdot_bh, old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 corrupt |= sync_dirty_buffer(dotdot_bh);
570 }
571error_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 Hirofumi9131dd42005-10-30 15:03:50 -0800576 fat_attach(new_inode, new_i_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 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 Karpov85c78592009-06-04 02:34:22 +0900590 fat_fs_error(new_dir->i_sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 "%s: Filesystem corrupted (i_pos %lld)",
Harvey Harrison8e24eea2008-04-30 00:55:09 -0700592 __func__, sinfo.i_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594 goto out;
595}
596
597/***** Rename, a wrapper for rename_same_dir & rename_diff_dir */
598static int msdos_rename(struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredif03b8ad2016-09-27 11:03:57 +0200599 struct inode *new_dir, struct dentry *new_dentry,
600 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Linus Torvalds8f593422008-05-19 19:53:01 -0700602 struct super_block *sb = old_dir->i_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 unsigned char old_msdos_name[MSDOS_NAME], new_msdos_name[MSDOS_NAME];
604 int err, is_hid;
605
Miklos Szeredif03b8ad2016-09-27 11:03:57 +0200606 if (flags & ~RENAME_NOREPLACE)
607 return -EINVAL;
608
Marco Stornellie40b34c2012-10-06 12:40:03 +0200609 mutex_lock(&MSDOS_SB(sb)->s_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 err = msdos_format_name(old_dentry->d_name.name,
612 old_dentry->d_name.len, old_msdos_name,
613 &MSDOS_SB(old_dir->i_sb)->options);
614 if (err)
615 goto out;
616 err = msdos_format_name(new_dentry->d_name.name,
617 new_dentry->d_name.len, new_msdos_name,
618 &MSDOS_SB(new_dir->i_sb)->options);
619 if (err)
620 goto out;
621
622 is_hid =
623 (new_dentry->d_name.name[0] == '.') && (new_msdos_name[0] != '.');
624
625 err = do_msdos_rename(old_dir, old_msdos_name, old_dentry,
626 new_dir, new_msdos_name, new_dentry, is_hid);
627out:
Marco Stornellie40b34c2012-10-06 12:40:03 +0200628 mutex_unlock(&MSDOS_SB(sb)->s_lock);
Chris Masonae78bf92006-09-29 02:00:03 -0700629 if (!err)
Linus Torvalds8f593422008-05-19 19:53:01 -0700630 err = fat_flush_inodes(sb, old_dir, new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return err;
632}
633
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800634static const struct inode_operations msdos_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 .create = msdos_create,
636 .lookup = msdos_lookup,
637 .unlink = msdos_unlink,
638 .mkdir = msdos_mkdir,
639 .rmdir = msdos_rmdir,
640 .rename = msdos_rename,
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700641 .setattr = fat_setattr,
OGAWA Hirofumida63fc72006-11-16 01:19:28 -0800642 .getattr = fat_getattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643};
644
Al Viro3d239852010-12-18 10:44:00 -0500645static void setup(struct super_block *sb)
646{
OGAWA Hirofumi384f5c92011-04-12 21:08:37 +0900647 MSDOS_SB(sb)->dir_ops = &msdos_dir_inode_operations;
Al Viro3d239852010-12-18 10:44:00 -0500648 sb->s_d_op = &msdos_dentry_operations;
649 sb->s_flags |= MS_NOATIME;
650}
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652static int msdos_fill_super(struct super_block *sb, void *data, int silent)
653{
OGAWA Hirofumi384f5c92011-04-12 21:08:37 +0900654 return fat_fill_super(sb, data, silent, 0, setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
Al Viro152a0832010-07-25 00:46:55 +0400657static struct dentry *msdos_mount(struct file_system_type *fs_type,
David Howells454e2392006-06-23 02:02:57 -0700658 int flags, const char *dev_name,
Al Viro152a0832010-07-25 00:46:55 +0400659 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Al Viro152a0832010-07-25 00:46:55 +0400661 return mount_bdev(fs_type, flags, dev_name, data, msdos_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
664static struct file_system_type msdos_fs_type = {
665 .owner = THIS_MODULE,
666 .name = "msdos",
Al Viro152a0832010-07-25 00:46:55 +0400667 .mount = msdos_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 .kill_sb = kill_block_super,
669 .fs_flags = FS_REQUIRES_DEV,
670};
Eric W. Biederman7f78e032013-03-02 19:39:14 -0800671MODULE_ALIAS_FS("msdos");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673static int __init init_msdos_fs(void)
674{
675 return register_filesystem(&msdos_fs_type);
676}
677
678static void __exit exit_msdos_fs(void)
679{
680 unregister_filesystem(&msdos_fs_type);
681}
682
683MODULE_LICENSE("GPL");
684MODULE_AUTHOR("Werner Almesberger");
685MODULE_DESCRIPTION("MS-DOS filesystem support");
686
687module_init(init_msdos_fs)
688module_exit(exit_msdos_fs)