dir.c 34 KB

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