dir.c 33 KB

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