PageRenderTime 59ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/release/src/linux/linux/fs/fat/dir.c

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