dir.c 31 KB

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