dir.c 32 KB

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