dir.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  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, wchar_t *uni, int len,
  113. int uni_xlate, 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 && ((len - NLS_MAX_CHARSET_SIZE) > 0)) {
  122. ec = *ip++;
  123. if ( (charlen = nls->uni2char(ec, op, NLS_MAX_CHARSET_SIZE)) > 0) {
  124. op += charlen;
  125. len -= charlen;
  126. } else {
  127. if (uni_xlate == 1) {
  128. *op = ':';
  129. for (k = 4; k > 0; k--) {
  130. nc = ec & 0xF;
  131. op[k] = nc > 9 ? nc + ('a' - 10)
  132. : nc + '0';
  133. ec >>= 4;
  134. }
  135. op += 5;
  136. len -= 5;
  137. } else {
  138. *op++ = '?';
  139. len--;
  140. }
  141. }
  142. }
  143. if (unlikely(*ip)) {
  144. printk(KERN_WARNING "FAT: filename was truncated while "
  145. "converting.");
  146. }
  147. *op = 0;
  148. return (op - ascii);
  149. }
  150. static inline int
  151. fat_short2uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
  152. {
  153. int charlen;
  154. charlen = t->char2uni(c, clen, uni);
  155. if (charlen < 0) {
  156. *uni = 0x003f; /* a question mark */
  157. charlen = 1;
  158. }
  159. return charlen;
  160. }
  161. static inline int
  162. fat_short2lower_uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
  163. {
  164. int charlen;
  165. wchar_t wc;
  166. charlen = t->char2uni(c, clen, &wc);
  167. if (charlen < 0) {
  168. *uni = 0x003f; /* a question mark */
  169. charlen = 1;
  170. } else if (charlen <= 1) {
  171. unsigned char nc = t->charset2lower[*c];
  172. if (!nc)
  173. nc = *c;
  174. if ( (charlen = t->char2uni(&nc, 1, uni)) < 0) {
  175. *uni = 0x003f; /* a question mark */
  176. charlen = 1;
  177. }
  178. } else
  179. *uni = wc;
  180. return charlen;
  181. }
  182. static inline int
  183. fat_shortname2uni(struct nls_table *nls, unsigned char *buf, int buf_size,
  184. wchar_t *uni_buf, unsigned short opt, int lower)
  185. {
  186. int len = 0;
  187. if (opt & VFAT_SFN_DISPLAY_LOWER)
  188. len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
  189. else if (opt & VFAT_SFN_DISPLAY_WIN95)
  190. len = fat_short2uni(nls, buf, buf_size, uni_buf);
  191. else if (opt & VFAT_SFN_DISPLAY_WINNT) {
  192. if (lower)
  193. len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
  194. else
  195. len = fat_short2uni(nls, buf, buf_size, uni_buf);
  196. } else
  197. len = fat_short2uni(nls, buf, buf_size, uni_buf);
  198. return len;
  199. }
  200. enum { PARSE_INVALID = 1, PARSE_NOT_LONGNAME, PARSE_EOF, };
  201. /**
  202. * fat_parse_long - Parse extended directory entry.
  203. *
  204. * This function returns zero on success, negative value on error, or one of
  205. * the following:
  206. *
  207. * %PARSE_INVALID - Directory entry is invalid.
  208. * %PARSE_NOT_LONGNAME - Directory entry does not contain longname.
  209. * %PARSE_EOF - Directory has no more entries.
  210. */
  211. static int fat_parse_long(struct inode *dir, loff_t *pos,
  212. struct buffer_head **bh, struct msdos_dir_entry **de,
  213. wchar_t **unicode, unsigned char *nr_slots)
  214. {
  215. struct msdos_dir_slot *ds;
  216. unsigned char id, slot, slots, alias_checksum;
  217. if (!*unicode) {
  218. *unicode = __getname();
  219. if (!*unicode) {
  220. brelse(*bh);
  221. return -ENOMEM;
  222. }
  223. }
  224. parse_long:
  225. slots = 0;
  226. ds = (struct msdos_dir_slot *)*de;
  227. id = ds->id;
  228. if (!(id & 0x40))
  229. return PARSE_INVALID;
  230. slots = id & ~0x40;
  231. if (slots > 20 || !slots) /* ceil(256 * 2 / 26) */
  232. return PARSE_INVALID;
  233. *nr_slots = slots;
  234. alias_checksum = ds->alias_checksum;
  235. slot = slots;
  236. while (1) {
  237. int offset;
  238. slot--;
  239. offset = slot * 13;
  240. fat16_towchar(*unicode + offset, ds->name0_4, 5);
  241. fat16_towchar(*unicode + offset + 5, ds->name5_10, 6);
  242. fat16_towchar(*unicode + offset + 11, ds->name11_12, 2);
  243. if (ds->id & 0x40)
  244. (*unicode)[offset + 13] = 0;
  245. if (fat_get_entry(dir, pos, bh, de) < 0)
  246. return PARSE_EOF;
  247. if (slot == 0)
  248. break;
  249. ds = (struct msdos_dir_slot *)*de;
  250. if (ds->attr != ATTR_EXT)
  251. return PARSE_NOT_LONGNAME;
  252. if ((ds->id & ~0x40) != slot)
  253. goto parse_long;
  254. if (ds->alias_checksum != alias_checksum)
  255. goto parse_long;
  256. }
  257. if ((*de)->name[0] == DELETED_FLAG)
  258. return PARSE_INVALID;
  259. if ((*de)->attr == ATTR_EXT)
  260. goto parse_long;
  261. if (IS_FREE((*de)->name) || ((*de)->attr & ATTR_VOLUME))
  262. return PARSE_INVALID;
  263. if (fat_checksum((*de)->name) != alias_checksum)
  264. *nr_slots = 0;
  265. return 0;
  266. }
  267. /*
  268. * Return values: negative -> error, 0 -> not found, positive -> found,
  269. * value is the total amount of slots, including the shortname entry.
  270. */
  271. int fat_search_long(struct inode *inode, const unsigned char *name,
  272. int name_len, struct fat_slot_info *sinfo)
  273. {
  274. struct super_block *sb = inode->i_sb;
  275. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  276. struct buffer_head *bh = NULL;
  277. struct msdos_dir_entry *de;
  278. struct nls_table *nls_io = sbi->nls_io;
  279. struct nls_table *nls_disk = sbi->nls_disk;
  280. wchar_t bufuname[14];
  281. unsigned char nr_slots;
  282. int xlate_len;
  283. wchar_t *unicode = NULL;
  284. unsigned char work[MSDOS_NAME];
  285. unsigned char *bufname = NULL;
  286. int uni_xlate = sbi->options.unicode_xlate;
  287. int utf8 = sbi->options.utf8;
  288. int anycase = (sbi->options.name_check != 's');
  289. unsigned short opt_shortname = sbi->options.shortname;
  290. loff_t cpos = 0;
  291. int chl, i, j, last_u, err;
  292. bufname = __getname();
  293. if (!bufname)
  294. return -ENOMEM;
  295. err = -ENOENT;
  296. while(1) {
  297. if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
  298. goto EODir;
  299. parse_record:
  300. nr_slots = 0;
  301. if (de->name[0] == DELETED_FLAG)
  302. continue;
  303. if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
  304. continue;
  305. if (de->attr != ATTR_EXT && IS_FREE(de->name))
  306. continue;
  307. if (de->attr == ATTR_EXT) {
  308. int status = fat_parse_long(inode, &cpos, &bh, &de,
  309. &unicode, &nr_slots);
  310. if (status < 0)
  311. return status;
  312. else if (status == PARSE_INVALID)
  313. continue;
  314. else if (status == PARSE_NOT_LONGNAME)
  315. goto parse_record;
  316. else if (status == PARSE_EOF)
  317. goto EODir;
  318. }
  319. memcpy(work, de->name, sizeof(de->name));
  320. /* see namei.c, msdos_format_name */
  321. if (work[0] == 0x05)
  322. work[0] = 0xE5;
  323. for (i = 0, j = 0, last_u = 0; i < 8;) {
  324. if (!work[i])
  325. break;
  326. chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
  327. &bufuname[j++], opt_shortname,
  328. de->lcase & CASE_LOWER_BASE);
  329. if (chl <= 1) {
  330. if (work[i] != ' ')
  331. last_u = j;
  332. } else {
  333. last_u = j;
  334. }
  335. i += chl;
  336. }
  337. j = last_u;
  338. fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
  339. for (i = 8; i < MSDOS_NAME;) {
  340. if (!work[i])
  341. break;
  342. chl = fat_shortname2uni(nls_disk, &work[i],
  343. MSDOS_NAME - i,
  344. &bufuname[j++], opt_shortname,
  345. de->lcase & CASE_LOWER_EXT);
  346. if (chl <= 1) {
  347. if (work[i] != ' ')
  348. last_u = j;
  349. } else {
  350. last_u = j;
  351. }
  352. i += chl;
  353. }
  354. if (!last_u)
  355. continue;
  356. bufuname[last_u] = 0x0000;
  357. xlate_len = utf8
  358. ?utf8_wcstombs(bufname, bufuname, PATH_MAX)
  359. :uni16_to_x8(bufname, bufuname, PATH_MAX, uni_xlate, nls_io);
  360. if (xlate_len == name_len)
  361. if ((!anycase && !memcmp(name, bufname, xlate_len)) ||
  362. (anycase && !nls_strnicmp(nls_io, name, bufname,
  363. xlate_len)))
  364. goto Found;
  365. if (nr_slots) {
  366. xlate_len = utf8
  367. ?utf8_wcstombs(bufname, unicode, PATH_MAX)
  368. :uni16_to_x8(bufname, unicode, PATH_MAX, uni_xlate, nls_io);
  369. if (xlate_len != name_len)
  370. continue;
  371. if ((!anycase && !memcmp(name, bufname, xlate_len)) ||
  372. (anycase && !nls_strnicmp(nls_io, name, bufname,
  373. xlate_len)))
  374. goto Found;
  375. }
  376. }
  377. Found:
  378. nr_slots++; /* include the de */
  379. sinfo->slot_off = cpos - nr_slots * sizeof(*de);
  380. sinfo->nr_slots = nr_slots;
  381. sinfo->de = de;
  382. sinfo->bh = bh;
  383. sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
  384. err = 0;
  385. EODir:
  386. if (bufname)
  387. __putname(bufname);
  388. if (unicode)
  389. __putname(unicode);
  390. return err;
  391. }
  392. EXPORT_SYMBOL_GPL(fat_search_long);
  393. struct fat_ioctl_filldir_callback {
  394. void __user *dirent;
  395. int result;
  396. /* for dir ioctl */
  397. const char *longname;
  398. int long_len;
  399. const char *shortname;
  400. int short_len;
  401. };
  402. static int __fat_readdir(struct inode *inode, struct file *filp, void *dirent,
  403. filldir_t filldir, int short_only, int both)
  404. {
  405. struct super_block *sb = inode->i_sb;
  406. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  407. struct buffer_head *bh;
  408. struct msdos_dir_entry *de;
  409. struct nls_table *nls_io = sbi->nls_io;
  410. struct nls_table *nls_disk = sbi->nls_disk;
  411. unsigned char long_slots;
  412. const char *fill_name;
  413. int fill_len;
  414. wchar_t bufuname[14];
  415. wchar_t *unicode = NULL;
  416. unsigned char c, work[MSDOS_NAME], bufname[56], *ptname = bufname;
  417. unsigned long lpos, dummy, *furrfu = &lpos;
  418. int uni_xlate = sbi->options.unicode_xlate;
  419. int isvfat = sbi->options.isvfat;
  420. int utf8 = sbi->options.utf8;
  421. int nocase = sbi->options.nocase;
  422. unsigned short opt_shortname = sbi->options.shortname;
  423. unsigned long inum;
  424. int chi, chl, i, i2, j, last, last_u, dotoffset = 0;
  425. loff_t cpos;
  426. int ret = 0;
  427. lock_super(sb);
  428. cpos = filp->f_pos;
  429. /* Fake . and .. for the root directory. */
  430. if (inode->i_ino == MSDOS_ROOT_INO) {
  431. while (cpos < 2) {
  432. if (filldir(dirent, "..", cpos+1, cpos, MSDOS_ROOT_INO, DT_DIR) < 0)
  433. goto out;
  434. cpos++;
  435. filp->f_pos++;
  436. }
  437. if (cpos == 2) {
  438. dummy = 2;
  439. furrfu = &dummy;
  440. cpos = 0;
  441. }
  442. }
  443. if (cpos & (sizeof(struct msdos_dir_entry)-1)) {
  444. ret = -ENOENT;
  445. goto out;
  446. }
  447. bh = NULL;
  448. GetNew:
  449. if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
  450. goto EODir;
  451. parse_record:
  452. long_slots = 0;
  453. /* Check for long filename entry */
  454. if (isvfat) {
  455. if (de->name[0] == DELETED_FLAG)
  456. goto RecEnd;
  457. if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
  458. goto RecEnd;
  459. if (de->attr != ATTR_EXT && IS_FREE(de->name))
  460. goto RecEnd;
  461. } else {
  462. if ((de->attr & ATTR_VOLUME) || IS_FREE(de->name))
  463. goto RecEnd;
  464. }
  465. if (isvfat && de->attr == ATTR_EXT) {
  466. int status = fat_parse_long(inode, &cpos, &bh, &de,
  467. &unicode, &long_slots);
  468. if (status < 0) {
  469. filp->f_pos = cpos;
  470. ret = status;
  471. goto out;
  472. } else if (status == PARSE_INVALID)
  473. goto RecEnd;
  474. else if (status == PARSE_NOT_LONGNAME)
  475. goto parse_record;
  476. else if (status == PARSE_EOF)
  477. goto EODir;
  478. }
  479. if (sbi->options.dotsOK) {
  480. ptname = bufname;
  481. dotoffset = 0;
  482. if (de->attr & ATTR_HIDDEN) {
  483. *ptname++ = '.';
  484. dotoffset = 1;
  485. }
  486. }
  487. memcpy(work, de->name, sizeof(de->name));
  488. /* see namei.c, msdos_format_name */
  489. if (work[0] == 0x05)
  490. work[0] = 0xE5;
  491. for (i = 0, j = 0, last = 0, last_u = 0; i < 8;) {
  492. if (!(c = work[i]))
  493. break;
  494. chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
  495. &bufuname[j++], opt_shortname,
  496. de->lcase & CASE_LOWER_BASE);
  497. if (chl <= 1) {
  498. ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
  499. if (c != ' ') {
  500. last = i;
  501. last_u = j;
  502. }
  503. } else {
  504. last_u = j;
  505. for (chi = 0; chi < chl && i < 8; chi++) {
  506. ptname[i] = work[i];
  507. i++; last = i;
  508. }
  509. }
  510. }
  511. i = last;
  512. j = last_u;
  513. fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
  514. ptname[i++] = '.';
  515. for (i2 = 8; i2 < MSDOS_NAME;) {
  516. if (!(c = work[i2]))
  517. break;
  518. chl = fat_shortname2uni(nls_disk, &work[i2], MSDOS_NAME - i2,
  519. &bufuname[j++], opt_shortname,
  520. de->lcase & CASE_LOWER_EXT);
  521. if (chl <= 1) {
  522. i2++;
  523. ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
  524. if (c != ' ') {
  525. last = i;
  526. last_u = j;
  527. }
  528. } else {
  529. last_u = j;
  530. for (chi = 0; chi < chl && i2 < MSDOS_NAME; chi++) {
  531. ptname[i++] = work[i2++];
  532. last = i;
  533. }
  534. }
  535. }
  536. if (!last)
  537. goto RecEnd;
  538. i = last + dotoffset;
  539. j = last_u;
  540. lpos = cpos - (long_slots+1)*sizeof(struct msdos_dir_entry);
  541. if (!memcmp(de->name, MSDOS_DOT, MSDOS_NAME))
  542. inum = inode->i_ino;
  543. else if (!memcmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
  544. inum = parent_ino(filp->f_path.dentry);
  545. } else {
  546. loff_t i_pos = fat_make_i_pos(sb, bh, de);
  547. struct inode *tmp = fat_iget(sb, i_pos);
  548. if (tmp) {
  549. inum = tmp->i_ino;
  550. iput(tmp);
  551. } else
  552. inum = iunique(sb, MSDOS_ROOT_INO);
  553. }
  554. if (isvfat) {
  555. bufuname[j] = 0x0000;
  556. i = utf8 ? utf8_wcstombs(bufname, bufuname, sizeof(bufname))
  557. : uni16_to_x8(bufname, bufuname, sizeof(bufname), uni_xlate, nls_io);
  558. }
  559. fill_name = bufname;
  560. fill_len = i;
  561. if (!short_only && long_slots) {
  562. /* convert the unicode long name. 261 is maximum size
  563. * of unicode buffer. (13 * slots + nul) */
  564. void *longname = unicode + 261;
  565. int buf_size = PATH_MAX - (261 * sizeof(unicode[0]));
  566. int long_len = utf8
  567. ? utf8_wcstombs(longname, unicode, buf_size)
  568. : uni16_to_x8(longname, unicode, buf_size, uni_xlate, nls_io);
  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 FillFailed;
  586. RecEnd:
  587. furrfu = &lpos;
  588. filp->f_pos = cpos;
  589. goto GetNew;
  590. EODir:
  591. filp->f_pos = cpos;
  592. FillFailed:
  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);