dir.c 33 KB

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