This source file includes following definitions.
- msdos_format_name
- msdos_find
- msdos_lookup
- msdos_create_entry
- msdos_create
- dump_fat
- msdos_mkdir
- msdos_empty
- msdos_rmdir
- msdos_unlink
- rename_same_dir
- rename_diff_dir
- msdos_rename
1
2
3
4
5
6
7 #include <asm/segment.h>
8
9 #include <linux/sched.h>
10 #include <linux/msdos_fs.h>
11 #include <linux/kernel.h>
12 #include <linux/errno.h>
13 #include <linux/string.h>
14 #include <linux/stat.h>
15
16
17
18 static char *reserved_names[] = {
19 "CON ","PRN ","NUL ","AUX ",
20 "LPT1 ","LPT2 ","LPT3 ","LPT4 ",
21 "COM1 ","COM2 ","COM3 ","COM4 ",
22 NULL };
23
24
25
26
27 static char bad_chars[] = "*?<>|\"";
28 static char bad_if_strict[] = "+=,; ";
29
30
31
32
33 static int msdos_format_name(char conv,const char *name,int len,char *res,
34 int dot_dirs)
35 {
36 char *walk,**reserved;
37 char c;
38 int space;
39
40 if (IS_FREE(name)) return -EINVAL;
41 if (name[0] == '.' && (len == 1 || (len == 2 && name[1] == '.'))) {
42 if (!dot_dirs) return -EEXIST;
43 memset(res+1,' ',10);
44 while (len--) *res++ = '.';
45 return 0;
46 }
47 space = 1;
48 c = 0;
49 for (walk = res; len && walk-res < 8; walk++) {
50 c = *name++;
51 len--;
52 if (conv != 'r' && strchr(bad_chars,c)) return -EINVAL;
53 if (conv == 's' && strchr(bad_if_strict,c)) return -EINVAL;
54 if (c >= 'A' && c <= 'Z' && conv == 's') return -EINVAL;
55 if (c < ' ' || c == ':' || c == '\\') return -EINVAL;
56 if (c == '.') break;
57 space = c == ' ';
58 *walk = c >= 'a' && c <= 'z' ? c-32 : c;
59 }
60 if (space) return -EINVAL;
61 if (conv == 's' && len && c != '.') {
62 c = *name++;
63 len--;
64 if (c != '.') return -EINVAL;
65 }
66 while (c != '.' && len--) c = *name++;
67 if (c == '.') {
68 while (walk-res < 8) *walk++ = ' ';
69 while (len > 0 && walk-res < MSDOS_NAME) {
70 c = *name++;
71 len--;
72 if (conv != 'r' && strchr(bad_chars,c)) return -EINVAL;
73 if (conv == 's' && strchr(bad_if_strict,c))
74 return -EINVAL;
75 if (c < ' ' || c == ':' || c == '\\' || c == '.')
76 return -EINVAL;
77 if (c >= 'A' && c <= 'Z' && conv == 's') return -EINVAL;
78 space = c == ' ';
79 *walk++ = c >= 'a' && c <= 'z' ? c-32 : c;
80 }
81 if (space) return -EINVAL;
82 if (conv == 's' && len) return -EINVAL;
83 }
84 while (walk-res < MSDOS_NAME) *walk++ = ' ';
85 for (reserved = reserved_names; *reserved; reserved++)
86 if (!strncmp(res,*reserved,8)) return -EINVAL;
87 return 0;
88 }
89
90
91
92
93 static int msdos_find(struct inode *dir,const char *name,int len,
94 struct buffer_head **bh,struct msdos_dir_entry **de,int *ino)
95 {
96 char msdos_name[MSDOS_NAME];
97 int res;
98
99 if ((res = msdos_format_name(MSDOS_SB(dir->i_sb)->name_check,name,len,
100 msdos_name,1)) < 0) return res;
101 return msdos_scan(dir,msdos_name,bh,de,ino);
102 }
103
104
105 int msdos_lookup(struct inode *dir,const char *name,int len,
106 struct inode **result)
107 {
108 int ino,res;
109 struct msdos_dir_entry *de;
110 struct buffer_head *bh;
111 struct inode *next;
112
113 *result = NULL;
114 if (!dir) return -ENOENT;
115 if (!S_ISDIR(dir->i_mode)) {
116 iput(dir);
117 return -ENOENT;
118 }
119 if (len == 1 && name[0] == '.') {
120 *result = dir;
121 return 0;
122 }
123 if (len == 2 && name[0] == '.' && name[1] == '.') {
124 ino = msdos_parent_ino(dir,0);
125 iput(dir);
126 if (ino < 0) return ino;
127 if (!(*result = iget(dir->i_sb,ino))) return -EACCES;
128 return 0;
129 }
130 if ((res = msdos_find(dir,name,len,&bh,&de,&ino)) < 0) {
131 iput(dir);
132 return res;
133 }
134 if (bh) brelse(bh);
135
136 if (!(*result = iget(dir->i_sb,ino))) {
137 iput(dir);
138 return -EACCES;
139 }
140 if (MSDOS_I(*result)->i_busy) {
141 iput(*result);
142 iput(dir);
143 return -ENOENT;
144 }
145 while (MSDOS_I(*result)->i_old) {
146 next = MSDOS_I(*result)->i_old;
147 iput(*result);
148 if (!(*result = iget(next->i_sb,next->i_ino))) {
149 fs_panic(dir->i_sb,"msdos_lookup: Can't happen");
150 iput(dir);
151 return -ENOENT;
152 }
153 }
154 iput(dir);
155 return 0;
156 }
157
158
159
160
161 static int msdos_create_entry(struct inode *dir,char *name,int is_dir,
162 struct inode **result)
163 {
164 struct buffer_head *bh;
165 struct msdos_dir_entry *de;
166 int res,ino;
167
168 if ((res = msdos_scan(dir,NULL,&bh,&de,&ino)) < 0) {
169 if (res != -ENOENT) return res;
170 if (dir->i_ino == MSDOS_ROOT_INO) return -ENOSPC;
171 if ((res = msdos_add_cluster(dir)) < 0) return res;
172 if ((res = msdos_scan(dir,NULL,&bh,&de,&ino)) < 0) return res;
173 }
174 memcpy(de->name,name,MSDOS_NAME);
175 de->attr = is_dir ? ATTR_DIR : ATTR_ARCH;
176 de->start = 0;
177 date_unix2dos(CURRENT_TIME,&de->time,&de->date);
178 de->size = 0;
179 bh->b_dirt = 1;
180 if ((*result = iget(dir->i_sb,ino)) != NULL)
181 msdos_read_inode(*result);
182 brelse(bh);
183 if (!*result) return -EIO;
184 (*result)->i_mtime = (*result)->i_atime = (*result)->i_ctime =
185 CURRENT_TIME;
186 (*result)->i_dirt = 1;
187 return 0;
188 }
189
190
191 int msdos_create(struct inode *dir,const char *name,int len,int mode,
192 struct inode **result)
193 {
194 struct buffer_head *bh;
195 struct msdos_dir_entry *de;
196 char msdos_name[MSDOS_NAME];
197 int ino,res;
198
199 if (!dir) return -ENOENT;
200 if ((res = msdos_format_name(MSDOS_SB(dir->i_sb)->name_check,name,len,
201 msdos_name,0)) < 0) {
202 iput(dir);
203 return res;
204 }
205 lock_creation();
206 if (msdos_scan(dir,msdos_name,&bh,&de,&ino) >= 0) {
207 unlock_creation();
208 brelse(bh);
209 iput(dir);
210 return -EEXIST;
211 }
212 res = msdos_create_entry(dir,msdos_name,S_ISDIR(mode),result);
213 unlock_creation();
214 iput(dir);
215 return res;
216 }
217
218
219 #ifdef DEBUG
220
221 static void dump_fat(struct super_block *sb,int start)
222 {
223 printk("[");
224 while (start) {
225 printk("%d ",start);
226 start = fat_access(sb,start,-1);
227 if (!start) {
228 printk("ERROR");
229 break;
230 }
231 if (start == -1) break;
232 }
233 printk("]\n");
234 }
235
236 #endif
237
238
239 int msdos_mkdir(struct inode *dir,const char *name,int len,int mode)
240 {
241 struct buffer_head *bh;
242 struct msdos_dir_entry *de;
243 struct inode *inode,*dot;
244 char msdos_name[MSDOS_NAME];
245 int ino,res;
246
247 if ((res = msdos_format_name(MSDOS_SB(dir->i_sb)->name_check,name,len,
248 msdos_name,0)) < 0) {
249 iput(dir);
250 return res;
251 }
252 lock_creation();
253 if (msdos_scan(dir,msdos_name,&bh,&de,&ino) >= 0) {
254 unlock_creation();
255 brelse(bh);
256 iput(dir);
257 return -EEXIST;
258 }
259 if ((res = msdos_create_entry(dir,msdos_name,1,&inode)) < 0) {
260 unlock_creation();
261 iput(dir);
262 return res;
263 }
264 dir->i_nlink++;
265 inode->i_nlink = 2;
266 MSDOS_I(inode)->i_busy = 1;
267 if ((res = msdos_add_cluster(inode)) < 0) goto mkdir_error;
268 if ((res = msdos_create_entry(inode,MSDOS_DOT,1,&dot)) < 0)
269 goto mkdir_error;
270 dot->i_size = inode->i_size;
271 MSDOS_I(dot)->i_start = MSDOS_I(inode)->i_start;
272 dot->i_nlink = inode->i_nlink;
273 dot->i_dirt = 1;
274 iput(dot);
275 if ((res = msdos_create_entry(inode,MSDOS_DOTDOT,1,&dot)) < 0)
276 goto mkdir_error;
277 unlock_creation();
278 dot->i_size = dir->i_size;
279 MSDOS_I(dot)->i_start = MSDOS_I(dir)->i_start;
280 dot->i_nlink = dir->i_nlink;
281 dot->i_dirt = 1;
282 MSDOS_I(inode)->i_busy = 0;
283 iput(dot);
284 iput(inode);
285 iput(dir);
286 return 0;
287 mkdir_error:
288 iput(inode);
289 if (msdos_rmdir(dir,name,len) < 0)
290 fs_panic(dir->i_sb,"rmdir in mkdir failed");
291 unlock_creation();
292 return res;
293 }
294
295
296 static int msdos_empty(struct inode *dir)
297 {
298 int pos;
299 struct buffer_head *bh;
300 struct msdos_dir_entry *de;
301
302 if (dir->i_count > 1)
303 return -EBUSY;
304 if (MSDOS_I(dir)->i_start) {
305 pos = 0;
306 bh = NULL;
307 while (msdos_get_entry(dir,&pos,&bh,&de) > -1)
308 if (!IS_FREE(de->name) && strncmp(de->name,MSDOS_DOT,
309 MSDOS_NAME) && strncmp(de->name,MSDOS_DOTDOT,
310 MSDOS_NAME)) {
311 brelse(bh);
312 return -ENOTEMPTY;
313 }
314 if (bh)
315 brelse(bh);
316 }
317 return 0;
318 }
319
320
321 int msdos_rmdir(struct inode *dir,const char *name,int len)
322 {
323 int res,ino;
324 struct buffer_head *bh;
325 struct msdos_dir_entry *de;
326 struct inode *inode;
327
328 bh = NULL;
329 inode = NULL;
330 res = -EPERM;
331 if (name[0] == '.' && (len == 1 || (len == 2 && name[1] == '.')))
332 goto rmdir_done;
333 if ((res = msdos_find(dir,name,len,&bh,&de,&ino)) < 0) goto rmdir_done;
334 res = -ENOENT;
335 if (!(inode = iget(dir->i_sb,ino))) goto rmdir_done;
336 res = -ENOTDIR;
337 if (!S_ISDIR(inode->i_mode)) goto rmdir_done;
338 res = -EBUSY;
339 if (dir->i_dev != inode->i_dev || dir == inode) goto rmdir_done;
340 res = msdos_empty(inode);
341 if (res)
342 goto rmdir_done;
343 inode->i_nlink = 0;
344 dir->i_mtime = CURRENT_TIME;
345 dir->i_nlink--;
346 inode->i_dirt = dir->i_dirt = 1;
347 de->name[0] = DELETED_FLAG;
348 bh->b_dirt = 1;
349 res = 0;
350 rmdir_done:
351 brelse(bh);
352 iput(dir);
353 iput(inode);
354 return res;
355 }
356
357
358 int msdos_unlink(struct inode *dir,const char *name,int len)
359 {
360 int res,ino;
361 struct buffer_head *bh;
362 struct msdos_dir_entry *de;
363 struct inode *inode;
364
365 bh = NULL;
366 inode = NULL;
367 if ((res = msdos_find(dir,name,len,&bh,&de,&ino)) < 0)
368 goto unlink_done;
369 if (!(inode = iget(dir->i_sb,ino))) {
370 res = -ENOENT;
371 goto unlink_done;
372 }
373 if (!S_ISREG(inode->i_mode)) {
374 res = -EPERM;
375 goto unlink_done;
376 }
377 inode->i_nlink = 0;
378 MSDOS_I(inode)->i_busy = 1;
379 inode->i_dirt = 1;
380 de->name[0] = DELETED_FLAG;
381 bh->b_dirt = 1;
382 unlink_done:
383 brelse(bh);
384 iput(inode);
385 iput(dir);
386 return res;
387 }
388
389
390 static int rename_same_dir(struct inode *old_dir,char *old_name,
391 struct inode *new_dir,char *new_name,struct buffer_head *old_bh,
392 struct msdos_dir_entry *old_de,int old_ino)
393 {
394 struct buffer_head *new_bh;
395 struct msdos_dir_entry *new_de;
396 struct inode *new_inode,*old_inode;
397 int new_ino,exists,error;
398
399 if (!strncmp(old_name,new_name,MSDOS_NAME)) return 0;
400 exists = msdos_scan(new_dir,new_name,&new_bh,&new_de,&new_ino) >= 0;
401 if (*(unsigned char *) old_de->name == DELETED_FLAG) {
402 if (exists) brelse(new_bh);
403 return -ENOENT;
404 }
405 if (exists) {
406 if (!(new_inode = iget(new_dir->i_sb,new_ino))) {
407 brelse(new_bh);
408 return -EIO;
409 }
410 error = S_ISDIR(new_inode->i_mode) ? (old_de->attr & ATTR_DIR) ?
411 msdos_empty(new_inode) : -EPERM : (old_de->attr & ATTR_DIR)
412 ? -EPERM : 0;
413 if (error) {
414 iput(new_inode);
415 brelse(new_bh);
416 return error;
417 }
418 if (S_ISDIR(new_inode->i_mode)) {
419 new_dir->i_nlink--;
420 new_dir->i_dirt = 1;
421 }
422 new_inode->i_nlink = 0;
423 MSDOS_I(new_inode)->i_busy = 1;
424 new_inode->i_dirt = 1;
425 new_de->name[0] = DELETED_FLAG;
426 new_bh->b_dirt = 1;
427 iput(new_inode);
428 brelse(new_bh);
429 }
430 memcpy(old_de->name,new_name,MSDOS_NAME);
431 old_bh->b_dirt = 1;
432 if (MSDOS_SB(old_dir->i_sb)->conversion == 'a')
433 if ((old_inode = iget(old_dir->i_sb,old_ino)) != NULL) {
434 msdos_read_inode(old_inode);
435 iput(old_inode);
436 }
437 return 0;
438 }
439
440
441 static int rename_diff_dir(struct inode *old_dir,char *old_name,
442 struct inode *new_dir,char *new_name,struct buffer_head *old_bh,
443 struct msdos_dir_entry *old_de,int old_ino)
444 {
445 struct buffer_head *new_bh,*free_bh,*dotdot_bh;
446 struct msdos_dir_entry *new_de,*free_de,*dotdot_de;
447 struct inode *old_inode,*new_inode,*free_inode,*dotdot_inode,*walk;
448 int new_ino,free_ino,dotdot_ino;
449 int error,exists,ino;
450
451 if (old_dir->i_dev != new_dir->i_dev) return -EINVAL;
452 if (old_ino == new_dir->i_ino) return -EINVAL;
453 if (!(walk = iget(new_dir->i_sb,new_dir->i_ino))) return -EIO;
454 while (walk->i_ino != MSDOS_ROOT_INO) {
455 ino = msdos_parent_ino(walk,1);
456 iput(walk);
457 if (ino < 0) return ino;
458 if (ino == old_ino) return -EINVAL;
459 if (!(walk = iget(new_dir->i_sb,ino))) return -EIO;
460 }
461 iput(walk);
462 while ((error = msdos_scan(new_dir,NULL,&free_bh,&free_de,&free_ino)) <
463 0) {
464 if (error != -ENOENT) return error;
465 error = msdos_add_cluster(new_dir);
466 if (error) return error;
467 }
468 exists = msdos_scan(new_dir,new_name,&new_bh,&new_de,&new_ino) >= 0;
469 if (!(old_inode = iget(old_dir->i_sb,old_ino))) {
470 brelse(free_bh);
471 if (exists) brelse(new_bh);
472 return -EIO;
473 }
474 if (*(unsigned char *) old_de->name == DELETED_FLAG) {
475 iput(old_inode);
476 brelse(free_bh);
477 if (exists) brelse(new_bh);
478 return -ENOENT;
479 }
480 new_inode = NULL;
481 if (exists) {
482 if (!(new_inode = iget(new_dir->i_sb,new_ino))) {
483 iput(old_inode);
484 brelse(new_bh);
485 return -EIO;
486 }
487 error = S_ISDIR(new_inode->i_mode) ? (old_de->attr & ATTR_DIR) ?
488 msdos_empty(new_inode) : -EPERM : (old_de->attr & ATTR_DIR)
489 ? -EPERM : 0;
490 if (error) {
491 iput(new_inode);
492 iput(old_inode);
493 brelse(new_bh);
494 return error;
495 }
496 new_inode->i_nlink = 0;
497 MSDOS_I(new_inode)->i_busy = 1;
498 new_inode->i_dirt = 1;
499 new_de->name[0] = DELETED_FLAG;
500 new_bh->b_dirt = 1;
501 }
502 memcpy(free_de,old_de,sizeof(struct msdos_dir_entry));
503 memcpy(free_de->name,new_name,MSDOS_NAME);
504 if (!(free_inode = iget(new_dir->i_sb,free_ino))) {
505 free_de->name[0] = DELETED_FLAG;
506
507 brelse(free_bh);
508 if (exists) {
509 iput(new_inode);
510 brelse(new_bh);
511 }
512 return -EIO;
513 }
514 if (exists && S_ISDIR(new_inode->i_mode)) {
515 new_dir->i_nlink--;
516 new_dir->i_dirt = 1;
517 }
518 msdos_read_inode(free_inode);
519 MSDOS_I(old_inode)->i_busy = 1;
520 cache_inval_inode(old_inode);
521 old_inode->i_dirt = 1;
522 old_de->name[0] = DELETED_FLAG;
523 old_bh->b_dirt = 1;
524 free_bh->b_dirt = 1;
525 if (!exists) iput(free_inode);
526 else {
527 MSDOS_I(new_inode)->i_depend = free_inode;
528 MSDOS_I(free_inode)->i_old = new_inode;
529
530 iput(new_inode);
531 brelse(new_bh);
532 }
533 if (S_ISDIR(old_inode->i_mode)) {
534 if ((error = msdos_scan(old_inode,MSDOS_DOTDOT,&dotdot_bh,
535 &dotdot_de,&dotdot_ino)) < 0) goto rename_done;
536 if (!(dotdot_inode = iget(old_inode->i_sb,dotdot_ino))) {
537 brelse(dotdot_bh);
538 error = -EIO;
539 goto rename_done;
540 }
541 dotdot_de->start = MSDOS_I(dotdot_inode)->i_start =
542 MSDOS_I(new_dir)->i_start;
543 dotdot_inode->i_dirt = 1;
544 dotdot_bh->b_dirt = 1;
545 old_dir->i_nlink--;
546 new_dir->i_nlink++;
547
548 dotdot_inode->i_nlink = new_dir->i_nlink;
549 iput(dotdot_inode);
550 brelse(dotdot_bh);
551 }
552 error = 0;
553 rename_done:
554 brelse(free_bh);
555 iput(old_inode);
556 return error;
557 }
558
559
560 int msdos_rename(struct inode *old_dir,const char *old_name,int old_len,
561 struct inode *new_dir,const char *new_name,int new_len)
562 {
563 char old_msdos_name[MSDOS_NAME],new_msdos_name[MSDOS_NAME];
564 struct buffer_head *old_bh;
565 struct msdos_dir_entry *old_de;
566 int old_ino,error;
567
568 if ((error = msdos_format_name(MSDOS_SB(old_dir->i_sb)->name_check,
569 old_name,old_len,old_msdos_name,1)) < 0) goto rename_done;
570 if ((error = msdos_format_name(MSDOS_SB(new_dir->i_sb)->name_check,
571 new_name,new_len,new_msdos_name,0)) < 0) goto rename_done;
572 if ((error = msdos_scan(old_dir,old_msdos_name,&old_bh,&old_de,
573 &old_ino)) < 0) goto rename_done;
574 lock_creation();
575 if (old_dir == new_dir)
576 error = rename_same_dir(old_dir,old_msdos_name,new_dir,
577 new_msdos_name,old_bh,old_de,old_ino);
578 else error = rename_diff_dir(old_dir,old_msdos_name,new_dir,
579 new_msdos_name,old_bh,old_de,old_ino);
580 unlock_creation();
581 brelse(old_bh);
582 rename_done:
583 iput(old_dir);
584 iput(new_dir);
585 return error;
586 }