dir.c 33 KB

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