1 /*
2 * linux/fs/fat/dir.c
3 *
4 * directory handling functions for fat-based filesystems
5 *
6 * Written 1992,1993 by Werner Almesberger
7 *
8 * Hidden files 1995 by Albert Cahalan <albert@ccs.neu.edu> <adc@coe.neu.edu>
9 *
10 * VFAT extensions by Gordon Chaffee <chaffee@plateau.cs.berkeley.edu>
11 * Merged with msdos fs by Henrik Storner <storner@osiris.ping.dk>
12 * Rewritten for constant inumbers. Plugged buffer overrun in readdir(). AV
13 * Short name translation 1999, 2001 by Wolfram Pienkoss <wp@bszh.de>
14 */
15
16 #include <linux/fs.h>
17 #include <linux/msdos_fs.h>
18 #include <linux/nls.h>
19 #include <linux/kernel.h>
20 #include <linux/errno.h>
21 #include <linux/stat.h>
22 #include <linux/string.h>
23 #include <linux/ioctl.h>
24 #include <linux/dirent.h>
25 #include <linux/mm.h>
26 #include <linux/ctype.h>
27
28 #include <asm/uaccess.h>
29
30 #define PRINTK(X)
31
32 struct file_operations fat_dir_operations = {
33 read: generic_read_dir,
34 readdir: fat_readdir,
35 ioctl: fat_dir_ioctl,
36 fsync: file_fsync,
37 };
38
39 /*
40 * Convert Unicode 16 to UTF8, translated Unicode, or ASCII.
41 * If uni_xlate is enabled and we can't get a 1:1 conversion, use a
42 * colon as an escape character since it is normally invalid on the vfat
43 * filesystem. The following four characters are the hexadecimal digits
44 * of Unicode value. This lets us do a full dump and restore of Unicode
45 * filenames. We could get into some trouble with long Unicode names,
46 * but ignore that right now.
47 * Ahem... Stack smashing in ring 0 isn't fun. Fixed.
48 */
49 static int
uni16_to_x8(unsigned char * ascii,wchar_t * uni,int uni_xlate,struct nls_table * nls)50 uni16_to_x8(unsigned char *ascii, wchar_t *uni, int uni_xlate,
51 struct nls_table *nls)
52 {
53 wchar_t *ip, ec;
54 unsigned char *op, nc;
55 int charlen;
56 int k;
57
58 ip = uni;
59 op = ascii;
60
61 while (*ip) {
62 ec = *ip++;
63 if ( (charlen = nls->uni2char(ec, op, NLS_MAX_CHARSET_SIZE)) > 0) {
64 op += charlen;
65 } else {
66 if (uni_xlate == 1) {
67 *op = ':';
68 for (k = 4; k > 0; k--) {
69 nc = ec & 0xF;
70 op[k] = nc > 9 ? nc + ('a' - 10)
71 : nc + '0';
72 ec >>= 4;
73 }
74 op += 5;
75 } else {
76 *op++ = '?';
77 }
78 }
79 /* We have some slack there, so it's OK */
80 if (op>ascii+256) {
81 op = ascii + 256;
82 break;
83 }
84 }
85 *op = 0;
86 return (op - ascii);
87 }
88
89 #if 0
90 static void dump_de(struct msdos_dir_entry *de)
91 {
92 int i;
93 unsigned char *p = (unsigned char *) de;
94 printk("[");
95
96 for (i = 0; i < 32; i++, p++) {
97 printk("%02x ", *p);
98 }
99 printk("]\n");
100 }
101 #endif
102
103 static inline unsigned char
fat_tolower(struct nls_table * t,unsigned char c)104 fat_tolower(struct nls_table *t, unsigned char c)
105 {
106 unsigned char nc = t->charset2lower[c];
107
108 return nc ? nc : c;
109 }
110
111 static inline int
fat_short2uni(struct nls_table * t,unsigned char * c,int clen,wchar_t * uni)112 fat_short2uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
113 {
114 int charlen;
115
116 charlen = t->char2uni(c, clen, uni);
117 if (charlen < 0) {
118 *uni = 0x003f; /* a question mark */
119 charlen = 1;
120 }
121 return charlen;
122 }
123
124 static inline int
fat_short2lower_uni(struct nls_table * t,unsigned char * c,int clen,wchar_t * uni)125 fat_short2lower_uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
126 {
127 int charlen;
128 wchar_t wc;
129
130 charlen = t->char2uni(c, clen, &wc);
131 if (charlen < 0) {
132 *uni = 0x003f; /* a question mark */
133 charlen = 1;
134 } else if (charlen <= 1) {
135 unsigned char nc = t->charset2lower[*c];
136
137 if (!nc)
138 nc = *c;
139
140 if ( (charlen = t->char2uni(&nc, 1, uni)) < 0) {
141 *uni = 0x003f; /* a question mark */
142 charlen = 1;
143 }
144 } else
145 *uni = wc;
146
147 return charlen;
148 }
149
150 static int
fat_strnicmp(struct nls_table * t,const unsigned char * s1,const unsigned char * s2,int len)151 fat_strnicmp(struct nls_table *t, const unsigned char *s1,
152 const unsigned char *s2, int len)
153 {
154 while(len--)
155 if (fat_tolower(t, *s1++) != fat_tolower(t, *s2++))
156 return 1;
157
158 return 0;
159 }
160
161 static inline int
fat_shortname2uni(struct nls_table * nls,char * buf,int buf_size,wchar_t * uni_buf,unsigned short opt,int lower)162 fat_shortname2uni(struct nls_table *nls, char *buf, int buf_size,
163 wchar_t *uni_buf, unsigned short opt, int lower)
164 {
165 int len = 0;
166
167 if (opt & VFAT_SFN_DISPLAY_LOWER)
168 len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
169 else if (opt & VFAT_SFN_DISPLAY_WIN95)
170 len = fat_short2uni(nls, buf, buf_size, uni_buf);
171 else if (opt & VFAT_SFN_DISPLAY_WINNT) {
172 if (lower)
173 len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
174 else
175 len = fat_short2uni(nls, buf, buf_size, uni_buf);
176 } else
177 len = fat_short2uni(nls, buf, buf_size, uni_buf);
178
179 return len;
180 }
181
182 /*
183 * Return values: negative -> error, 0 -> not found, positive -> found,
184 * value is the total amount of slots, including the shortname entry.
185 */
fat_search_long(struct inode * inode,const char * name,int name_len,int anycase,loff_t * spos,loff_t * lpos)186 int fat_search_long(struct inode *inode, const char *name, int name_len,
187 int anycase, loff_t *spos, loff_t *lpos)
188 {
189 struct super_block *sb = inode->i_sb;
190 struct buffer_head *bh = NULL;
191 struct msdos_dir_entry *de;
192 struct nls_table *nls_io = MSDOS_SB(sb)->nls_io;
193 struct nls_table *nls_disk = MSDOS_SB(sb)->nls_disk;
194 wchar_t bufuname[14];
195 unsigned char xlate_len, long_slots;
196 wchar_t *unicode = NULL;
197 char work[8], bufname[260]; /* 256 + 4 */
198 int uni_xlate = MSDOS_SB(sb)->options.unicode_xlate;
199 int utf8 = MSDOS_SB(sb)->options.utf8;
200 unsigned short opt_shortname = MSDOS_SB(sb)->options.shortname;
201 int chl, i, j, last_u, res = 0;
202 loff_t i_pos, cpos = 0;
203
204 while(1) {
205 if (fat_get_entry(inode,&cpos,&bh,&de,&i_pos) == -1)
206 goto EODir;
207 parse_record:
208 long_slots = 0;
209 if (de->name[0] == (__s8) DELETED_FLAG)
210 continue;
211 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
212 continue;
213 if (de->attr != ATTR_EXT && IS_FREE(de->name))
214 continue;
215 if (de->attr == ATTR_EXT) {
216 struct msdos_dir_slot *ds;
217 unsigned char id;
218 unsigned char slot;
219 unsigned char slots;
220 unsigned char sum;
221 unsigned char alias_checksum;
222
223 if (!unicode) {
224 unicode = (wchar_t *)
225 __get_free_page(GFP_KERNEL);
226 if (!unicode) {
227 fat_brelse(sb, bh);
228 return -ENOMEM;
229 }
230 }
231 parse_long:
232 slots = 0;
233 ds = (struct msdos_dir_slot *) de;
234 id = ds->id;
235 if (!(id & 0x40))
236 continue;
237 slots = id & ~0x40;
238 if (slots > 20 || !slots) /* ceil(256 * 2 / 26) */
239 continue;
240 long_slots = slots;
241 alias_checksum = ds->alias_checksum;
242
243 slot = slots;
244 while (1) {
245 int offset;
246
247 slot--;
248 offset = slot * 13;
249 fat16_towchar(unicode + offset, ds->name0_4, 5);
250 fat16_towchar(unicode + offset + 5, ds->name5_10, 6);
251 fat16_towchar(unicode + offset + 11, ds->name11_12, 2);
252
253 if (ds->id & 0x40) {
254 unicode[offset + 13] = 0;
255 }
256 if (fat_get_entry(inode,&cpos,&bh,&de,&i_pos)<0)
257 goto EODir;
258 if (slot == 0)
259 break;
260 ds = (struct msdos_dir_slot *) de;
261 if (ds->attr != ATTR_EXT)
262 goto parse_record;
263 if ((ds->id & ~0x40) != slot)
264 goto parse_long;
265 if (ds->alias_checksum != alias_checksum)
266 goto parse_long;
267 }
268 if (de->name[0] == (__s8) DELETED_FLAG)
269 continue;
270 if (de->attr == ATTR_EXT)
271 goto parse_long;
272 if (IS_FREE(de->name) || (de->attr & ATTR_VOLUME))
273 continue;
274 for (sum = 0, i = 0; i < 11; i++)
275 sum = (((sum&1)<<7)|((sum&0xfe)>>1)) + de->name[i];
276 if (sum != alias_checksum)
277 long_slots = 0;
278 }
279
280 for (i = 0; i < 8; i++) {
281 /* see namei.c, msdos_format_name */
282 if (de->name[i] == 0x05)
283 work[i] = 0xE5;
284 else
285 work[i] = de->name[i];
286 }
287 for (i = 0, j = 0, last_u = 0; i < 8;) {
288 if (!work[i]) break;
289 chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
290 &bufuname[j++], opt_shortname,
291 de->lcase & CASE_LOWER_BASE);
292 if (chl <= 1) {
293 if (work[i] != ' ')
294 last_u = j;
295 } else {
296 last_u = j;
297 }
298 i += chl;
299 }
300 j = last_u;
301 fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
302 for (i = 0; i < 3;) {
303 if (!de->ext[i]) break;
304 chl = fat_shortname2uni(nls_disk, &de->ext[i], 3 - i,
305 &bufuname[j++], opt_shortname,
306 de->lcase & CASE_LOWER_EXT);
307 if (chl <= 1) {
308 if (de->ext[i] != ' ')
309 last_u = j;
310 } else {
311 last_u = j;
312 }
313 i += chl;
314 }
315 if (!last_u)
316 continue;
317
318 bufuname[last_u] = 0x0000;
319 xlate_len = utf8
320 ?utf8_wcstombs(bufname, bufuname, sizeof(bufname))
321 :uni16_to_x8(bufname, bufuname, uni_xlate, nls_io);
322 if (xlate_len == name_len)
323 if ((!anycase && !memcmp(name, bufname, xlate_len)) ||
324 (anycase && !fat_strnicmp(nls_io, name, bufname,
325 xlate_len)))
326 goto Found;
327
328 if (long_slots) {
329 xlate_len = utf8
330 ?utf8_wcstombs(bufname, unicode, sizeof(bufname))
331 :uni16_to_x8(bufname, unicode, uni_xlate, nls_io);
332 if (xlate_len != name_len)
333 continue;
334 if ((!anycase && !memcmp(name, bufname, xlate_len)) ||
335 (anycase && !fat_strnicmp(nls_io, name, bufname,
336 xlate_len)))
337 goto Found;
338 }
339 }
340
341 Found:
342 res = long_slots + 1;
343 *spos = cpos - sizeof(struct msdos_dir_entry);
344 *lpos = cpos - res*sizeof(struct msdos_dir_entry);
345 EODir:
346 fat_brelse(sb, bh);
347 if (unicode) {
348 free_page((unsigned long) unicode);
349 }
350 return res;
351 }
352
fat_readdirx(struct inode * inode,struct file * filp,void * dirent,filldir_t filldir,int shortnames,int both)353 static int fat_readdirx(struct inode *inode, struct file *filp, void *dirent,
354 filldir_t filldir, int shortnames, int both)
355 {
356 struct super_block *sb = inode->i_sb;
357 struct buffer_head *bh;
358 struct msdos_dir_entry *de;
359 struct nls_table *nls_io = MSDOS_SB(sb)->nls_io;
360 struct nls_table *nls_disk = MSDOS_SB(sb)->nls_disk;
361 wchar_t bufuname[14];
362 unsigned char long_slots;
363 wchar_t *unicode = NULL;
364 char c, work[8], bufname[56], *ptname = bufname;
365 unsigned long lpos, dummy, *furrfu = &lpos;
366 int uni_xlate = MSDOS_SB(sb)->options.unicode_xlate;
367 int isvfat = MSDOS_SB(sb)->options.isvfat;
368 int utf8 = MSDOS_SB(sb)->options.utf8;
369 int nocase = MSDOS_SB(sb)->options.nocase;
370 unsigned short opt_shortname = MSDOS_SB(sb)->options.shortname;
371 unsigned long inum;
372 int chi, chl, i, i2, j, last, last_u, dotoffset = 0;
373 loff_t i_pos, cpos;
374
375 cpos = filp->f_pos;
376 /* Fake . and .. for the root directory. */
377 if (inode->i_ino == MSDOS_ROOT_INO) {
378 while (cpos < 2) {
379 if (filldir(dirent, "..", cpos+1, cpos, MSDOS_ROOT_INO, DT_DIR) < 0)
380 return 0;
381 cpos++;
382 filp->f_pos++;
383 }
384 if (cpos == 2) {
385 dummy = 2;
386 furrfu = &dummy;
387 cpos = 0;
388 }
389 }
390 if (cpos & (sizeof(struct msdos_dir_entry)-1))
391 return -ENOENT;
392
393 bh = NULL;
394 GetNew:
395 long_slots = 0;
396 if (fat_get_entry(inode,&cpos,&bh,&de,&i_pos) == -1)
397 goto EODir;
398 /* Check for long filename entry */
399 if (isvfat) {
400 if (de->name[0] == (__s8) DELETED_FLAG)
401 goto RecEnd;
402 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
403 goto RecEnd;
404 if (de->attr != ATTR_EXT && IS_FREE(de->name))
405 goto RecEnd;
406 } else {
407 if ((de->attr & ATTR_VOLUME) || IS_FREE(de->name))
408 goto RecEnd;
409 }
410
411 if (isvfat && de->attr == ATTR_EXT) {
412 struct msdos_dir_slot *ds;
413 unsigned char id;
414 unsigned char slot;
415 unsigned char slots;
416 unsigned char sum;
417 unsigned char alias_checksum;
418
419 if (!unicode) {
420 unicode = (wchar_t *)
421 __get_free_page(GFP_KERNEL);
422 if (!unicode) {
423 filp->f_pos = cpos;
424 fat_brelse(sb, bh);
425 return -ENOMEM;
426 }
427 }
428 ParseLong:
429 slots = 0;
430 ds = (struct msdos_dir_slot *) de;
431 id = ds->id;
432 if (!(id & 0x40))
433 goto RecEnd;
434 slots = id & ~0x40;
435 if (slots > 20 || !slots) /* ceil(256 * 2 / 26) */
436 goto RecEnd;
437 long_slots = slots;
438 alias_checksum = ds->alias_checksum;
439
440 slot = slots;
441 while (1) {
442 int offset;
443
444 slot--;
445 offset = slot * 13;
446 fat16_towchar(unicode + offset, ds->name0_4, 5);
447 fat16_towchar(unicode + offset + 5, ds->name5_10, 6);
448 fat16_towchar(unicode + offset + 11, ds->name11_12, 2);
449
450 if (ds->id & 0x40) {
451 unicode[offset + 13] = 0;
452 }
453 if (fat_get_entry(inode,&cpos,&bh,&de,&i_pos) == -1)
454 goto EODir;
455 if (slot == 0)
456 break;
457 ds = (struct msdos_dir_slot *) de;
458 if (ds->attr != ATTR_EXT)
459 goto RecEnd; /* XXX */
460 if ((ds->id & ~0x40) != slot)
461 goto ParseLong;
462 if (ds->alias_checksum != alias_checksum)
463 goto ParseLong;
464 }
465 if (de->name[0] == (__s8) DELETED_FLAG)
466 goto RecEnd;
467 if (de->attr == ATTR_EXT)
468 goto ParseLong;
469 if (IS_FREE(de->name) || (de->attr & ATTR_VOLUME))
470 goto RecEnd;
471 for (sum = 0, i = 0; i < 11; i++)
472 sum = (((sum&1)<<7)|((sum&0xfe)>>1)) + de->name[i];
473 if (sum != alias_checksum)
474 long_slots = 0;
475 }
476
477 if ((de->attr & ATTR_HIDDEN) && MSDOS_SB(sb)->options.dotsOK) {
478 *ptname++ = '.';
479 dotoffset = 1;
480 }
481
482 for (i = 0; i < 8; i++) {
483 /* see namei.c, msdos_format_name */
484 if (de->name[i] == 0x05)
485 work[i] = 0xE5;
486 else
487 work[i] = de->name[i];
488 }
489 for (i = 0, j = 0, last = 0, last_u = 0; i < 8;) {
490 if (!(c = work[i])) break;
491 chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
492 &bufuname[j++], opt_shortname,
493 de->lcase & CASE_LOWER_BASE);
494 if (chl <= 1) {
495 ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
496 if (c != ' ') {
497 last = i;
498 last_u = j;
499 }
500 } else {
501 last_u = j;
502 for (chi = 0; chi < chl && i < 8; chi++) {
503 ptname[i] = work[i];
504 i++; last = i;
505 }
506 }
507 }
508 i = last;
509 j = last_u;
510 fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
511 ptname[i++] = '.';
512 for (i2 = 0; i2 < 3;) {
513 if (!(c = de->ext[i2])) break;
514 chl = fat_shortname2uni(nls_disk, &de->ext[i2], 3 - i2,
515 &bufuname[j++], opt_shortname,
516 de->lcase & CASE_LOWER_EXT);
517 if (chl <= 1) {
518 i2++;
519 ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
520 if (c != ' ') {
521 last = i;
522 last_u = j;
523 }
524 } else {
525 last_u = j;
526 for (chi = 0; chi < chl && i2 < 3; chi++) {
527 ptname[i++] = de->ext[i2++];
528 last = i;
529 }
530 }
531 }
532 if (!last)
533 goto RecEnd;
534
535 i = last + dotoffset;
536 j = last_u;
537
538 lpos = cpos - (long_slots+1)*sizeof(struct msdos_dir_entry);
539 if (!memcmp(de->name,MSDOS_DOT,11))
540 inum = inode->i_ino;
541 else if (!memcmp(de->name,MSDOS_DOTDOT,11)) {
542 /* inum = fat_parent_ino(inode,0); */
543 inum = filp->f_dentry->d_parent->d_inode->i_ino;
544 } else {
545 struct inode *tmp = fat_iget(sb, i_pos);
546 if (tmp) {
547 inum = tmp->i_ino;
548 iput(tmp);
549 } else
550 inum = iunique(sb, MSDOS_ROOT_INO);
551 }
552
553 if (isvfat) {
554 bufuname[j] = 0x0000;
555 i = utf8 ? utf8_wcstombs(bufname, bufuname, sizeof(bufname))
556 : uni16_to_x8(bufname, bufuname, uni_xlate, nls_io);
557 }
558
559 if (!long_slots||shortnames) {
560 if (both)
561 bufname[i] = '\0';
562 if (filldir(dirent, bufname, i, *furrfu, inum,
563 (de->attr & ATTR_DIR) ? DT_DIR : DT_REG) < 0)
564 goto FillFailed;
565 } else {
566 char longname[275];
567 int long_len = utf8
568 ? utf8_wcstombs(longname, unicode, sizeof(longname))
569 : uni16_to_x8(longname, unicode, uni_xlate,
570 nls_io);
571 if (both) {
572 memcpy(&longname[long_len+1], bufname, i);
573 long_len += i;
574 }
575 if (filldir(dirent, longname, long_len, *furrfu, inum,
576 (de->attr & ATTR_DIR) ? DT_DIR : DT_REG) < 0)
577 goto FillFailed;
578 }
579
580 RecEnd:
581 furrfu = &lpos;
582 filp->f_pos = cpos;
583 goto GetNew;
584 EODir:
585 filp->f_pos = cpos;
586 FillFailed:
587 if (bh)
588 fat_brelse(sb, bh);
589 if (unicode) {
590 free_page((unsigned long) unicode);
591 }
592 return 0;
593 }
594
fat_readdir(struct file * filp,void * dirent,filldir_t filldir)595 int fat_readdir(struct file *filp, void *dirent, filldir_t filldir)
596 {
597 struct inode *inode = filp->f_dentry->d_inode;
598 return fat_readdirx(inode, filp, dirent, filldir, 0, 0);
599 }
600
vfat_ioctl_fill(void * buf,const char * name,int name_len,loff_t offset,ino_t ino,unsigned int d_type)601 static int vfat_ioctl_fill(
602 void * buf,
603 const char * name,
604 int name_len,
605 loff_t offset,
606 ino_t ino,
607 unsigned int d_type)
608 {
609 struct dirent *d1 = (struct dirent *)buf;
610 struct dirent *d2 = d1 + 1;
611 int len, slen;
612 int dotdir;
613
614 get_user(len, &d1->d_reclen);
615 if (len != 0) {
616 return -1;
617 }
618
619 if ((name_len == 1 && name[0] == '.') ||
620 (name_len == 2 && name[0] == '.' && name[1] == '.')) {
621 dotdir = 1;
622 len = name_len;
623 } else {
624 dotdir = 0;
625 len = strlen(name);
626 }
627 if (len != name_len) {
628 copy_to_user(d2->d_name, name, len);
629 put_user(0, d2->d_name + len);
630 put_user(len, &d2->d_reclen);
631 put_user(ino, &d2->d_ino);
632 put_user(offset, &d2->d_off);
633 slen = name_len - len;
634 copy_to_user(d1->d_name, name+len+1, slen);
635 put_user(0, d1->d_name+slen);
636 put_user(slen, &d1->d_reclen);
637 } else {
638 put_user(0, d2->d_name);
639 put_user(0, &d2->d_reclen);
640 copy_to_user(d1->d_name, name, len);
641 put_user(0, d1->d_name+len);
642 put_user(len, &d1->d_reclen);
643 }
644 PRINTK(("FAT d1=%p d2=%p len=%d, name_len=%d\n",
645 d1, d2, len, name_len));
646
647 return 0;
648 }
649
fat_dir_ioctl(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)650 int fat_dir_ioctl(struct inode * inode, struct file * filp,
651 unsigned int cmd, unsigned long arg)
652 {
653 int err;
654 /*
655 * We want to provide an interface for Samba to be able
656 * to get the short filename for a given long filename.
657 * Samba should use this ioctl instead of readdir() to
658 * get the information it needs.
659 */
660 switch (cmd) {
661 case VFAT_IOCTL_READDIR_BOTH: {
662 struct dirent *d1 = (struct dirent *)arg;
663 err = verify_area(VERIFY_WRITE, d1, sizeof(struct dirent[2]));
664 if (err)
665 return err;
666 put_user(0, &d1->d_reclen);
667 return fat_readdirx(inode,filp,(void *)arg,
668 vfat_ioctl_fill, 0, 1);
669 }
670 case VFAT_IOCTL_READDIR_SHORT: {
671 struct dirent *d1 = (struct dirent *)arg;
672 put_user(0, &d1->d_reclen);
673 err = verify_area(VERIFY_WRITE, d1, sizeof(struct dirent[2]));
674 if (err)
675 return err;
676 return fat_readdirx(inode,filp,(void *)arg,
677 vfat_ioctl_fill, 1, 1);
678 }
679 default:
680 /* forward ioctl to CVF extension */
681 if (MSDOS_SB(inode->i_sb)->cvf_format &&
682 MSDOS_SB(inode->i_sb)->cvf_format->cvf_dir_ioctl)
683 return MSDOS_SB(inode->i_sb)->cvf_format
684 ->cvf_dir_ioctl(inode,filp,cmd,arg);
685 return -EINVAL;
686 }
687
688 return 0;
689 }
690
691 /***** See if directory is empty */
fat_dir_empty(struct inode * dir)692 int fat_dir_empty(struct inode *dir)
693 {
694 loff_t pos, i_pos;
695 struct buffer_head *bh;
696 struct msdos_dir_entry *de;
697 int result = 0;
698
699 pos = 0;
700 bh = NULL;
701 while (fat_get_entry(dir,&pos,&bh,&de,&i_pos) > -1) {
702 /* Ignore vfat longname entries */
703 if (de->attr == ATTR_EXT)
704 continue;
705 if (!IS_FREE(de->name) &&
706 strncmp(de->name,MSDOS_DOT , MSDOS_NAME) &&
707 strncmp(de->name,MSDOS_DOTDOT, MSDOS_NAME)) {
708 result = -ENOTEMPTY;
709 break;
710 }
711 }
712 if (bh)
713 fat_brelse(dir->i_sb, bh);
714
715 return result;
716 }
717
718 /* This assumes that size of cluster is above the 32*slots */
719
fat_add_entries(struct inode * dir,int slots,struct buffer_head ** bh,struct msdos_dir_entry ** de,loff_t * i_pos)720 int fat_add_entries(struct inode *dir,int slots, struct buffer_head **bh,
721 struct msdos_dir_entry **de, loff_t *i_pos)
722 {
723 struct super_block *sb = dir->i_sb;
724 loff_t offset, curr;
725 int row;
726 struct buffer_head *new_bh;
727
728 offset = curr = 0;
729 *bh = NULL;
730 row = 0;
731 while (fat_get_entry(dir,&curr,bh,de,i_pos) > -1) {
732 if (IS_FREE((*de)->name)) {
733 if (++row == slots)
734 return offset;
735 } else {
736 row = 0;
737 offset = curr;
738 }
739 }
740 if ((dir->i_ino == MSDOS_ROOT_INO) && (MSDOS_SB(sb)->fat_bits != 32))
741 return -ENOSPC;
742 new_bh = fat_extend_dir(dir);
743 if (!new_bh)
744 return -ENOSPC;
745 fat_brelse(sb, new_bh);
746 do fat_get_entry(dir,&curr,bh,de,i_pos); while (++row<slots);
747 return offset;
748 }
749
fat_new_dir(struct inode * dir,struct inode * parent,int is_vfat)750 int fat_new_dir(struct inode *dir, struct inode *parent, int is_vfat)
751 {
752 struct super_block *sb = dir->i_sb;
753 struct buffer_head *bh;
754 struct msdos_dir_entry *de;
755 __u16 date, time;
756
757 if ((bh = fat_extend_dir(dir)) == NULL) return -ENOSPC;
758 /* zeroed out, so... */
759 fat_date_unix2dos(dir->i_mtime,&time,&date);
760 de = (struct msdos_dir_entry*)&bh->b_data[0];
761 memcpy(de[0].name,MSDOS_DOT,MSDOS_NAME);
762 memcpy(de[1].name,MSDOS_DOTDOT,MSDOS_NAME);
763 de[0].attr = de[1].attr = ATTR_DIR;
764 de[0].time = de[1].time = CT_LE_W(time);
765 de[0].date = de[1].date = CT_LE_W(date);
766 if (is_vfat) { /* extra timestamps */
767 de[0].ctime = de[1].ctime = CT_LE_W(time);
768 de[0].adate = de[0].cdate =
769 de[1].adate = de[1].cdate = CT_LE_W(date);
770 }
771 de[0].start = CT_LE_W(MSDOS_I(dir)->i_logstart);
772 de[0].starthi = CT_LE_W(MSDOS_I(dir)->i_logstart>>16);
773 de[1].start = CT_LE_W(MSDOS_I(parent)->i_logstart);
774 de[1].starthi = CT_LE_W(MSDOS_I(parent)->i_logstart>>16);
775 fat_mark_buffer_dirty(sb, bh);
776 fat_brelse(sb, bh);
777 dir->i_atime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
778 mark_inode_dirty(dir);
779
780 return 0;
781 }
782
783 /*
784 * Overrides for Emacs so that we follow Linus's tabbing style.
785 * Emacs will notice this stuff at the end of the file and automatically
786 * adjust the settings for this buffer only. This must remain at the end
787 * of the file.
788 * ---------------------------------------------------------------------------
789 * Local variables:
790 * c-indent-level: 8
791 * c-brace-imaginary-offset: 0
792 * c-brace-offset: -8
793 * c-argdecl-indent: 8
794 * c-label-offset: -8
795 * c-continued-statement-offset: 8
796 * c-continued-brace-offset: 0
797 * End:
798 */
799