dir.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  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_getblk(sb, phys);
  44. if (bh && !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. int offset;
  66. int err;
  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);
  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. /*
  199. * Return values: negative -> error, 0 -> not found, positive -> found,
  200. * value is the total amount of slots, including the shortname entry.
  201. */
  202. int fat_search_long(struct inode *inode, const unsigned char *name,
  203. int name_len, struct fat_slot_info *sinfo)
  204. {
  205. struct super_block *sb = inode->i_sb;
  206. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  207. struct buffer_head *bh = NULL;
  208. struct msdos_dir_entry *de;
  209. struct nls_table *nls_io = sbi->nls_io;
  210. struct nls_table *nls_disk = sbi->nls_disk;
  211. wchar_t bufuname[14];
  212. unsigned char xlate_len, nr_slots;
  213. wchar_t *unicode = NULL;
  214. unsigned char work[8], bufname[260]; /* 256 + 4 */
  215. int uni_xlate = sbi->options.unicode_xlate;
  216. int utf8 = sbi->options.utf8;
  217. int anycase = (sbi->options.name_check != 's');
  218. unsigned short opt_shortname = sbi->options.shortname;
  219. loff_t cpos = 0;
  220. int chl, i, j, last_u, err;
  221. err = -ENOENT;
  222. while(1) {
  223. if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
  224. goto EODir;
  225. parse_record:
  226. nr_slots = 0;
  227. if (de->name[0] == DELETED_FLAG)
  228. continue;
  229. if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
  230. continue;
  231. if (de->attr != ATTR_EXT && IS_FREE(de->name))
  232. continue;
  233. if (de->attr == ATTR_EXT) {
  234. struct msdos_dir_slot *ds;
  235. unsigned char id;
  236. unsigned char slot;
  237. unsigned char slots;
  238. unsigned char sum;
  239. unsigned char alias_checksum;
  240. if (!unicode) {
  241. unicode = (wchar_t *)
  242. __get_free_page(GFP_KERNEL);
  243. if (!unicode) {
  244. brelse(bh);
  245. return -ENOMEM;
  246. }
  247. }
  248. parse_long:
  249. slots = 0;
  250. ds = (struct msdos_dir_slot *) de;
  251. id = ds->id;
  252. if (!(id & 0x40))
  253. continue;
  254. slots = id & ~0x40;
  255. if (slots > 20 || !slots) /* ceil(256 * 2 / 26) */
  256. continue;
  257. nr_slots = slots;
  258. alias_checksum = ds->alias_checksum;
  259. slot = slots;
  260. while (1) {
  261. int offset;
  262. slot--;
  263. offset = slot * 13;
  264. fat16_towchar(unicode + offset, ds->name0_4, 5);
  265. fat16_towchar(unicode + offset + 5, ds->name5_10, 6);
  266. fat16_towchar(unicode + offset + 11, ds->name11_12, 2);
  267. if (ds->id & 0x40) {
  268. unicode[offset + 13] = 0;
  269. }
  270. if (fat_get_entry(inode, &cpos, &bh, &de) < 0)
  271. goto EODir;
  272. if (slot == 0)
  273. break;
  274. ds = (struct msdos_dir_slot *) de;
  275. if (ds->attr != ATTR_EXT)
  276. goto parse_record;
  277. if ((ds->id & ~0x40) != slot)
  278. goto parse_long;
  279. if (ds->alias_checksum != alias_checksum)
  280. goto parse_long;
  281. }
  282. if (de->name[0] == DELETED_FLAG)
  283. continue;
  284. if (de->attr == ATTR_EXT)
  285. goto parse_long;
  286. if (IS_FREE(de->name) || (de->attr & ATTR_VOLUME))
  287. continue;
  288. for (sum = 0, i = 0; i < 11; i++)
  289. sum = (((sum&1)<<7)|((sum&0xfe)>>1)) + de->name[i];
  290. if (sum != alias_checksum)
  291. nr_slots = 0;
  292. }
  293. memcpy(work, de->name, sizeof(de->name));
  294. /* see namei.c, msdos_format_name */
  295. if (work[0] == 0x05)
  296. work[0] = 0xE5;
  297. for (i = 0, j = 0, last_u = 0; i < 8;) {
  298. if (!work[i]) break;
  299. chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
  300. &bufuname[j++], opt_shortname,
  301. de->lcase & CASE_LOWER_BASE);
  302. if (chl <= 1) {
  303. if (work[i] != ' ')
  304. last_u = j;
  305. } else {
  306. last_u = j;
  307. }
  308. i += chl;
  309. }
  310. j = last_u;
  311. fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
  312. for (i = 0; i < 3;) {
  313. if (!de->ext[i]) break;
  314. chl = fat_shortname2uni(nls_disk, &de->ext[i], 3 - i,
  315. &bufuname[j++], opt_shortname,
  316. de->lcase & CASE_LOWER_EXT);
  317. if (chl <= 1) {
  318. if (de->ext[i] != ' ')
  319. last_u = j;
  320. } else {
  321. last_u = j;
  322. }
  323. i += chl;
  324. }
  325. if (!last_u)
  326. continue;
  327. bufuname[last_u] = 0x0000;
  328. xlate_len = utf8
  329. ?utf8_wcstombs(bufname, bufuname, sizeof(bufname))
  330. :uni16_to_x8(bufname, bufuname, uni_xlate, nls_io);
  331. if (xlate_len == name_len)
  332. if ((!anycase && !memcmp(name, bufname, xlate_len)) ||
  333. (anycase && !nls_strnicmp(nls_io, name, bufname,
  334. xlate_len)))
  335. goto Found;
  336. if (nr_slots) {
  337. xlate_len = utf8
  338. ?utf8_wcstombs(bufname, unicode, sizeof(bufname))
  339. :uni16_to_x8(bufname, unicode, uni_xlate, nls_io);
  340. if (xlate_len != name_len)
  341. continue;
  342. if ((!anycase && !memcmp(name, bufname, xlate_len)) ||
  343. (anycase && !nls_strnicmp(nls_io, name, bufname,
  344. xlate_len)))
  345. goto Found;
  346. }
  347. }
  348. Found:
  349. nr_slots++; /* include the de */
  350. sinfo->slot_off = cpos - nr_slots * sizeof(*de);
  351. sinfo->nr_slots = nr_slots;
  352. sinfo->de = de;
  353. sinfo->bh = bh;
  354. sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
  355. err = 0;
  356. EODir:
  357. if (unicode)
  358. free_page((unsigned long)unicode);
  359. return err;
  360. }
  361. EXPORT_SYMBOL(fat_search_long);
  362. struct fat_ioctl_filldir_callback {
  363. struct dirent __user *dirent;
  364. int result;
  365. /* for dir ioctl */
  366. const char *longname;
  367. int long_len;
  368. const char *shortname;
  369. int short_len;
  370. };
  371. static int fat_readdirx(struct inode *inode, struct file *filp, void *dirent,
  372. filldir_t filldir, int short_only, int both)
  373. {
  374. struct super_block *sb = inode->i_sb;
  375. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  376. struct buffer_head *bh;
  377. struct msdos_dir_entry *de;
  378. struct nls_table *nls_io = sbi->nls_io;
  379. struct nls_table *nls_disk = sbi->nls_disk;
  380. unsigned char long_slots;
  381. const char *fill_name;
  382. int fill_len;
  383. wchar_t bufuname[14];
  384. wchar_t *unicode = NULL;
  385. unsigned char c, work[8], bufname[56], *ptname = bufname;
  386. unsigned long lpos, dummy, *furrfu = &lpos;
  387. int uni_xlate = sbi->options.unicode_xlate;
  388. int isvfat = sbi->options.isvfat;
  389. int utf8 = sbi->options.utf8;
  390. int nocase = sbi->options.nocase;
  391. unsigned short opt_shortname = sbi->options.shortname;
  392. unsigned long inum;
  393. int chi, chl, i, i2, j, last, last_u, dotoffset = 0;
  394. loff_t cpos;
  395. int ret = 0;
  396. lock_kernel();
  397. cpos = filp->f_pos;
  398. /* Fake . and .. for the root directory. */
  399. if (inode->i_ino == MSDOS_ROOT_INO) {
  400. while (cpos < 2) {
  401. if (filldir(dirent, "..", cpos+1, cpos, MSDOS_ROOT_INO, DT_DIR) < 0)
  402. goto out;
  403. cpos++;
  404. filp->f_pos++;
  405. }
  406. if (cpos == 2) {
  407. dummy = 2;
  408. furrfu = &dummy;
  409. cpos = 0;
  410. }
  411. }
  412. if (cpos & (sizeof(struct msdos_dir_entry)-1)) {
  413. ret = -ENOENT;
  414. goto out;
  415. }
  416. bh = NULL;
  417. GetNew:
  418. long_slots = 0;
  419. if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
  420. goto EODir;
  421. /* Check for long filename entry */
  422. if (isvfat) {
  423. if (de->name[0] == DELETED_FLAG)
  424. goto RecEnd;
  425. if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
  426. goto RecEnd;
  427. if (de->attr != ATTR_EXT && IS_FREE(de->name))
  428. goto RecEnd;
  429. } else {
  430. if ((de->attr & ATTR_VOLUME) || IS_FREE(de->name))
  431. goto RecEnd;
  432. }
  433. if (isvfat && de->attr == ATTR_EXT) {
  434. struct msdos_dir_slot *ds;
  435. unsigned char id;
  436. unsigned char slot;
  437. unsigned char slots;
  438. unsigned char sum;
  439. unsigned char alias_checksum;
  440. if (!unicode) {
  441. unicode = (wchar_t *)__get_free_page(GFP_KERNEL);
  442. if (!unicode) {
  443. filp->f_pos = cpos;
  444. brelse(bh);
  445. ret = -ENOMEM;
  446. goto out;
  447. }
  448. }
  449. ParseLong:
  450. slots = 0;
  451. ds = (struct msdos_dir_slot *) de;
  452. id = ds->id;
  453. if (!(id & 0x40))
  454. goto RecEnd;
  455. slots = id & ~0x40;
  456. if (slots > 20 || !slots) /* ceil(256 * 2 / 26) */
  457. goto RecEnd;
  458. long_slots = slots;
  459. alias_checksum = ds->alias_checksum;
  460. slot = slots;
  461. while (1) {
  462. int offset;
  463. slot--;
  464. offset = slot * 13;
  465. fat16_towchar(unicode + offset, ds->name0_4, 5);
  466. fat16_towchar(unicode + offset + 5, ds->name5_10, 6);
  467. fat16_towchar(unicode + offset + 11, ds->name11_12, 2);
  468. if (ds->id & 0x40) {
  469. unicode[offset + 13] = 0;
  470. }
  471. if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
  472. goto EODir;
  473. if (slot == 0)
  474. break;
  475. ds = (struct msdos_dir_slot *) de;
  476. if (ds->attr != ATTR_EXT)
  477. goto RecEnd; /* XXX */
  478. if ((ds->id & ~0x40) != slot)
  479. goto ParseLong;
  480. if (ds->alias_checksum != alias_checksum)
  481. goto ParseLong;
  482. }
  483. if (de->name[0] == DELETED_FLAG)
  484. goto RecEnd;
  485. if (de->attr == ATTR_EXT)
  486. goto ParseLong;
  487. if (IS_FREE(de->name) || (de->attr & ATTR_VOLUME))
  488. goto RecEnd;
  489. for (sum = 0, i = 0; i < 11; i++)
  490. sum = (((sum&1)<<7)|((sum&0xfe)>>1)) + de->name[i];
  491. if (sum != alias_checksum)
  492. long_slots = 0;
  493. }
  494. if (sbi->options.dotsOK) {
  495. ptname = bufname;
  496. dotoffset = 0;
  497. if (de->attr & ATTR_HIDDEN) {
  498. *ptname++ = '.';
  499. dotoffset = 1;
  500. }
  501. }
  502. memcpy(work, de->name, sizeof(de->name));
  503. /* see namei.c, msdos_format_name */
  504. if (work[0] == 0x05)
  505. work[0] = 0xE5;
  506. for (i = 0, j = 0, last = 0, last_u = 0; i < 8;) {
  507. if (!(c = work[i])) break;
  508. chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
  509. &bufuname[j++], opt_shortname,
  510. de->lcase & CASE_LOWER_BASE);
  511. if (chl <= 1) {
  512. ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
  513. if (c != ' ') {
  514. last = i;
  515. last_u = j;
  516. }
  517. } else {
  518. last_u = j;
  519. for (chi = 0; chi < chl && i < 8; chi++) {
  520. ptname[i] = work[i];
  521. i++; last = i;
  522. }
  523. }
  524. }
  525. i = last;
  526. j = last_u;
  527. fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
  528. ptname[i++] = '.';
  529. for (i2 = 0; i2 < 3;) {
  530. if (!(c = de->ext[i2])) break;
  531. chl = fat_shortname2uni(nls_disk, &de->ext[i2], 3 - i2,
  532. &bufuname[j++], opt_shortname,
  533. de->lcase & CASE_LOWER_EXT);
  534. if (chl <= 1) {
  535. i2++;
  536. ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
  537. if (c != ' ') {
  538. last = i;
  539. last_u = j;
  540. }
  541. } else {
  542. last_u = j;
  543. for (chi = 0; chi < chl && i2 < 3; chi++) {
  544. ptname[i++] = de->ext[i2++];
  545. last = i;
  546. }
  547. }
  548. }
  549. if (!last)
  550. goto RecEnd;
  551. i = last + dotoffset;
  552. j = last_u;
  553. lpos = cpos - (long_slots+1)*sizeof(struct msdos_dir_entry);
  554. if (!memcmp(de->name, MSDOS_DOT, MSDOS_NAME))
  555. inum = inode->i_ino;
  556. else if (!memcmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
  557. inum = parent_ino(filp->f_dentry);
  558. } else {
  559. loff_t i_pos = fat_make_i_pos(sb, bh, de);
  560. struct inode *tmp = fat_iget(sb, i_pos);
  561. if (tmp) {
  562. inum = tmp->i_ino;
  563. iput(tmp);
  564. } else
  565. inum = iunique(sb, MSDOS_ROOT_INO);
  566. }
  567. if (isvfat) {
  568. bufuname[j] = 0x0000;
  569. i = utf8 ? utf8_wcstombs(bufname, bufuname, sizeof(bufname))
  570. : uni16_to_x8(bufname, bufuname, uni_xlate, nls_io);
  571. }
  572. fill_name = bufname;
  573. fill_len = i;
  574. if (!short_only && long_slots) {
  575. /* convert the unicode long name. 261 is maximum size
  576. * of unicode buffer. (13 * slots + nul) */
  577. void *longname = unicode + 261;
  578. int buf_size = PAGE_SIZE - (261 * sizeof(unicode[0]));
  579. int long_len = utf8
  580. ? utf8_wcstombs(longname, unicode, buf_size)
  581. : uni16_to_x8(longname, unicode, uni_xlate, nls_io);
  582. if (!both) {
  583. fill_name = longname;
  584. fill_len = long_len;
  585. } else {
  586. /* hack for fat_ioctl_filldir() */
  587. struct fat_ioctl_filldir_callback *p = dirent;
  588. p->longname = longname;
  589. p->long_len = long_len;
  590. p->shortname = bufname;
  591. p->short_len = i;
  592. fill_name = NULL;
  593. fill_len = 0;
  594. }
  595. }
  596. if (filldir(dirent, fill_name, fill_len, *furrfu, inum,
  597. (de->attr & ATTR_DIR) ? DT_DIR : DT_REG) < 0)
  598. goto FillFailed;
  599. RecEnd:
  600. furrfu = &lpos;
  601. filp->f_pos = cpos;
  602. goto GetNew;
  603. EODir:
  604. filp->f_pos = cpos;
  605. FillFailed:
  606. brelse(bh);
  607. if (unicode)
  608. free_page((unsigned long)unicode);
  609. out:
  610. unlock_kernel();
  611. return ret;
  612. }
  613. static int fat_readdir(struct file *filp, void *dirent, filldir_t filldir)
  614. {
  615. struct inode *inode = filp->f_dentry->d_inode;
  616. return fat_readdirx(inode, filp, dirent, filldir, 0, 0);
  617. }
  618. static int fat_ioctl_filldir(void *__buf, const char *name, int name_len,
  619. loff_t offset, ino_t ino, unsigned int d_type)
  620. {
  621. struct fat_ioctl_filldir_callback *buf = __buf;
  622. struct dirent __user *d1 = buf->dirent;
  623. struct dirent __user *d2 = d1 + 1;
  624. if (buf->result)
  625. return -EINVAL;
  626. buf->result++;
  627. if (name != NULL) {
  628. /* dirent has only short name */
  629. if (name_len >= sizeof(d1->d_name))
  630. name_len = sizeof(d1->d_name) - 1;
  631. if (put_user(0, d2->d_name) ||
  632. put_user(0, &d2->d_reclen) ||
  633. copy_to_user(d1->d_name, name, name_len) ||
  634. put_user(0, d1->d_name + name_len) ||
  635. put_user(name_len, &d1->d_reclen))
  636. goto efault;
  637. } else {
  638. /* dirent has short and long name */
  639. const char *longname = buf->longname;
  640. int long_len = buf->long_len;
  641. const char *shortname = buf->shortname;
  642. int short_len = buf->short_len;
  643. if (long_len >= sizeof(d1->d_name))
  644. long_len = sizeof(d1->d_name) - 1;
  645. if (short_len >= sizeof(d1->d_name))
  646. short_len = sizeof(d1->d_name) - 1;
  647. if (copy_to_user(d2->d_name, longname, long_len) ||
  648. put_user(0, d2->d_name + long_len) ||
  649. put_user(long_len, &d2->d_reclen) ||
  650. put_user(ino, &d2->d_ino) ||
  651. put_user(offset, &d2->d_off) ||
  652. copy_to_user(d1->d_name, shortname, short_len) ||
  653. put_user(0, d1->d_name + short_len) ||
  654. put_user(short_len, &d1->d_reclen))
  655. goto efault;
  656. }
  657. return 0;
  658. efault:
  659. buf->result = -EFAULT;
  660. return -EFAULT;
  661. }
  662. static int fat_dir_ioctl(struct inode * inode, struct file * filp,
  663. unsigned int cmd, unsigned long arg)
  664. {
  665. struct fat_ioctl_filldir_callback buf;
  666. struct dirent __user *d1;
  667. int ret, short_only, both;
  668. switch (cmd) {
  669. case VFAT_IOCTL_READDIR_SHORT:
  670. short_only = 1;
  671. both = 0;
  672. break;
  673. case VFAT_IOCTL_READDIR_BOTH:
  674. short_only = 0;
  675. both = 1;
  676. break;
  677. default:
  678. return fat_generic_ioctl(inode, filp, cmd, arg);
  679. }
  680. d1 = (struct dirent __user *)arg;
  681. if (!access_ok(VERIFY_WRITE, d1, sizeof(struct dirent[2])))
  682. return -EFAULT;
  683. /*
  684. * Yes, we don't need this put_user() absolutely. However old
  685. * code didn't return the right value. So, app use this value,
  686. * in order to check whether it is EOF.
  687. */
  688. if (put_user(0, &d1->d_reclen))
  689. return -EFAULT;
  690. buf.dirent = d1;
  691. buf.result = 0;
  692. down(&inode->i_sem);
  693. ret = -ENOENT;
  694. if (!IS_DEADDIR(inode)) {
  695. ret = fat_readdirx(inode, filp, &buf, fat_ioctl_filldir,
  696. short_only, both);
  697. }
  698. up(&inode->i_sem);
  699. if (ret >= 0)
  700. ret = buf.result;
  701. return ret;
  702. }
  703. struct file_operations fat_dir_operations = {
  704. .read = generic_read_dir,
  705. .readdir = fat_readdir,
  706. .ioctl = fat_dir_ioctl,
  707. .fsync = file_fsync,
  708. };
  709. static int fat_get_short_entry(struct inode *dir, loff_t *pos,
  710. struct buffer_head **bh,
  711. struct msdos_dir_entry **de)
  712. {
  713. while (fat_get_entry(dir, pos, bh, de) >= 0) {
  714. /* free entry or long name entry or volume label */
  715. if (!IS_FREE((*de)->name) && !((*de)->attr & ATTR_VOLUME))
  716. return 0;
  717. }
  718. return -ENOENT;
  719. }
  720. /*
  721. * The ".." entry can not provide the "struct fat_slot_info" informations
  722. * for inode. So, this function provide the some informations only.
  723. */
  724. int fat_get_dotdot_entry(struct inode *dir, struct buffer_head **bh,
  725. struct msdos_dir_entry **de, loff_t *i_pos)
  726. {
  727. loff_t offset;
  728. offset = 0;
  729. *bh = NULL;
  730. while (fat_get_short_entry(dir, &offset, bh, de) >= 0) {
  731. if (!strncmp((*de)->name, MSDOS_DOTDOT, MSDOS_NAME)) {
  732. *i_pos = fat_make_i_pos(dir->i_sb, *bh, *de);
  733. return 0;
  734. }
  735. }
  736. return -ENOENT;
  737. }
  738. EXPORT_SYMBOL(fat_get_dotdot_entry);
  739. /* See if directory is empty */
  740. int fat_dir_empty(struct inode *dir)
  741. {
  742. struct buffer_head *bh;
  743. struct msdos_dir_entry *de;
  744. loff_t cpos;
  745. int result = 0;
  746. bh = NULL;
  747. cpos = 0;
  748. while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
  749. if (strncmp(de->name, MSDOS_DOT , MSDOS_NAME) &&
  750. strncmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
  751. result = -ENOTEMPTY;
  752. break;
  753. }
  754. }
  755. brelse(bh);
  756. return result;
  757. }
  758. EXPORT_SYMBOL(fat_dir_empty);
  759. /*
  760. * fat_subdirs counts the number of sub-directories of dir. It can be run
  761. * on directories being created.
  762. */
  763. int fat_subdirs(struct inode *dir)
  764. {
  765. struct buffer_head *bh;
  766. struct msdos_dir_entry *de;
  767. loff_t cpos;
  768. int count = 0;
  769. bh = NULL;
  770. cpos = 0;
  771. while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
  772. if (de->attr & ATTR_DIR)
  773. count++;
  774. }
  775. brelse(bh);
  776. return count;
  777. }
  778. /*
  779. * Scans a directory for a given file (name points to its formatted name).
  780. * Returns an error code or zero.
  781. */
  782. int fat_scan(struct inode *dir, const unsigned char *name,
  783. struct fat_slot_info *sinfo)
  784. {
  785. struct super_block *sb = dir->i_sb;
  786. sinfo->slot_off = 0;
  787. sinfo->bh = NULL;
  788. while (fat_get_short_entry(dir, &sinfo->slot_off, &sinfo->bh,
  789. &sinfo->de) >= 0) {
  790. if (!strncmp(sinfo->de->name, name, MSDOS_NAME)) {
  791. sinfo->slot_off -= sizeof(*sinfo->de);
  792. sinfo->nr_slots = 1;
  793. sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
  794. return 0;
  795. }
  796. }
  797. return -ENOENT;
  798. }
  799. EXPORT_SYMBOL(fat_scan);
  800. static int __fat_remove_entries(struct inode *dir, loff_t pos, int nr_slots)
  801. {
  802. struct super_block *sb = dir->i_sb;
  803. struct buffer_head *bh;
  804. struct msdos_dir_entry *de, *endp;
  805. int err = 0, orig_slots;
  806. while (nr_slots) {
  807. bh = NULL;
  808. if (fat_get_entry(dir, &pos, &bh, &de) < 0) {
  809. err = -EIO;
  810. break;
  811. }
  812. orig_slots = nr_slots;
  813. endp = (struct msdos_dir_entry *)(bh->b_data + sb->s_blocksize);
  814. while (nr_slots && de < endp) {
  815. de->name[0] = DELETED_FLAG;
  816. de++;
  817. nr_slots--;
  818. }
  819. mark_buffer_dirty(bh);
  820. if (IS_DIRSYNC(dir))
  821. err = sync_dirty_buffer(bh);
  822. brelse(bh);
  823. if (err)
  824. break;
  825. /* pos is *next* de's position, so this does `- sizeof(de)' */
  826. pos += ((orig_slots - nr_slots) * sizeof(*de)) - sizeof(*de);
  827. }
  828. return err;
  829. }
  830. int fat_remove_entries(struct inode *dir, struct fat_slot_info *sinfo)
  831. {
  832. struct msdos_dir_entry *de;
  833. struct buffer_head *bh;
  834. int err = 0, nr_slots;
  835. /*
  836. * First stage: Remove the shortname. By this, the directory
  837. * entry is removed.
  838. */
  839. nr_slots = sinfo->nr_slots;
  840. de = sinfo->de;
  841. sinfo->de = NULL;
  842. bh = sinfo->bh;
  843. sinfo->bh = NULL;
  844. while (nr_slots && de >= (struct msdos_dir_entry *)bh->b_data) {
  845. de->name[0] = DELETED_FLAG;
  846. de--;
  847. nr_slots--;
  848. }
  849. mark_buffer_dirty(bh);
  850. if (IS_DIRSYNC(dir))
  851. err = sync_dirty_buffer(bh);
  852. brelse(bh);
  853. if (err)
  854. return err;
  855. dir->i_version++;
  856. if (nr_slots) {
  857. /*
  858. * Second stage: remove the remaining longname slots.
  859. * (This directory entry is already removed, and so return
  860. * the success)
  861. */
  862. err = __fat_remove_entries(dir, sinfo->slot_off, nr_slots);
  863. if (err) {
  864. printk(KERN_WARNING
  865. "FAT: Couldn't remove the long name slots\n");
  866. }
  867. }
  868. dir->i_mtime = dir->i_atime = CURRENT_TIME_SEC;
  869. if (IS_DIRSYNC(dir))
  870. (void)fat_sync_inode(dir);
  871. else
  872. mark_inode_dirty(dir);
  873. return 0;
  874. }
  875. EXPORT_SYMBOL(fat_remove_entries);
  876. static int fat_zeroed_cluster(struct inode *dir, sector_t blknr, int nr_used,
  877. struct buffer_head **bhs, int nr_bhs)
  878. {
  879. struct super_block *sb = dir->i_sb;
  880. sector_t last_blknr = blknr + MSDOS_SB(sb)->sec_per_clus;
  881. int err, i, n;
  882. /* Zeroing the unused blocks on this cluster */
  883. blknr += nr_used;
  884. n = nr_used;
  885. while (blknr < last_blknr) {
  886. bhs[n] = sb_getblk(sb, blknr);
  887. if (!bhs[n]) {
  888. err = -ENOMEM;
  889. goto error;
  890. }
  891. memset(bhs[n]->b_data, 0, sb->s_blocksize);
  892. set_buffer_uptodate(bhs[n]);
  893. mark_buffer_dirty(bhs[n]);
  894. n++;
  895. blknr++;
  896. if (n == nr_bhs) {
  897. if (IS_DIRSYNC(dir)) {
  898. err = fat_sync_bhs(bhs, n);
  899. if (err)
  900. goto error;
  901. }
  902. for (i = 0; i < n; i++)
  903. brelse(bhs[i]);
  904. n = 0;
  905. }
  906. }
  907. if (IS_DIRSYNC(dir)) {
  908. err = fat_sync_bhs(bhs, n);
  909. if (err)
  910. goto error;
  911. }
  912. for (i = 0; i < n; i++)
  913. brelse(bhs[i]);
  914. return 0;
  915. error:
  916. for (i = 0; i < n; i++)
  917. bforget(bhs[i]);
  918. return err;
  919. }
  920. int fat_alloc_new_dir(struct inode *dir, struct timespec *ts)
  921. {
  922. struct super_block *sb = dir->i_sb;
  923. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  924. struct buffer_head *bhs[MAX_BUF_PER_PAGE];
  925. struct msdos_dir_entry *de;
  926. sector_t blknr;
  927. __le16 date, time;
  928. int err, cluster;
  929. err = fat_alloc_clusters(dir, &cluster, 1);
  930. if (err)
  931. goto error;
  932. blknr = fat_clus_to_blknr(sbi, cluster);
  933. bhs[0] = sb_getblk(sb, blknr);
  934. if (!bhs[0]) {
  935. err = -ENOMEM;
  936. goto error_free;
  937. }
  938. fat_date_unix2dos(ts->tv_sec, &time, &date);
  939. de = (struct msdos_dir_entry *)bhs[0]->b_data;
  940. /* filling the new directory slots ("." and ".." entries) */
  941. memcpy(de[0].name, MSDOS_DOT, MSDOS_NAME);
  942. memcpy(de[1].name, MSDOS_DOTDOT, MSDOS_NAME);
  943. de->attr = de[1].attr = ATTR_DIR;
  944. de[0].lcase = de[1].lcase = 0;
  945. de[0].time = de[1].time = time;
  946. de[0].date = de[1].date = date;
  947. de[0].ctime_cs = de[1].ctime_cs = 0;
  948. if (sbi->options.isvfat) {
  949. /* extra timestamps */
  950. de[0].ctime = de[1].ctime = time;
  951. de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = date;
  952. } else {
  953. de[0].ctime = de[1].ctime = 0;
  954. de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = 0;
  955. }
  956. de[0].start = cpu_to_le16(cluster);
  957. de[0].starthi = cpu_to_le16(cluster >> 16);
  958. de[1].start = cpu_to_le16(MSDOS_I(dir)->i_logstart);
  959. de[1].starthi = cpu_to_le16(MSDOS_I(dir)->i_logstart >> 16);
  960. de[0].size = de[1].size = 0;
  961. memset(de + 2, 0, sb->s_blocksize - 2 * sizeof(*de));
  962. set_buffer_uptodate(bhs[0]);
  963. mark_buffer_dirty(bhs[0]);
  964. err = fat_zeroed_cluster(dir, blknr, 1, bhs, MAX_BUF_PER_PAGE);
  965. if (err)
  966. goto error_free;
  967. return cluster;
  968. error_free:
  969. fat_free_clusters(dir, cluster);
  970. error:
  971. return err;
  972. }
  973. EXPORT_SYMBOL(fat_alloc_new_dir);
  974. static int fat_add_new_entries(struct inode *dir, void *slots, int nr_slots,
  975. int *nr_cluster, struct msdos_dir_entry **de,
  976. struct buffer_head **bh, loff_t *i_pos)
  977. {
  978. struct super_block *sb = dir->i_sb;
  979. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  980. struct buffer_head *bhs[MAX_BUF_PER_PAGE];
  981. sector_t blknr, start_blknr, last_blknr;
  982. unsigned long size, copy;
  983. int err, i, n, offset, cluster[2];
  984. /*
  985. * The minimum cluster size is 512bytes, and maximum entry
  986. * size is 32*slots (672bytes). So, iff the cluster size is
  987. * 512bytes, we may need two clusters.
  988. */
  989. size = nr_slots * sizeof(struct msdos_dir_entry);
  990. *nr_cluster = (size + (sbi->cluster_size - 1)) >> sbi->cluster_bits;
  991. BUG_ON(*nr_cluster > 2);
  992. err = fat_alloc_clusters(dir, cluster, *nr_cluster);
  993. if (err)
  994. goto error;
  995. /*
  996. * First stage: Fill the directory entry. NOTE: This cluster
  997. * is not referenced from any inode yet, so updates order is
  998. * not important.
  999. */
  1000. i = n = copy = 0;
  1001. do {
  1002. start_blknr = blknr = fat_clus_to_blknr(sbi, cluster[i]);
  1003. last_blknr = start_blknr + sbi->sec_per_clus;
  1004. while (blknr < last_blknr) {
  1005. bhs[n] = sb_getblk(sb, blknr);
  1006. if (!bhs[n]) {
  1007. err = -ENOMEM;
  1008. goto error_nomem;
  1009. }
  1010. /* fill the directory entry */
  1011. copy = min(size, sb->s_blocksize);
  1012. memcpy(bhs[n]->b_data, slots, copy);
  1013. slots += copy;
  1014. size -= copy;
  1015. set_buffer_uptodate(bhs[n]);
  1016. mark_buffer_dirty(bhs[n]);
  1017. if (!size)
  1018. break;
  1019. n++;
  1020. blknr++;
  1021. }
  1022. } while (++i < *nr_cluster);
  1023. memset(bhs[n]->b_data + copy, 0, sb->s_blocksize - copy);
  1024. offset = copy - sizeof(struct msdos_dir_entry);
  1025. get_bh(bhs[n]);
  1026. *bh = bhs[n];
  1027. *de = (struct msdos_dir_entry *)((*bh)->b_data + offset);
  1028. *i_pos = fat_make_i_pos(sb, *bh, *de);
  1029. /* Second stage: clear the rest of cluster, and write outs */
  1030. err = fat_zeroed_cluster(dir, start_blknr, ++n, bhs, MAX_BUF_PER_PAGE);
  1031. if (err)
  1032. goto error_free;
  1033. return cluster[0];
  1034. error_free:
  1035. brelse(*bh);
  1036. *bh = NULL;
  1037. n = 0;
  1038. error_nomem:
  1039. for (i = 0; i < n; i++)
  1040. bforget(bhs[i]);
  1041. fat_free_clusters(dir, cluster[0]);
  1042. error:
  1043. return err;
  1044. }
  1045. int fat_add_entries(struct inode *dir, void *slots, int nr_slots,
  1046. struct fat_slot_info *sinfo)
  1047. {
  1048. struct super_block *sb = dir->i_sb;
  1049. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  1050. struct buffer_head *bh, *prev, *bhs[3]; /* 32*slots (672bytes) */
  1051. struct msdos_dir_entry *de;
  1052. int err, free_slots, i, nr_bhs;
  1053. loff_t pos, i_pos;
  1054. sinfo->nr_slots = nr_slots;
  1055. /* First stage: search free direcotry entries */
  1056. free_slots = nr_bhs = 0;
  1057. bh = prev = NULL;
  1058. pos = 0;
  1059. err = -ENOSPC;
  1060. while (fat_get_entry(dir, &pos, &bh, &de) > -1) {
  1061. /* check the maximum size of directory */
  1062. if (pos >= FAT_MAX_DIR_SIZE)
  1063. goto error;
  1064. if (IS_FREE(de->name)) {
  1065. if (prev != bh) {
  1066. get_bh(bh);
  1067. bhs[nr_bhs] = prev = bh;
  1068. nr_bhs++;
  1069. }
  1070. free_slots++;
  1071. if (free_slots == nr_slots)
  1072. goto found;
  1073. } else {
  1074. for (i = 0; i < nr_bhs; i++)
  1075. brelse(bhs[i]);
  1076. prev = NULL;
  1077. free_slots = nr_bhs = 0;
  1078. }
  1079. }
  1080. if (dir->i_ino == MSDOS_ROOT_INO) {
  1081. if (sbi->fat_bits != 32)
  1082. goto error;
  1083. } else if (MSDOS_I(dir)->i_start == 0) {
  1084. printk(KERN_ERR "FAT: Corrupted directory (i_pos %lld)\n",
  1085. MSDOS_I(dir)->i_pos);
  1086. err = -EIO;
  1087. goto error;
  1088. }
  1089. found:
  1090. err = 0;
  1091. pos -= free_slots * sizeof(*de);
  1092. nr_slots -= free_slots;
  1093. if (free_slots) {
  1094. /*
  1095. * Second stage: filling the free entries with new entries.
  1096. * NOTE: If this slots has shortname, first, we write
  1097. * the long name slots, then write the short name.
  1098. */
  1099. int size = free_slots * sizeof(*de);
  1100. int offset = pos & (sb->s_blocksize - 1);
  1101. int long_bhs = nr_bhs - (nr_slots == 0);
  1102. /* Fill the long name slots. */
  1103. for (i = 0; i < long_bhs; i++) {
  1104. int copy = min_t(int, sb->s_blocksize - offset, size);
  1105. memcpy(bhs[i]->b_data + offset, slots, copy);
  1106. mark_buffer_dirty(bhs[i]);
  1107. offset = 0;
  1108. slots += copy;
  1109. size -= copy;
  1110. }
  1111. if (long_bhs && IS_DIRSYNC(dir))
  1112. err = fat_sync_bhs(bhs, long_bhs);
  1113. if (!err && i < nr_bhs) {
  1114. /* Fill the short name slot. */
  1115. int copy = min_t(int, sb->s_blocksize - offset, size);
  1116. memcpy(bhs[i]->b_data + offset, slots, copy);
  1117. mark_buffer_dirty(bhs[i]);
  1118. if (IS_DIRSYNC(dir))
  1119. err = sync_dirty_buffer(bhs[i]);
  1120. }
  1121. for (i = 0; i < nr_bhs; i++)
  1122. brelse(bhs[i]);
  1123. if (err)
  1124. goto error_remove;
  1125. }
  1126. if (nr_slots) {
  1127. int cluster, nr_cluster;
  1128. /*
  1129. * Third stage: allocate the cluster for new entries.
  1130. * And initialize the cluster with new entries, then
  1131. * add the cluster to dir.
  1132. */
  1133. cluster = fat_add_new_entries(dir, slots, nr_slots, &nr_cluster,
  1134. &de, &bh, &i_pos);
  1135. if (cluster < 0) {
  1136. err = cluster;
  1137. goto error_remove;
  1138. }
  1139. err = fat_chain_add(dir, cluster, nr_cluster);
  1140. if (err) {
  1141. fat_free_clusters(dir, cluster);
  1142. goto error_remove;
  1143. }
  1144. if (dir->i_size & (sbi->cluster_size - 1)) {
  1145. fat_fs_panic(sb, "Odd directory size");
  1146. dir->i_size = (dir->i_size + sbi->cluster_size - 1)
  1147. & ~((loff_t)sbi->cluster_size - 1);
  1148. }
  1149. dir->i_size += nr_cluster << sbi->cluster_bits;
  1150. MSDOS_I(dir)->mmu_private += nr_cluster << sbi->cluster_bits;
  1151. }
  1152. sinfo->slot_off = pos;
  1153. sinfo->de = de;
  1154. sinfo->bh = bh;
  1155. sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
  1156. return 0;
  1157. error:
  1158. brelse(bh);
  1159. for (i = 0; i < nr_bhs; i++)
  1160. brelse(bhs[i]);
  1161. return err;
  1162. error_remove:
  1163. brelse(bh);
  1164. if (free_slots)
  1165. __fat_remove_entries(dir, pos, free_slots);
  1166. return err;
  1167. }
  1168. EXPORT_SYMBOL(fat_add_entries);