dir.c 31 KB

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