PageRenderTime 73ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/fs/fat/dir.c

https://gitlab.com/LiquidSmooth-Devices/android_kernel_shooter
C | 1373 lines | 1098 code | 139 blank | 136 comment | 284 complexity | 81fa040a36a8fe8c202aa710313b0f7a MD5 | raw file
Possible License(s): GPL-2.0
  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/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/time.h>
  18. #include <linux/buffer_head.h>
  19. #include <linux/compat.h>
  20. #include <asm/uaccess.h>
  21. #include <linux/kernel.h>
  22. #include "fat.h"
  23. /*
  24. * Maximum buffer size of short name.
  25. * [(MSDOS_NAME + '.') * max one char + nul]
  26. * For msdos style, ['.' (hidden) + MSDOS_NAME + '.' + nul]
  27. */
  28. #define FAT_MAX_SHORT_SIZE ((MSDOS_NAME + 1) * NLS_MAX_CHARSET_SIZE + 1)
  29. /*
  30. * Maximum buffer size of unicode chars from slots.
  31. * [(max longname slots * 13 (size in a slot) + nul) * sizeof(wchar_t)]
  32. */
  33. #define FAT_MAX_UNI_CHARS ((MSDOS_SLOTS - 1) * 13 + 1)
  34. #define FAT_MAX_UNI_SIZE (FAT_MAX_UNI_CHARS * sizeof(wchar_t))
  35. static inline loff_t fat_make_i_pos(struct super_block *sb,
  36. struct buffer_head *bh,
  37. struct msdos_dir_entry *de)
  38. {
  39. return ((loff_t)bh->b_blocknr << MSDOS_SB(sb)->dir_per_block_bits)
  40. | (de - (struct msdos_dir_entry *)bh->b_data);
  41. }
  42. static inline void fat_dir_readahead(struct inode *dir, sector_t iblock,
  43. sector_t phys)
  44. {
  45. struct super_block *sb = dir->i_sb;
  46. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  47. struct buffer_head *bh;
  48. int sec;
  49. /* This is not a first sector of cluster, or sec_per_clus == 1 */
  50. if ((iblock & (sbi->sec_per_clus - 1)) || sbi->sec_per_clus == 1)
  51. return;
  52. /* root dir of FAT12/FAT16 */
  53. if ((sbi->fat_bits != 32) && (dir->i_ino == MSDOS_ROOT_INO))
  54. return;
  55. bh = sb_find_get_block(sb, phys);
  56. if (bh == NULL || !buffer_uptodate(bh)) {
  57. for (sec = 0; sec < sbi->sec_per_clus; sec++)
  58. sb_breadahead(sb, phys + sec);
  59. }
  60. brelse(bh);
  61. }
  62. /* Returns the inode number of the directory entry at offset pos. If bh is
  63. non-NULL, it is brelse'd before. Pos is incremented. The buffer header is
  64. returned in bh.
  65. AV. Most often we do it item-by-item. Makes sense to optimize.
  66. AV. OK, there we go: if both bh and de are non-NULL we assume that we just
  67. AV. want the next entry (took one explicit de=NULL in vfat/namei.c).
  68. AV. It's done in fat_get_entry() (inlined), here the slow case lives.
  69. AV. Additionally, when we return -1 (i.e. reached the end of directory)
  70. AV. we make bh NULL.
  71. */
  72. static int fat__get_entry(struct inode *dir, loff_t *pos,
  73. struct buffer_head **bh, struct msdos_dir_entry **de)
  74. {
  75. struct super_block *sb = dir->i_sb;
  76. sector_t phys, iblock;
  77. unsigned long mapped_blocks;
  78. int err, offset;
  79. next:
  80. if (*bh)
  81. brelse(*bh);
  82. *bh = NULL;
  83. iblock = *pos >> sb->s_blocksize_bits;
  84. err = fat_bmap(dir, iblock, &phys, &mapped_blocks, 0);
  85. if (err || !phys)
  86. return -1; /* beyond EOF or error */
  87. fat_dir_readahead(dir, iblock, phys);
  88. *bh = sb_bread(sb, phys);
  89. if (*bh == NULL) {
  90. fat_msg(sb, KERN_ERR, "Directory bread(block %llu) failed",
  91. (llu)phys);
  92. /* skip this block */
  93. *pos = (iblock + 1) << sb->s_blocksize_bits;
  94. goto next;
  95. }
  96. offset = *pos & (sb->s_blocksize - 1);
  97. *pos += sizeof(struct msdos_dir_entry);
  98. *de = (struct msdos_dir_entry *)((*bh)->b_data + offset);
  99. return 0;
  100. }
  101. static inline int fat_get_entry(struct inode *dir, loff_t *pos,
  102. struct buffer_head **bh,
  103. struct msdos_dir_entry **de)
  104. {
  105. /* Fast stuff first */
  106. if (*bh && *de &&
  107. (*de - (struct msdos_dir_entry *)(*bh)->b_data) < MSDOS_SB(dir->i_sb)->dir_per_block - 1) {
  108. *pos += sizeof(struct msdos_dir_entry);
  109. (*de)++;
  110. return 0;
  111. }
  112. return fat__get_entry(dir, pos, bh, de);
  113. }
  114. /*
  115. * Convert Unicode 16 to UTF-8, translated Unicode, or ASCII.
  116. * If uni_xlate is enabled and we can't get a 1:1 conversion, use a
  117. * colon as an escape character since it is normally invalid on the vfat
  118. * filesystem. The following four characters are the hexadecimal digits
  119. * of Unicode value. This lets us do a full dump and restore of Unicode
  120. * filenames. We could get into some trouble with long Unicode names,
  121. * but ignore that right now.
  122. * Ahem... Stack smashing in ring 0 isn't fun. Fixed.
  123. */
  124. static int uni16_to_x8(struct super_block *sb, unsigned char *ascii,
  125. const wchar_t *uni, int len, struct nls_table *nls)
  126. {
  127. int uni_xlate = MSDOS_SB(sb)->options.unicode_xlate;
  128. const wchar_t *ip;
  129. wchar_t ec;
  130. unsigned char *op;
  131. int charlen;
  132. ip = uni;
  133. op = ascii;
  134. while (*ip && ((len - NLS_MAX_CHARSET_SIZE) > 0)) {
  135. ec = *ip++;
  136. if ((charlen = nls->uni2char(ec, op, NLS_MAX_CHARSET_SIZE)) > 0) {
  137. op += charlen;
  138. len -= charlen;
  139. } else {
  140. if (uni_xlate == 1) {
  141. *op++ = ':';
  142. op = pack_hex_byte(op, ec >> 8);
  143. op = pack_hex_byte(op, ec);
  144. len -= 5;
  145. } else {
  146. *op++ = '?';
  147. len--;
  148. }
  149. }
  150. }
  151. if (unlikely(*ip)) {
  152. fat_msg(sb, KERN_WARNING, "filename was truncated while "
  153. "converting.");
  154. }
  155. *op = 0;
  156. return (op - ascii);
  157. }
  158. static inline int fat_uni_to_x8(struct super_block *sb, const wchar_t *uni,
  159. unsigned char *buf, int size)
  160. {
  161. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  162. if (sbi->options.utf8)
  163. return utf16s_to_utf8s(uni, FAT_MAX_UNI_CHARS,
  164. UTF16_HOST_ENDIAN, buf, size);
  165. else
  166. return uni16_to_x8(sb, buf, uni, size, sbi->nls_io);
  167. }
  168. static inline int
  169. fat_short2uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
  170. {
  171. int charlen;
  172. charlen = t->char2uni(c, clen, uni);
  173. if (charlen < 0) {
  174. *uni = 0x003f; /* a question mark */
  175. charlen = 1;
  176. }
  177. return charlen;
  178. }
  179. static inline int
  180. fat_short2lower_uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
  181. {
  182. int charlen;
  183. wchar_t wc;
  184. charlen = t->char2uni(c, clen, &wc);
  185. if (charlen < 0) {
  186. *uni = 0x003f; /* a question mark */
  187. charlen = 1;
  188. } else if (charlen <= 1) {
  189. unsigned char nc = t->charset2lower[*c];
  190. if (!nc)
  191. nc = *c;
  192. if ( (charlen = t->char2uni(&nc, 1, uni)) < 0) {
  193. *uni = 0x003f; /* a question mark */
  194. charlen = 1;
  195. }
  196. } else
  197. *uni = wc;
  198. return charlen;
  199. }
  200. static inline int
  201. fat_shortname2uni(struct nls_table *nls, unsigned char *buf, int buf_size,
  202. wchar_t *uni_buf, unsigned short opt, int lower)
  203. {
  204. int len = 0;
  205. if (opt & VFAT_SFN_DISPLAY_LOWER)
  206. len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
  207. else if (opt & VFAT_SFN_DISPLAY_WIN95)
  208. len = fat_short2uni(nls, buf, buf_size, uni_buf);
  209. else if (opt & VFAT_SFN_DISPLAY_WINNT) {
  210. if (lower)
  211. len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
  212. else
  213. len = fat_short2uni(nls, buf, buf_size, uni_buf);
  214. } else
  215. len = fat_short2uni(nls, buf, buf_size, uni_buf);
  216. return len;
  217. }
  218. static inline int fat_name_match(struct msdos_sb_info *sbi,
  219. const unsigned char *a, int a_len,
  220. const unsigned char *b, int b_len)
  221. {
  222. if (a_len != b_len)
  223. return 0;
  224. if (sbi->options.name_check != 's')
  225. return !nls_strnicmp(sbi->nls_io, a, b, a_len);
  226. else
  227. return !memcmp(a, b, a_len);
  228. }
  229. enum { PARSE_INVALID = 1, PARSE_NOT_LONGNAME, PARSE_EOF, };
  230. /**
  231. * fat_parse_long - Parse extended directory entry.
  232. *
  233. * This function returns zero on success, negative value on error, or one of
  234. * the following:
  235. *
  236. * %PARSE_INVALID - Directory entry is invalid.
  237. * %PARSE_NOT_LONGNAME - Directory entry does not contain longname.
  238. * %PARSE_EOF - Directory has no more entries.
  239. */
  240. static int fat_parse_long(struct inode *dir, loff_t *pos,
  241. struct buffer_head **bh, struct msdos_dir_entry **de,
  242. wchar_t **unicode, unsigned char *nr_slots)
  243. {
  244. struct msdos_dir_slot *ds;
  245. unsigned char id, slot, slots, alias_checksum;
  246. if (!*unicode) {
  247. *unicode = __getname();
  248. if (!*unicode) {
  249. brelse(*bh);
  250. return -ENOMEM;
  251. }
  252. }
  253. parse_long:
  254. slots = 0;
  255. ds = (struct msdos_dir_slot *)*de;
  256. id = ds->id;
  257. if (!(id & 0x40))
  258. return PARSE_INVALID;
  259. slots = id & ~0x40;
  260. if (slots > 20 || !slots) /* ceil(256 * 2 / 26) */
  261. return PARSE_INVALID;
  262. *nr_slots = slots;
  263. alias_checksum = ds->alias_checksum;
  264. slot = slots;
  265. while (1) {
  266. int offset;
  267. slot--;
  268. offset = slot * 13;
  269. fat16_towchar(*unicode + offset, ds->name0_4, 5);
  270. fat16_towchar(*unicode + offset + 5, ds->name5_10, 6);
  271. fat16_towchar(*unicode + offset + 11, ds->name11_12, 2);
  272. if (ds->id & 0x40)
  273. (*unicode)[offset + 13] = 0;
  274. if (fat_get_entry(dir, pos, bh, de) < 0)
  275. return PARSE_EOF;
  276. if (slot == 0)
  277. break;
  278. ds = (struct msdos_dir_slot *)*de;
  279. if (ds->attr != ATTR_EXT)
  280. return PARSE_NOT_LONGNAME;
  281. if ((ds->id & ~0x40) != slot)
  282. goto parse_long;
  283. if (ds->alias_checksum != alias_checksum)
  284. goto parse_long;
  285. }
  286. if ((*de)->name[0] == DELETED_FLAG)
  287. return PARSE_INVALID;
  288. if ((*de)->attr == ATTR_EXT)
  289. goto parse_long;
  290. if (IS_FREE((*de)->name) || ((*de)->attr & ATTR_VOLUME))
  291. return PARSE_INVALID;
  292. if (fat_checksum((*de)->name) != alias_checksum)
  293. *nr_slots = 0;
  294. return 0;
  295. }
  296. /*
  297. * Return values: negative -> error, 0 -> not found, positive -> found,
  298. * value is the total amount of slots, including the shortname entry.
  299. */
  300. int fat_search_long(struct inode *inode, const unsigned char *name,
  301. int name_len, struct fat_slot_info *sinfo)
  302. {
  303. struct super_block *sb = inode->i_sb;
  304. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  305. struct buffer_head *bh = NULL;
  306. struct msdos_dir_entry *de;
  307. struct nls_table *nls_disk = sbi->nls_disk;
  308. unsigned char nr_slots;
  309. wchar_t bufuname[14];
  310. wchar_t *unicode = NULL;
  311. unsigned char work[MSDOS_NAME];
  312. unsigned char bufname[FAT_MAX_SHORT_SIZE];
  313. unsigned short opt_shortname = sbi->options.shortname;
  314. loff_t cpos = 0;
  315. int chl, i, j, last_u, err, len;
  316. err = -ENOENT;
  317. while (1) {
  318. if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
  319. goto end_of_dir;
  320. parse_record:
  321. nr_slots = 0;
  322. if (de->name[0] == DELETED_FLAG)
  323. continue;
  324. if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
  325. continue;
  326. if (de->attr != ATTR_EXT && IS_FREE(de->name))
  327. continue;
  328. if (de->attr == ATTR_EXT) {
  329. int status = fat_parse_long(inode, &cpos, &bh, &de,
  330. &unicode, &nr_slots);
  331. if (status < 0) {
  332. err = status;
  333. goto end_of_dir;
  334. } else if (status == PARSE_INVALID)
  335. continue;
  336. else if (status == PARSE_NOT_LONGNAME)
  337. goto parse_record;
  338. else if (status == PARSE_EOF)
  339. goto end_of_dir;
  340. }
  341. memcpy(work, de->name, sizeof(de->name));
  342. /* see namei.c, msdos_format_name */
  343. if (work[0] == 0x05)
  344. work[0] = 0xE5;
  345. for (i = 0, j = 0, last_u = 0; i < 8;) {
  346. if (!work[i])
  347. break;
  348. chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
  349. &bufuname[j++], opt_shortname,
  350. de->lcase & CASE_LOWER_BASE);
  351. if (chl <= 1) {
  352. if (work[i] != ' ')
  353. last_u = j;
  354. } else {
  355. last_u = j;
  356. }
  357. i += chl;
  358. }
  359. j = last_u;
  360. fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
  361. for (i = 8; i < MSDOS_NAME;) {
  362. if (!work[i])
  363. break;
  364. chl = fat_shortname2uni(nls_disk, &work[i],
  365. MSDOS_NAME - i,
  366. &bufuname[j++], opt_shortname,
  367. de->lcase & CASE_LOWER_EXT);
  368. if (chl <= 1) {
  369. if (work[i] != ' ')
  370. last_u = j;
  371. } else {
  372. last_u = j;
  373. }
  374. i += chl;
  375. }
  376. if (!last_u)
  377. continue;
  378. /* Compare shortname */
  379. bufuname[last_u] = 0x0000;
  380. len = fat_uni_to_x8(sb, bufuname, bufname, sizeof(bufname));
  381. if (fat_name_match(sbi, name, name_len, bufname, len))
  382. goto found;
  383. if (nr_slots) {
  384. void *longname = unicode + FAT_MAX_UNI_CHARS;
  385. int size = PATH_MAX - FAT_MAX_UNI_SIZE;
  386. /* Compare longname */
  387. len = fat_uni_to_x8(sb, unicode, longname, size);
  388. if (fat_name_match(sbi, name, name_len, longname, len))
  389. goto found;
  390. }
  391. }
  392. found:
  393. nr_slots++; /* include the de */
  394. sinfo->slot_off = cpos - nr_slots * sizeof(*de);
  395. sinfo->nr_slots = nr_slots;
  396. sinfo->de = de;
  397. sinfo->bh = bh;
  398. sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
  399. err = 0;
  400. end_of_dir:
  401. if (unicode)
  402. __putname(unicode);
  403. return err;
  404. }
  405. EXPORT_SYMBOL_GPL(fat_search_long);
  406. struct fat_ioctl_filldir_callback {
  407. void __user *dirent;
  408. int result;
  409. /* for dir ioctl */
  410. const char *longname;
  411. int long_len;
  412. const char *shortname;
  413. int short_len;
  414. };
  415. static int __fat_readdir(struct inode *inode, struct file *filp, void *dirent,
  416. filldir_t filldir, int short_only, int both)
  417. {
  418. struct super_block *sb = inode->i_sb;
  419. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  420. struct buffer_head *bh;
  421. struct msdos_dir_entry *de;
  422. struct nls_table *nls_disk = sbi->nls_disk;
  423. unsigned char nr_slots;
  424. wchar_t bufuname[14];
  425. wchar_t *unicode = NULL;
  426. unsigned char c, work[MSDOS_NAME];
  427. unsigned char bufname[FAT_MAX_SHORT_SIZE], *ptname = bufname;
  428. unsigned short opt_shortname = sbi->options.shortname;
  429. int isvfat = sbi->options.isvfat;
  430. int nocase = sbi->options.nocase;
  431. const char *fill_name = NULL;
  432. unsigned long inum;
  433. unsigned long lpos, dummy, *furrfu = &lpos;
  434. loff_t cpos;
  435. int chi, chl, i, i2, j, last, last_u, dotoffset = 0, fill_len = 0;
  436. int ret = 0;
  437. lock_super(sb);
  438. cpos = filp->f_pos;
  439. /* Fake . and .. for the root directory. */
  440. if (inode->i_ino == MSDOS_ROOT_INO) {
  441. while (cpos < 2) {
  442. if (filldir(dirent, "..", cpos+1, cpos, MSDOS_ROOT_INO, DT_DIR) < 0)
  443. goto out;
  444. cpos++;
  445. filp->f_pos++;
  446. }
  447. if (cpos == 2) {
  448. dummy = 2;
  449. furrfu = &dummy;
  450. cpos = 0;
  451. }
  452. }
  453. if (cpos & (sizeof(struct msdos_dir_entry) - 1)) {
  454. ret = -ENOENT;
  455. goto out;
  456. }
  457. bh = NULL;
  458. get_new:
  459. if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
  460. goto end_of_dir;
  461. parse_record:
  462. nr_slots = 0;
  463. /*
  464. * Check for long filename entry, but if short_only, we don't
  465. * need to parse long filename.
  466. */
  467. if (isvfat && !short_only) {
  468. if (de->name[0] == DELETED_FLAG)
  469. goto record_end;
  470. if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
  471. goto record_end;
  472. if (de->attr != ATTR_EXT && IS_FREE(de->name))
  473. goto record_end;
  474. } else {
  475. if ((de->attr & ATTR_VOLUME) || IS_FREE(de->name))
  476. goto record_end;
  477. }
  478. if (isvfat && de->attr == ATTR_EXT) {
  479. int status = fat_parse_long(inode, &cpos, &bh, &de,
  480. &unicode, &nr_slots);
  481. if (status < 0) {
  482. filp->f_pos = cpos;
  483. ret = status;
  484. goto out;
  485. } else if (status == PARSE_INVALID)
  486. goto record_end;
  487. else if (status == PARSE_NOT_LONGNAME)
  488. goto parse_record;
  489. else if (status == PARSE_EOF)
  490. goto end_of_dir;
  491. if (nr_slots) {
  492. void *longname = unicode + FAT_MAX_UNI_CHARS;
  493. int size = PATH_MAX - FAT_MAX_UNI_SIZE;
  494. int len = fat_uni_to_x8(sb, unicode, longname, size);
  495. fill_name = longname;
  496. fill_len = len;
  497. /* !both && !short_only, so we don't need shortname. */
  498. if (!both)
  499. goto start_filldir;
  500. }
  501. }
  502. if (sbi->options.dotsOK) {
  503. ptname = bufname;
  504. dotoffset = 0;
  505. if (de->attr & ATTR_HIDDEN) {
  506. *ptname++ = '.';
  507. dotoffset = 1;
  508. }
  509. }
  510. memcpy(work, de->name, sizeof(de->name));
  511. /* see namei.c, msdos_format_name */
  512. if (work[0] == 0x05)
  513. work[0] = 0xE5;
  514. for (i = 0, j = 0, last = 0, last_u = 0; i < 8;) {
  515. if (!(c = work[i]))
  516. break;
  517. chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
  518. &bufuname[j++], opt_shortname,
  519. de->lcase & CASE_LOWER_BASE);
  520. if (chl <= 1) {
  521. ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
  522. if (c != ' ') {
  523. last = i;
  524. last_u = j;
  525. }
  526. } else {
  527. last_u = j;
  528. for (chi = 0; chi < chl && i < 8; chi++) {
  529. ptname[i] = work[i];
  530. i++; last = i;
  531. }
  532. }
  533. }
  534. i = last;
  535. j = last_u;
  536. fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
  537. ptname[i++] = '.';
  538. for (i2 = 8; i2 < MSDOS_NAME;) {
  539. if (!(c = work[i2]))
  540. break;
  541. chl = fat_shortname2uni(nls_disk, &work[i2], MSDOS_NAME - i2,
  542. &bufuname[j++], opt_shortname,
  543. de->lcase & CASE_LOWER_EXT);
  544. if (chl <= 1) {
  545. i2++;
  546. ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
  547. if (c != ' ') {
  548. last = i;
  549. last_u = j;
  550. }
  551. } else {
  552. last_u = j;
  553. for (chi = 0; chi < chl && i2 < MSDOS_NAME; chi++) {
  554. ptname[i++] = work[i2++];
  555. last = i;
  556. }
  557. }
  558. }
  559. if (!last)
  560. goto record_end;
  561. i = last + dotoffset;
  562. j = last_u;
  563. if (isvfat) {
  564. bufuname[j] = 0x0000;
  565. i = fat_uni_to_x8(sb, bufuname, bufname, sizeof(bufname));
  566. }
  567. if (nr_slots) {
  568. /* hack for fat_ioctl_filldir() */
  569. struct fat_ioctl_filldir_callback *p = dirent;
  570. p->longname = fill_name;
  571. p->long_len = fill_len;
  572. p->shortname = bufname;
  573. p->short_len = i;
  574. fill_name = NULL;
  575. fill_len = 0;
  576. } else {
  577. fill_name = bufname;
  578. fill_len = i;
  579. }
  580. start_filldir:
  581. lpos = cpos - (nr_slots + 1) * sizeof(struct msdos_dir_entry);
  582. if (!memcmp(de->name, MSDOS_DOT, MSDOS_NAME))
  583. inum = inode->i_ino;
  584. else if (!memcmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
  585. inum = parent_ino(filp->f_path.dentry);
  586. } else {
  587. loff_t i_pos = fat_make_i_pos(sb, bh, de);
  588. struct inode *tmp = fat_iget(sb, i_pos);
  589. if (tmp) {
  590. inum = tmp->i_ino;
  591. iput(tmp);
  592. } else
  593. inum = iunique(sb, MSDOS_ROOT_INO);
  594. }
  595. if (filldir(dirent, fill_name, fill_len, *furrfu, inum,
  596. (de->attr & ATTR_DIR) ? DT_DIR : DT_REG) < 0)
  597. goto fill_failed;
  598. record_end:
  599. furrfu = &lpos;
  600. filp->f_pos = cpos;
  601. goto get_new;
  602. end_of_dir:
  603. filp->f_pos = cpos;
  604. fill_failed:
  605. brelse(bh);
  606. if (unicode)
  607. __putname(unicode);
  608. out:
  609. unlock_super(sb);
  610. return ret;
  611. }
  612. static int fat_readdir(struct file *filp, void *dirent, filldir_t filldir)
  613. {
  614. struct inode *inode = filp->f_path.dentry->d_inode;
  615. return __fat_readdir(inode, filp, dirent, filldir, 0, 0);
  616. }
  617. #define FAT_IOCTL_FILLDIR_FUNC(func, dirent_type) \
  618. static int func(void *__buf, const char *name, int name_len, \
  619. loff_t offset, u64 ino, unsigned int d_type) \
  620. { \
  621. struct fat_ioctl_filldir_callback *buf = __buf; \
  622. struct dirent_type __user *d1 = buf->dirent; \
  623. struct dirent_type __user *d2 = d1 + 1; \
  624. \
  625. if (buf->result) \
  626. return -EINVAL; \
  627. buf->result++; \
  628. \
  629. if (name != NULL) { \
  630. /* dirent has only short name */ \
  631. if (name_len >= sizeof(d1->d_name)) \
  632. name_len = sizeof(d1->d_name) - 1; \
  633. \
  634. if (put_user(0, d2->d_name) || \
  635. put_user(0, &d2->d_reclen) || \
  636. copy_to_user(d1->d_name, name, name_len) || \
  637. put_user(0, d1->d_name + name_len) || \
  638. put_user(name_len, &d1->d_reclen)) \
  639. goto efault; \
  640. } else { \
  641. /* dirent has short and long name */ \
  642. const char *longname = buf->longname; \
  643. int long_len = buf->long_len; \
  644. const char *shortname = buf->shortname; \
  645. int short_len = buf->short_len; \
  646. \
  647. if (long_len >= sizeof(d1->d_name)) \
  648. long_len = sizeof(d1->d_name) - 1; \
  649. if (short_len >= sizeof(d1->d_name)) \
  650. short_len = sizeof(d1->d_name) - 1; \
  651. \
  652. if (copy_to_user(d2->d_name, longname, long_len) || \
  653. put_user(0, d2->d_name + long_len) || \
  654. put_user(long_len, &d2->d_reclen) || \
  655. put_user(ino, &d2->d_ino) || \
  656. put_user(offset, &d2->d_off) || \
  657. copy_to_user(d1->d_name, shortname, short_len) || \
  658. put_user(0, d1->d_name + short_len) || \
  659. put_user(short_len, &d1->d_reclen)) \
  660. goto efault; \
  661. } \
  662. return 0; \
  663. efault: \
  664. buf->result = -EFAULT; \
  665. return -EFAULT; \
  666. }
  667. FAT_IOCTL_FILLDIR_FUNC(fat_ioctl_filldir, __fat_dirent)
  668. static int fat_ioctl_readdir(struct inode *inode, struct file *filp,
  669. void __user *dirent, filldir_t filldir,
  670. int short_only, int both)
  671. {
  672. struct fat_ioctl_filldir_callback buf;
  673. int ret;
  674. buf.dirent = dirent;
  675. buf.result = 0;
  676. mutex_lock(&inode->i_mutex);
  677. ret = -ENOENT;
  678. if (!IS_DEADDIR(inode)) {
  679. ret = __fat_readdir(inode, filp, &buf, filldir,
  680. short_only, both);
  681. }
  682. mutex_unlock(&inode->i_mutex);
  683. if (ret >= 0)
  684. ret = buf.result;
  685. return ret;
  686. }
  687. static int fat_ioctl_volume_id(struct inode *dir)
  688. {
  689. struct super_block *sb = dir->i_sb;
  690. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  691. return sbi->vol_id;
  692. }
  693. static long fat_dir_ioctl(struct file *filp, unsigned int cmd,
  694. unsigned long arg)
  695. {
  696. struct inode *inode = filp->f_path.dentry->d_inode;
  697. struct __fat_dirent __user *d1 = (struct __fat_dirent __user *)arg;
  698. int short_only, both;
  699. switch (cmd) {
  700. case VFAT_IOCTL_READDIR_SHORT:
  701. short_only = 1;
  702. both = 0;
  703. break;
  704. case VFAT_IOCTL_READDIR_BOTH:
  705. short_only = 0;
  706. both = 1;
  707. break;
  708. case VFAT_IOCTL_GET_VOLUME_ID:
  709. return fat_ioctl_volume_id(inode);
  710. default:
  711. return fat_generic_ioctl(filp, cmd, arg);
  712. }
  713. if (!access_ok(VERIFY_WRITE, d1, sizeof(struct __fat_dirent[2])))
  714. return -EFAULT;
  715. /*
  716. * Yes, we don't need this put_user() absolutely. However old
  717. * code didn't return the right value. So, app use this value,
  718. * in order to check whether it is EOF.
  719. */
  720. if (put_user(0, &d1->d_reclen))
  721. return -EFAULT;
  722. return fat_ioctl_readdir(inode, filp, d1, fat_ioctl_filldir,
  723. short_only, both);
  724. }
  725. #ifdef CONFIG_COMPAT
  726. #define VFAT_IOCTL_READDIR_BOTH32 _IOR('r', 1, struct compat_dirent[2])
  727. #define VFAT_IOCTL_READDIR_SHORT32 _IOR('r', 2, struct compat_dirent[2])
  728. FAT_IOCTL_FILLDIR_FUNC(fat_compat_ioctl_filldir, compat_dirent)
  729. static long fat_compat_dir_ioctl(struct file *filp, unsigned cmd,
  730. unsigned long arg)
  731. {
  732. struct inode *inode = filp->f_path.dentry->d_inode;
  733. struct compat_dirent __user *d1 = compat_ptr(arg);
  734. int short_only, both;
  735. switch (cmd) {
  736. case VFAT_IOCTL_READDIR_SHORT32:
  737. short_only = 1;
  738. both = 0;
  739. break;
  740. case VFAT_IOCTL_READDIR_BOTH32:
  741. short_only = 0;
  742. both = 1;
  743. break;
  744. case VFAT_IOCTL_GET_VOLUME_ID:
  745. return fat_ioctl_volume_id(inode);
  746. default:
  747. return fat_generic_ioctl(filp, cmd, (unsigned long)arg);
  748. }
  749. if (!access_ok(VERIFY_WRITE, d1, sizeof(struct compat_dirent[2])))
  750. return -EFAULT;
  751. /*
  752. * Yes, we don't need this put_user() absolutely. However old
  753. * code didn't return the right value. So, app use this value,
  754. * in order to check whether it is EOF.
  755. */
  756. if (put_user(0, &d1->d_reclen))
  757. return -EFAULT;
  758. return fat_ioctl_readdir(inode, filp, d1, fat_compat_ioctl_filldir,
  759. short_only, both);
  760. }
  761. #endif /* CONFIG_COMPAT */
  762. const struct file_operations fat_dir_operations = {
  763. .llseek = generic_file_llseek,
  764. .read = generic_read_dir,
  765. .readdir = fat_readdir,
  766. .unlocked_ioctl = fat_dir_ioctl,
  767. #ifdef CONFIG_COMPAT
  768. .compat_ioctl = fat_compat_dir_ioctl,
  769. #endif
  770. .fsync = fat_file_fsync,
  771. };
  772. static int fat_get_short_entry(struct inode *dir, loff_t *pos,
  773. struct buffer_head **bh,
  774. struct msdos_dir_entry **de)
  775. {
  776. while (fat_get_entry(dir, pos, bh, de) >= 0) {
  777. /* free entry or long name entry or volume label */
  778. if (!IS_FREE((*de)->name) && !((*de)->attr & ATTR_VOLUME))
  779. return 0;
  780. }
  781. return -ENOENT;
  782. }
  783. /*
  784. * The ".." entry can not provide the "struct fat_slot_info" informations
  785. * for inode. So, this function provide the some informations only.
  786. */
  787. int fat_get_dotdot_entry(struct inode *dir, struct buffer_head **bh,
  788. struct msdos_dir_entry **de, loff_t *i_pos)
  789. {
  790. loff_t offset;
  791. offset = 0;
  792. *bh = NULL;
  793. while (fat_get_short_entry(dir, &offset, bh, de) >= 0) {
  794. if (!strncmp((*de)->name, MSDOS_DOTDOT, MSDOS_NAME)) {
  795. *i_pos = fat_make_i_pos(dir->i_sb, *bh, *de);
  796. return 0;
  797. }
  798. }
  799. return -ENOENT;
  800. }
  801. EXPORT_SYMBOL_GPL(fat_get_dotdot_entry);
  802. /* See if directory is empty */
  803. int fat_dir_empty(struct inode *dir)
  804. {
  805. struct buffer_head *bh;
  806. struct msdos_dir_entry *de;
  807. loff_t cpos;
  808. int result = 0;
  809. bh = NULL;
  810. cpos = 0;
  811. while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
  812. if (strncmp(de->name, MSDOS_DOT , MSDOS_NAME) &&
  813. strncmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
  814. result = -ENOTEMPTY;
  815. break;
  816. }
  817. }
  818. brelse(bh);
  819. return result;
  820. }
  821. EXPORT_SYMBOL_GPL(fat_dir_empty);
  822. /*
  823. * fat_subdirs counts the number of sub-directories of dir. It can be run
  824. * on directories being created.
  825. */
  826. int fat_subdirs(struct inode *dir)
  827. {
  828. struct buffer_head *bh;
  829. struct msdos_dir_entry *de;
  830. loff_t cpos;
  831. int count = 0;
  832. bh = NULL;
  833. cpos = 0;
  834. while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
  835. if (de->attr & ATTR_DIR)
  836. count++;
  837. }
  838. brelse(bh);
  839. return count;
  840. }
  841. /*
  842. * Scans a directory for a given file (name points to its formatted name).
  843. * Returns an error code or zero.
  844. */
  845. int fat_scan(struct inode *dir, const unsigned char *name,
  846. struct fat_slot_info *sinfo)
  847. {
  848. struct super_block *sb = dir->i_sb;
  849. sinfo->slot_off = 0;
  850. sinfo->bh = NULL;
  851. while (fat_get_short_entry(dir, &sinfo->slot_off, &sinfo->bh,
  852. &sinfo->de) >= 0) {
  853. if (!strncmp(sinfo->de->name, name, MSDOS_NAME)) {
  854. sinfo->slot_off -= sizeof(*sinfo->de);
  855. sinfo->nr_slots = 1;
  856. sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
  857. return 0;
  858. }
  859. }
  860. return -ENOENT;
  861. }
  862. EXPORT_SYMBOL_GPL(fat_scan);
  863. static int __fat_remove_entries(struct inode *dir, loff_t pos, int nr_slots)
  864. {
  865. struct super_block *sb = dir->i_sb;
  866. struct buffer_head *bh;
  867. struct msdos_dir_entry *de, *endp;
  868. int err = 0, orig_slots;
  869. while (nr_slots) {
  870. bh = NULL;
  871. if (fat_get_entry(dir, &pos, &bh, &de) < 0) {
  872. err = -EIO;
  873. break;
  874. }
  875. orig_slots = nr_slots;
  876. endp = (struct msdos_dir_entry *)(bh->b_data + sb->s_blocksize);
  877. while (nr_slots && de < endp) {
  878. de->name[0] = DELETED_FLAG;
  879. de++;
  880. nr_slots--;
  881. }
  882. mark_buffer_dirty_inode(bh, dir);
  883. if (IS_DIRSYNC(dir))
  884. err = sync_dirty_buffer(bh);
  885. brelse(bh);
  886. if (err)
  887. break;
  888. /* pos is *next* de's position, so this does `- sizeof(de)' */
  889. pos += ((orig_slots - nr_slots) * sizeof(*de)) - sizeof(*de);
  890. }
  891. return err;
  892. }
  893. int fat_remove_entries(struct inode *dir, struct fat_slot_info *sinfo)
  894. {
  895. struct super_block *sb = dir->i_sb;
  896. struct msdos_dir_entry *de;
  897. struct buffer_head *bh;
  898. int err = 0, nr_slots;
  899. /*
  900. * First stage: Remove the shortname. By this, the directory
  901. * entry is removed.
  902. */
  903. nr_slots = sinfo->nr_slots;
  904. de = sinfo->de;
  905. sinfo->de = NULL;
  906. bh = sinfo->bh;
  907. sinfo->bh = NULL;
  908. while (nr_slots && de >= (struct msdos_dir_entry *)bh->b_data) {
  909. de->name[0] = DELETED_FLAG;
  910. de--;
  911. nr_slots--;
  912. }
  913. mark_buffer_dirty_inode(bh, dir);
  914. if (IS_DIRSYNC(dir))
  915. err = sync_dirty_buffer(bh);
  916. brelse(bh);
  917. if (err)
  918. return err;
  919. dir->i_version++;
  920. if (nr_slots) {
  921. /*
  922. * Second stage: remove the remaining longname slots.
  923. * (This directory entry is already removed, and so return
  924. * the success)
  925. */
  926. err = __fat_remove_entries(dir, sinfo->slot_off, nr_slots);
  927. if (err) {
  928. fat_msg(sb, KERN_WARNING,
  929. "Couldn't remove the long name slots");
  930. }
  931. }
  932. dir->i_mtime = dir->i_atime = CURRENT_TIME_SEC;
  933. if (IS_DIRSYNC(dir))
  934. (void)fat_sync_inode(dir);
  935. else
  936. mark_inode_dirty(dir);
  937. return 0;
  938. }
  939. EXPORT_SYMBOL_GPL(fat_remove_entries);
  940. static int fat_zeroed_cluster(struct inode *dir, sector_t blknr, int nr_used,
  941. struct buffer_head **bhs, int nr_bhs)
  942. {
  943. struct super_block *sb = dir->i_sb;
  944. sector_t last_blknr = blknr + MSDOS_SB(sb)->sec_per_clus;
  945. int err, i, n;
  946. /* Zeroing the unused blocks on this cluster */
  947. blknr += nr_used;
  948. n = nr_used;
  949. while (blknr < last_blknr) {
  950. bhs[n] = sb_getblk(sb, blknr);
  951. if (!bhs[n]) {
  952. err = -ENOMEM;
  953. goto error;
  954. }
  955. memset(bhs[n]->b_data, 0, sb->s_blocksize);
  956. set_buffer_uptodate(bhs[n]);
  957. mark_buffer_dirty_inode(bhs[n], dir);
  958. n++;
  959. blknr++;
  960. if (n == nr_bhs) {
  961. if (IS_DIRSYNC(dir)) {
  962. err = fat_sync_bhs(bhs, n);
  963. if (err)
  964. goto error;
  965. }
  966. for (i = 0; i < n; i++)
  967. brelse(bhs[i]);
  968. n = 0;
  969. }
  970. }
  971. if (IS_DIRSYNC(dir)) {
  972. err = fat_sync_bhs(bhs, n);
  973. if (err)
  974. goto error;
  975. }
  976. for (i = 0; i < n; i++)
  977. brelse(bhs[i]);
  978. return 0;
  979. error:
  980. for (i = 0; i < n; i++)
  981. bforget(bhs[i]);
  982. return err;
  983. }
  984. int fat_alloc_new_dir(struct inode *dir, struct timespec *ts)
  985. {
  986. struct super_block *sb = dir->i_sb;
  987. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  988. struct buffer_head *bhs[MAX_BUF_PER_PAGE];
  989. struct msdos_dir_entry *de;
  990. sector_t blknr;
  991. __le16 date, time;
  992. u8 time_cs;
  993. int err, cluster;
  994. err = fat_alloc_clusters(dir, &cluster, 1);
  995. if (err)
  996. goto error;
  997. blknr = fat_clus_to_blknr(sbi, cluster);
  998. bhs[0] = sb_getblk(sb, blknr);
  999. if (!bhs[0]) {
  1000. err = -ENOMEM;
  1001. goto error_free;
  1002. }
  1003. fat_time_unix2fat(sbi, ts, &time, &date, &time_cs);
  1004. de = (struct msdos_dir_entry *)bhs[0]->b_data;
  1005. /* filling the new directory slots ("." and ".." entries) */
  1006. memcpy(de[0].name, MSDOS_DOT, MSDOS_NAME);
  1007. memcpy(de[1].name, MSDOS_DOTDOT, MSDOS_NAME);
  1008. de->attr = de[1].attr = ATTR_DIR;
  1009. de[0].lcase = de[1].lcase = 0;
  1010. de[0].time = de[1].time = time;
  1011. de[0].date = de[1].date = date;
  1012. if (sbi->options.isvfat) {
  1013. /* extra timestamps */
  1014. de[0].ctime = de[1].ctime = time;
  1015. de[0].ctime_cs = de[1].ctime_cs = time_cs;
  1016. de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = date;
  1017. } else {
  1018. de[0].ctime = de[1].ctime = 0;
  1019. de[0].ctime_cs = de[1].ctime_cs = 0;
  1020. de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = 0;
  1021. }
  1022. de[0].start = cpu_to_le16(cluster);
  1023. de[0].starthi = cpu_to_le16(cluster >> 16);
  1024. de[1].start = cpu_to_le16(MSDOS_I(dir)->i_logstart);
  1025. de[1].starthi = cpu_to_le16(MSDOS_I(dir)->i_logstart >> 16);
  1026. de[0].size = de[1].size = 0;
  1027. memset(de + 2, 0, sb->s_blocksize - 2 * sizeof(*de));
  1028. set_buffer_uptodate(bhs[0]);
  1029. mark_buffer_dirty_inode(bhs[0], dir);
  1030. err = fat_zeroed_cluster(dir, blknr, 1, bhs, MAX_BUF_PER_PAGE);
  1031. if (err)
  1032. goto error_free;
  1033. return cluster;
  1034. error_free:
  1035. fat_free_clusters(dir, cluster);
  1036. error:
  1037. return err;
  1038. }
  1039. EXPORT_SYMBOL_GPL(fat_alloc_new_dir);
  1040. static int fat_add_new_entries(struct inode *dir, void *slots, int nr_slots,
  1041. int *nr_cluster, struct msdos_dir_entry **de,
  1042. struct buffer_head **bh, loff_t *i_pos)
  1043. {
  1044. struct super_block *sb = dir->i_sb;
  1045. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  1046. struct buffer_head *bhs[MAX_BUF_PER_PAGE];
  1047. sector_t blknr, start_blknr, last_blknr;
  1048. unsigned long size, copy;
  1049. int err, i, n, offset, cluster[2];
  1050. /*
  1051. * The minimum cluster size is 512bytes, and maximum entry
  1052. * size is 32*slots (672bytes). So, iff the cluster size is
  1053. * 512bytes, we may need two clusters.
  1054. */
  1055. size = nr_slots * sizeof(struct msdos_dir_entry);
  1056. *nr_cluster = (size + (sbi->cluster_size - 1)) >> sbi->cluster_bits;
  1057. BUG_ON(*nr_cluster > 2);
  1058. err = fat_alloc_clusters(dir, cluster, *nr_cluster);
  1059. if (err)
  1060. goto error;
  1061. /*
  1062. * First stage: Fill the directory entry. NOTE: This cluster
  1063. * is not referenced from any inode yet, so updates order is
  1064. * not important.
  1065. */
  1066. i = n = copy = 0;
  1067. do {
  1068. start_blknr = blknr = fat_clus_to_blknr(sbi, cluster[i]);
  1069. last_blknr = start_blknr + sbi->sec_per_clus;
  1070. while (blknr < last_blknr) {
  1071. bhs[n] = sb_getblk(sb, blknr);
  1072. if (!bhs[n]) {
  1073. err = -ENOMEM;
  1074. goto error_nomem;
  1075. }
  1076. /* fill the directory entry */
  1077. copy = min(size, sb->s_blocksize);
  1078. memcpy(bhs[n]->b_data, slots, copy);
  1079. slots += copy;
  1080. size -= copy;
  1081. set_buffer_uptodate(bhs[n]);
  1082. mark_buffer_dirty_inode(bhs[n], dir);
  1083. if (!size)
  1084. break;
  1085. n++;
  1086. blknr++;
  1087. }
  1088. } while (++i < *nr_cluster);
  1089. memset(bhs[n]->b_data + copy, 0, sb->s_blocksize - copy);
  1090. offset = copy - sizeof(struct msdos_dir_entry);
  1091. get_bh(bhs[n]);
  1092. *bh = bhs[n];
  1093. *de = (struct msdos_dir_entry *)((*bh)->b_data + offset);
  1094. *i_pos = fat_make_i_pos(sb, *bh, *de);
  1095. /* Second stage: clear the rest of cluster, and write outs */
  1096. err = fat_zeroed_cluster(dir, start_blknr, ++n, bhs, MAX_BUF_PER_PAGE);
  1097. if (err)
  1098. goto error_free;
  1099. return cluster[0];
  1100. error_free:
  1101. brelse(*bh);
  1102. *bh = NULL;
  1103. n = 0;
  1104. error_nomem:
  1105. for (i = 0; i < n; i++)
  1106. bforget(bhs[i]);
  1107. fat_free_clusters(dir, cluster[0]);
  1108. error:
  1109. return err;
  1110. }
  1111. int fat_add_entries(struct inode *dir, void *slots, int nr_slots,
  1112. struct fat_slot_info *sinfo)
  1113. {
  1114. struct super_block *sb = dir->i_sb;
  1115. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  1116. struct buffer_head *bh, *prev, *bhs[3]; /* 32*slots (672bytes) */
  1117. struct msdos_dir_entry *de;
  1118. int err, free_slots, i, nr_bhs;
  1119. loff_t pos, i_pos;
  1120. sinfo->nr_slots = nr_slots;
  1121. /* First stage: search free direcotry entries */
  1122. free_slots = nr_bhs = 0;
  1123. bh = prev = NULL;
  1124. pos = 0;
  1125. err = -ENOSPC;
  1126. while (fat_get_entry(dir, &pos, &bh, &de) > -1) {
  1127. /* check the maximum size of directory */
  1128. if (pos >= FAT_MAX_DIR_SIZE)
  1129. goto error;
  1130. if (IS_FREE(de->name)) {
  1131. if (prev != bh) {
  1132. get_bh(bh);
  1133. bhs[nr_bhs] = prev = bh;
  1134. nr_bhs++;
  1135. }
  1136. free_slots++;
  1137. if (free_slots == nr_slots)
  1138. goto found;
  1139. } else {
  1140. for (i = 0; i < nr_bhs; i++)
  1141. brelse(bhs[i]);
  1142. prev = NULL;
  1143. free_slots = nr_bhs = 0;
  1144. }
  1145. }
  1146. if (dir->i_ino == MSDOS_ROOT_INO) {
  1147. if (sbi->fat_bits != 32)
  1148. goto error;
  1149. } else if (MSDOS_I(dir)->i_start == 0) {
  1150. fat_msg(sb, KERN_ERR, "Corrupted directory (i_pos %lld)",
  1151. MSDOS_I(dir)->i_pos);
  1152. err = -EIO;
  1153. goto error;
  1154. }
  1155. found:
  1156. err = 0;
  1157. pos -= free_slots * sizeof(*de);
  1158. nr_slots -= free_slots;
  1159. if (free_slots) {
  1160. /*
  1161. * Second stage: filling the free entries with new entries.
  1162. * NOTE: If this slots has shortname, first, we write
  1163. * the long name slots, then write the short name.
  1164. */
  1165. int size = free_slots * sizeof(*de);
  1166. int offset = pos & (sb->s_blocksize - 1);
  1167. int long_bhs = nr_bhs - (nr_slots == 0);
  1168. /* Fill the long name slots. */
  1169. for (i = 0; i < long_bhs; i++) {
  1170. int copy = min_t(int, sb->s_blocksize - offset, size);
  1171. memcpy(bhs[i]->b_data + offset, slots, copy);
  1172. mark_buffer_dirty_inode(bhs[i], dir);
  1173. offset = 0;
  1174. slots += copy;
  1175. size -= copy;
  1176. }
  1177. if (long_bhs && IS_DIRSYNC(dir))
  1178. err = fat_sync_bhs(bhs, long_bhs);
  1179. if (!err && i < nr_bhs) {
  1180. /* Fill the short name slot. */
  1181. int copy = min_t(int, sb->s_blocksize - offset, size);
  1182. memcpy(bhs[i]->b_data + offset, slots, copy);
  1183. mark_buffer_dirty_inode(bhs[i], dir);
  1184. if (IS_DIRSYNC(dir))
  1185. err = sync_dirty_buffer(bhs[i]);
  1186. }
  1187. for (i = 0; i < nr_bhs; i++)
  1188. brelse(bhs[i]);
  1189. if (err)
  1190. goto error_remove;
  1191. }
  1192. if (nr_slots) {
  1193. int cluster, nr_cluster;
  1194. /*
  1195. * Third stage: allocate the cluster for new entries.
  1196. * And initialize the cluster with new entries, then
  1197. * add the cluster to dir.
  1198. */
  1199. cluster = fat_add_new_entries(dir, slots, nr_slots, &nr_cluster,
  1200. &de, &bh, &i_pos);
  1201. if (cluster < 0) {
  1202. err = cluster;
  1203. goto error_remove;
  1204. }
  1205. err = fat_chain_add(dir, cluster, nr_cluster);
  1206. if (err) {
  1207. fat_free_clusters(dir, cluster);
  1208. goto error_remove;
  1209. }
  1210. if (dir->i_size & (sbi->cluster_size - 1)) {
  1211. fat_fs_error(sb, "Odd directory size");
  1212. dir->i_size = (dir->i_size + sbi->cluster_size - 1)
  1213. & ~((loff_t)sbi->cluster_size - 1);
  1214. }
  1215. dir->i_size += nr_cluster << sbi->cluster_bits;
  1216. MSDOS_I(dir)->mmu_private += nr_cluster << sbi->cluster_bits;
  1217. }
  1218. sinfo->slot_off = pos;
  1219. sinfo->de = de;
  1220. sinfo->bh = bh;
  1221. sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
  1222. return 0;
  1223. error:
  1224. brelse(bh);
  1225. for (i = 0; i < nr_bhs; i++)
  1226. brelse(bhs[i]);
  1227. return err;
  1228. error_remove:
  1229. brelse(bh);
  1230. if (free_slots)
  1231. __fat_remove_entries(dir, pos, free_slots);
  1232. return err;
  1233. }
  1234. EXPORT_SYMBOL_GPL(fat_add_entries);