dir.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  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. void __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. #define FAT_IOCTL_FILLDIR_FUNC(func, dirent_type) \
  593. static int func(void *__buf, const char *name, int name_len, \
  594. loff_t offset, u64 ino, unsigned int d_type) \
  595. { \
  596. struct fat_ioctl_filldir_callback *buf = __buf; \
  597. struct dirent_type __user *d1 = buf->dirent; \
  598. struct dirent_type __user *d2 = d1 + 1; \
  599. \
  600. if (buf->result) \
  601. return -EINVAL; \
  602. buf->result++; \
  603. \
  604. if (name != NULL) { \
  605. /* dirent has only short name */ \
  606. if (name_len >= sizeof(d1->d_name)) \
  607. name_len = sizeof(d1->d_name) - 1; \
  608. \
  609. if (put_user(0, d2->d_name) || \
  610. put_user(0, &d2->d_reclen) || \
  611. copy_to_user(d1->d_name, name, name_len) || \
  612. put_user(0, d1->d_name + name_len) || \
  613. put_user(name_len, &d1->d_reclen)) \
  614. goto efault; \
  615. } else { \
  616. /* dirent has short and long name */ \
  617. const char *longname = buf->longname; \
  618. int long_len = buf->long_len; \
  619. const char *shortname = buf->shortname; \
  620. int short_len = buf->short_len; \
  621. \
  622. if (long_len >= sizeof(d1->d_name)) \
  623. long_len = sizeof(d1->d_name) - 1; \
  624. if (short_len >= sizeof(d1->d_name)) \
  625. short_len = sizeof(d1->d_name) - 1; \
  626. \
  627. if (copy_to_user(d2->d_name, longname, long_len) || \
  628. put_user(0, d2->d_name + long_len) || \
  629. put_user(long_len, &d2->d_reclen) || \
  630. put_user(ino, &d2->d_ino) || \
  631. put_user(offset, &d2->d_off) || \
  632. copy_to_user(d1->d_name, shortname, short_len) || \
  633. put_user(0, d1->d_name + short_len) || \
  634. put_user(short_len, &d1->d_reclen)) \
  635. goto efault; \
  636. } \
  637. return 0; \
  638. efault: \
  639. buf->result = -EFAULT; \
  640. return -EFAULT; \
  641. }
  642. FAT_IOCTL_FILLDIR_FUNC(fat_ioctl_filldir, dirent)
  643. static int fat_ioctl_readdir(struct inode *inode, struct file *filp,
  644. void __user *dirent, filldir_t filldir,
  645. int short_only, int both)
  646. {
  647. struct fat_ioctl_filldir_callback buf;
  648. int ret;
  649. buf.dirent = dirent;
  650. buf.result = 0;
  651. mutex_lock(&inode->i_mutex);
  652. ret = -ENOENT;
  653. if (!IS_DEADDIR(inode)) {
  654. ret = __fat_readdir(inode, filp, &buf, filldir,
  655. short_only, both);
  656. }
  657. mutex_unlock(&inode->i_mutex);
  658. if (ret >= 0)
  659. ret = buf.result;
  660. return ret;
  661. }
  662. static int fat_dir_ioctl(struct inode *inode, struct file *filp,
  663. unsigned int cmd, unsigned long arg)
  664. {
  665. struct dirent __user *d1 = (struct dirent __user *)arg;
  666. int short_only, both;
  667. switch (cmd) {
  668. case VFAT_IOCTL_READDIR_SHORT:
  669. short_only = 1;
  670. both = 0;
  671. break;
  672. case VFAT_IOCTL_READDIR_BOTH:
  673. short_only = 0;
  674. both = 1;
  675. break;
  676. default:
  677. return fat_generic_ioctl(inode, filp, cmd, arg);
  678. }
  679. if (!access_ok(VERIFY_WRITE, d1, sizeof(struct dirent[2])))
  680. return -EFAULT;
  681. /*
  682. * Yes, we don't need this put_user() absolutely. However old
  683. * code didn't return the right value. So, app use this value,
  684. * in order to check whether it is EOF.
  685. */
  686. if (put_user(0, &d1->d_reclen))
  687. return -EFAULT;
  688. return fat_ioctl_readdir(inode, filp, d1, fat_ioctl_filldir,
  689. short_only, both);
  690. }
  691. #ifdef CONFIG_COMPAT
  692. #define VFAT_IOCTL_READDIR_BOTH32 _IOR('r', 1, struct compat_dirent[2])
  693. #define VFAT_IOCTL_READDIR_SHORT32 _IOR('r', 2, struct compat_dirent[2])
  694. FAT_IOCTL_FILLDIR_FUNC(fat_compat_ioctl_filldir, compat_dirent)
  695. static long fat_compat_dir_ioctl(struct file *filp, unsigned cmd,
  696. unsigned long arg)
  697. {
  698. struct inode *inode = filp->f_path.dentry->d_inode;
  699. struct compat_dirent __user *d1 = compat_ptr(arg);
  700. int short_only, both;
  701. switch (cmd) {
  702. case VFAT_IOCTL_READDIR_SHORT32:
  703. short_only = 1;
  704. both = 0;
  705. break;
  706. case VFAT_IOCTL_READDIR_BOTH32:
  707. short_only = 0;
  708. both = 1;
  709. break;
  710. default:
  711. return -ENOIOCTLCMD;
  712. }
  713. if (!access_ok(VERIFY_WRITE, d1, sizeof(struct compat_dirent[2])))
  714. return -EFAULT;
  715. /*
  716. * Yes, we don't need this put_user() absolutely. However old
  717. * code didn't return the right value. So, app use this value,
  718. * in order to check whether it is EOF.
  719. */
  720. if (put_user(0, &d1->d_reclen))
  721. return -EFAULT;
  722. return fat_ioctl_readdir(inode, filp, d1, fat_compat_ioctl_filldir,
  723. short_only, both);
  724. }
  725. #endif /* CONFIG_COMPAT */
  726. const struct file_operations fat_dir_operations = {
  727. .read = generic_read_dir,
  728. .readdir = fat_readdir,
  729. .ioctl = fat_dir_ioctl,
  730. #ifdef CONFIG_COMPAT
  731. .compat_ioctl = fat_compat_dir_ioctl,
  732. #endif
  733. .fsync = file_fsync,
  734. };
  735. static int fat_get_short_entry(struct inode *dir, loff_t *pos,
  736. struct buffer_head **bh,
  737. struct msdos_dir_entry **de)
  738. {
  739. while (fat_get_entry(dir, pos, bh, de) >= 0) {
  740. /* free entry or long name entry or volume label */
  741. if (!IS_FREE((*de)->name) && !((*de)->attr & ATTR_VOLUME))
  742. return 0;
  743. }
  744. return -ENOENT;
  745. }
  746. /*
  747. * The ".." entry can not provide the "struct fat_slot_info" informations
  748. * for inode. So, this function provide the some informations only.
  749. */
  750. int fat_get_dotdot_entry(struct inode *dir, struct buffer_head **bh,
  751. struct msdos_dir_entry **de, loff_t *i_pos)
  752. {
  753. loff_t offset;
  754. offset = 0;
  755. *bh = NULL;
  756. while (fat_get_short_entry(dir, &offset, bh, de) >= 0) {
  757. if (!strncmp((*de)->name, MSDOS_DOTDOT, MSDOS_NAME)) {
  758. *i_pos = fat_make_i_pos(dir->i_sb, *bh, *de);
  759. return 0;
  760. }
  761. }
  762. return -ENOENT;
  763. }
  764. EXPORT_SYMBOL_GPL(fat_get_dotdot_entry);
  765. /* See if directory is empty */
  766. int fat_dir_empty(struct inode *dir)
  767. {
  768. struct buffer_head *bh;
  769. struct msdos_dir_entry *de;
  770. loff_t cpos;
  771. int result = 0;
  772. bh = NULL;
  773. cpos = 0;
  774. while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
  775. if (strncmp(de->name, MSDOS_DOT , MSDOS_NAME) &&
  776. strncmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
  777. result = -ENOTEMPTY;
  778. break;
  779. }
  780. }
  781. brelse(bh);
  782. return result;
  783. }
  784. EXPORT_SYMBOL_GPL(fat_dir_empty);
  785. /*
  786. * fat_subdirs counts the number of sub-directories of dir. It can be run
  787. * on directories being created.
  788. */
  789. int fat_subdirs(struct inode *dir)
  790. {
  791. struct buffer_head *bh;
  792. struct msdos_dir_entry *de;
  793. loff_t cpos;
  794. int count = 0;
  795. bh = NULL;
  796. cpos = 0;
  797. while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
  798. if (de->attr & ATTR_DIR)
  799. count++;
  800. }
  801. brelse(bh);
  802. return count;
  803. }
  804. /*
  805. * Scans a directory for a given file (name points to its formatted name).
  806. * Returns an error code or zero.
  807. */
  808. int fat_scan(struct inode *dir, const unsigned char *name,
  809. struct fat_slot_info *sinfo)
  810. {
  811. struct super_block *sb = dir->i_sb;
  812. sinfo->slot_off = 0;
  813. sinfo->bh = NULL;
  814. while (fat_get_short_entry(dir, &sinfo->slot_off, &sinfo->bh,
  815. &sinfo->de) >= 0) {
  816. if (!strncmp(sinfo->de->name, name, MSDOS_NAME)) {
  817. sinfo->slot_off -= sizeof(*sinfo->de);
  818. sinfo->nr_slots = 1;
  819. sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
  820. return 0;
  821. }
  822. }
  823. return -ENOENT;
  824. }
  825. EXPORT_SYMBOL_GPL(fat_scan);
  826. static int __fat_remove_entries(struct inode *dir, loff_t pos, int nr_slots)
  827. {
  828. struct super_block *sb = dir->i_sb;
  829. struct buffer_head *bh;
  830. struct msdos_dir_entry *de, *endp;
  831. int err = 0, orig_slots;
  832. while (nr_slots) {
  833. bh = NULL;
  834. if (fat_get_entry(dir, &pos, &bh, &de) < 0) {
  835. err = -EIO;
  836. break;
  837. }
  838. orig_slots = nr_slots;
  839. endp = (struct msdos_dir_entry *)(bh->b_data + sb->s_blocksize);
  840. while (nr_slots && de < endp) {
  841. de->name[0] = DELETED_FLAG;
  842. de++;
  843. nr_slots--;
  844. }
  845. mark_buffer_dirty(bh);
  846. if (IS_DIRSYNC(dir))
  847. err = sync_dirty_buffer(bh);
  848. brelse(bh);
  849. if (err)
  850. break;
  851. /* pos is *next* de's position, so this does `- sizeof(de)' */
  852. pos += ((orig_slots - nr_slots) * sizeof(*de)) - sizeof(*de);
  853. }
  854. return err;
  855. }
  856. int fat_remove_entries(struct inode *dir, struct fat_slot_info *sinfo)
  857. {
  858. struct msdos_dir_entry *de;
  859. struct buffer_head *bh;
  860. int err = 0, nr_slots;
  861. /*
  862. * First stage: Remove the shortname. By this, the directory
  863. * entry is removed.
  864. */
  865. nr_slots = sinfo->nr_slots;
  866. de = sinfo->de;
  867. sinfo->de = NULL;
  868. bh = sinfo->bh;
  869. sinfo->bh = NULL;
  870. while (nr_slots && de >= (struct msdos_dir_entry *)bh->b_data) {
  871. de->name[0] = DELETED_FLAG;
  872. de--;
  873. nr_slots--;
  874. }
  875. mark_buffer_dirty(bh);
  876. if (IS_DIRSYNC(dir))
  877. err = sync_dirty_buffer(bh);
  878. brelse(bh);
  879. if (err)
  880. return err;
  881. dir->i_version++;
  882. if (nr_slots) {
  883. /*
  884. * Second stage: remove the remaining longname slots.
  885. * (This directory entry is already removed, and so return
  886. * the success)
  887. */
  888. err = __fat_remove_entries(dir, sinfo->slot_off, nr_slots);
  889. if (err) {
  890. printk(KERN_WARNING
  891. "FAT: Couldn't remove the long name slots\n");
  892. }
  893. }
  894. dir->i_mtime = dir->i_atime = CURRENT_TIME_SEC;
  895. if (IS_DIRSYNC(dir))
  896. (void)fat_sync_inode(dir);
  897. else
  898. mark_inode_dirty(dir);
  899. return 0;
  900. }
  901. EXPORT_SYMBOL_GPL(fat_remove_entries);
  902. static int fat_zeroed_cluster(struct inode *dir, sector_t blknr, int nr_used,
  903. struct buffer_head **bhs, int nr_bhs)
  904. {
  905. struct super_block *sb = dir->i_sb;
  906. sector_t last_blknr = blknr + MSDOS_SB(sb)->sec_per_clus;
  907. int err, i, n;
  908. /* Zeroing the unused blocks on this cluster */
  909. blknr += nr_used;
  910. n = nr_used;
  911. while (blknr < last_blknr) {
  912. bhs[n] = sb_getblk(sb, blknr);
  913. if (!bhs[n]) {
  914. err = -ENOMEM;
  915. goto error;
  916. }
  917. memset(bhs[n]->b_data, 0, sb->s_blocksize);
  918. set_buffer_uptodate(bhs[n]);
  919. mark_buffer_dirty(bhs[n]);
  920. n++;
  921. blknr++;
  922. if (n == nr_bhs) {
  923. if (IS_DIRSYNC(dir)) {
  924. err = fat_sync_bhs(bhs, n);
  925. if (err)
  926. goto error;
  927. }
  928. for (i = 0; i < n; i++)
  929. brelse(bhs[i]);
  930. n = 0;
  931. }
  932. }
  933. if (IS_DIRSYNC(dir)) {
  934. err = fat_sync_bhs(bhs, n);
  935. if (err)
  936. goto error;
  937. }
  938. for (i = 0; i < n; i++)
  939. brelse(bhs[i]);
  940. return 0;
  941. error:
  942. for (i = 0; i < n; i++)
  943. bforget(bhs[i]);
  944. return err;
  945. }
  946. int fat_alloc_new_dir(struct inode *dir, struct timespec *ts)
  947. {
  948. struct super_block *sb = dir->i_sb;
  949. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  950. struct buffer_head *bhs[MAX_BUF_PER_PAGE];
  951. struct msdos_dir_entry *de;
  952. sector_t blknr;
  953. __le16 date, time;
  954. int err, cluster;
  955. err = fat_alloc_clusters(dir, &cluster, 1);
  956. if (err)
  957. goto error;
  958. blknr = fat_clus_to_blknr(sbi, cluster);
  959. bhs[0] = sb_getblk(sb, blknr);
  960. if (!bhs[0]) {
  961. err = -ENOMEM;
  962. goto error_free;
  963. }
  964. fat_date_unix2dos(ts->tv_sec, &time, &date);
  965. de = (struct msdos_dir_entry *)bhs[0]->b_data;
  966. /* filling the new directory slots ("." and ".." entries) */
  967. memcpy(de[0].name, MSDOS_DOT, MSDOS_NAME);
  968. memcpy(de[1].name, MSDOS_DOTDOT, MSDOS_NAME);
  969. de->attr = de[1].attr = ATTR_DIR;
  970. de[0].lcase = de[1].lcase = 0;
  971. de[0].time = de[1].time = time;
  972. de[0].date = de[1].date = date;
  973. de[0].ctime_cs = de[1].ctime_cs = 0;
  974. if (sbi->options.isvfat) {
  975. /* extra timestamps */
  976. de[0].ctime = de[1].ctime = time;
  977. de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = date;
  978. } else {
  979. de[0].ctime = de[1].ctime = 0;
  980. de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = 0;
  981. }
  982. de[0].start = cpu_to_le16(cluster);
  983. de[0].starthi = cpu_to_le16(cluster >> 16);
  984. de[1].start = cpu_to_le16(MSDOS_I(dir)->i_logstart);
  985. de[1].starthi = cpu_to_le16(MSDOS_I(dir)->i_logstart >> 16);
  986. de[0].size = de[1].size = 0;
  987. memset(de + 2, 0, sb->s_blocksize - 2 * sizeof(*de));
  988. set_buffer_uptodate(bhs[0]);
  989. mark_buffer_dirty(bhs[0]);
  990. err = fat_zeroed_cluster(dir, blknr, 1, bhs, MAX_BUF_PER_PAGE);
  991. if (err)
  992. goto error_free;
  993. return cluster;
  994. error_free:
  995. fat_free_clusters(dir, cluster);
  996. error:
  997. return err;
  998. }
  999. EXPORT_SYMBOL_GPL(fat_alloc_new_dir);
  1000. static int fat_add_new_entries(struct inode *dir, void *slots, int nr_slots,
  1001. int *nr_cluster, struct msdos_dir_entry **de,
  1002. struct buffer_head **bh, loff_t *i_pos)
  1003. {
  1004. struct super_block *sb = dir->i_sb;
  1005. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  1006. struct buffer_head *bhs[MAX_BUF_PER_PAGE];
  1007. sector_t blknr, start_blknr, last_blknr;
  1008. unsigned long size, copy;
  1009. int err, i, n, offset, cluster[2];
  1010. /*
  1011. * The minimum cluster size is 512bytes, and maximum entry
  1012. * size is 32*slots (672bytes). So, iff the cluster size is
  1013. * 512bytes, we may need two clusters.
  1014. */
  1015. size = nr_slots * sizeof(struct msdos_dir_entry);
  1016. *nr_cluster = (size + (sbi->cluster_size - 1)) >> sbi->cluster_bits;
  1017. BUG_ON(*nr_cluster > 2);
  1018. err = fat_alloc_clusters(dir, cluster, *nr_cluster);
  1019. if (err)
  1020. goto error;
  1021. /*
  1022. * First stage: Fill the directory entry. NOTE: This cluster
  1023. * is not referenced from any inode yet, so updates order is
  1024. * not important.
  1025. */
  1026. i = n = copy = 0;
  1027. do {
  1028. start_blknr = blknr = fat_clus_to_blknr(sbi, cluster[i]);
  1029. last_blknr = start_blknr + sbi->sec_per_clus;
  1030. while (blknr < last_blknr) {
  1031. bhs[n] = sb_getblk(sb, blknr);
  1032. if (!bhs[n]) {
  1033. err = -ENOMEM;
  1034. goto error_nomem;
  1035. }
  1036. /* fill the directory entry */
  1037. copy = min(size, sb->s_blocksize);
  1038. memcpy(bhs[n]->b_data, slots, copy);
  1039. slots += copy;
  1040. size -= copy;
  1041. set_buffer_uptodate(bhs[n]);
  1042. mark_buffer_dirty(bhs[n]);
  1043. if (!size)
  1044. break;
  1045. n++;
  1046. blknr++;
  1047. }
  1048. } while (++i < *nr_cluster);
  1049. memset(bhs[n]->b_data + copy, 0, sb->s_blocksize - copy);
  1050. offset = copy - sizeof(struct msdos_dir_entry);
  1051. get_bh(bhs[n]);
  1052. *bh = bhs[n];
  1053. *de = (struct msdos_dir_entry *)((*bh)->b_data + offset);
  1054. *i_pos = fat_make_i_pos(sb, *bh, *de);
  1055. /* Second stage: clear the rest of cluster, and write outs */
  1056. err = fat_zeroed_cluster(dir, start_blknr, ++n, bhs, MAX_BUF_PER_PAGE);
  1057. if (err)
  1058. goto error_free;
  1059. return cluster[0];
  1060. error_free:
  1061. brelse(*bh);
  1062. *bh = NULL;
  1063. n = 0;
  1064. error_nomem:
  1065. for (i = 0; i < n; i++)
  1066. bforget(bhs[i]);
  1067. fat_free_clusters(dir, cluster[0]);
  1068. error:
  1069. return err;
  1070. }
  1071. int fat_add_entries(struct inode *dir, void *slots, int nr_slots,
  1072. struct fat_slot_info *sinfo)
  1073. {
  1074. struct super_block *sb = dir->i_sb;
  1075. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  1076. struct buffer_head *bh, *prev, *bhs[3]; /* 32*slots (672bytes) */
  1077. struct msdos_dir_entry *de;
  1078. int err, free_slots, i, nr_bhs;
  1079. loff_t pos, i_pos;
  1080. sinfo->nr_slots = nr_slots;
  1081. /* First stage: search free direcotry entries */
  1082. free_slots = nr_bhs = 0;
  1083. bh = prev = NULL;
  1084. pos = 0;
  1085. err = -ENOSPC;
  1086. while (fat_get_entry(dir, &pos, &bh, &de) > -1) {
  1087. /* check the maximum size of directory */
  1088. if (pos >= FAT_MAX_DIR_SIZE)
  1089. goto error;
  1090. if (IS_FREE(de->name)) {
  1091. if (prev != bh) {
  1092. get_bh(bh);
  1093. bhs[nr_bhs] = prev = bh;
  1094. nr_bhs++;
  1095. }
  1096. free_slots++;
  1097. if (free_slots == nr_slots)
  1098. goto found;
  1099. } else {
  1100. for (i = 0; i < nr_bhs; i++)
  1101. brelse(bhs[i]);
  1102. prev = NULL;
  1103. free_slots = nr_bhs = 0;
  1104. }
  1105. }
  1106. if (dir->i_ino == MSDOS_ROOT_INO) {
  1107. if (sbi->fat_bits != 32)
  1108. goto error;
  1109. } else if (MSDOS_I(dir)->i_start == 0) {
  1110. printk(KERN_ERR "FAT: Corrupted directory (i_pos %lld)\n",
  1111. MSDOS_I(dir)->i_pos);
  1112. err = -EIO;
  1113. goto error;
  1114. }
  1115. found:
  1116. err = 0;
  1117. pos -= free_slots * sizeof(*de);
  1118. nr_slots -= free_slots;
  1119. if (free_slots) {
  1120. /*
  1121. * Second stage: filling the free entries with new entries.
  1122. * NOTE: If this slots has shortname, first, we write
  1123. * the long name slots, then write the short name.
  1124. */
  1125. int size = free_slots * sizeof(*de);
  1126. int offset = pos & (sb->s_blocksize - 1);
  1127. int long_bhs = nr_bhs - (nr_slots == 0);
  1128. /* Fill the long name slots. */
  1129. for (i = 0; i < long_bhs; i++) {
  1130. int copy = min_t(int, sb->s_blocksize - offset, size);
  1131. memcpy(bhs[i]->b_data + offset, slots, copy);
  1132. mark_buffer_dirty(bhs[i]);
  1133. offset = 0;
  1134. slots += copy;
  1135. size -= copy;
  1136. }
  1137. if (long_bhs && IS_DIRSYNC(dir))
  1138. err = fat_sync_bhs(bhs, long_bhs);
  1139. if (!err && i < nr_bhs) {
  1140. /* Fill the short name slot. */
  1141. int copy = min_t(int, sb->s_blocksize - offset, size);
  1142. memcpy(bhs[i]->b_data + offset, slots, copy);
  1143. mark_buffer_dirty(bhs[i]);
  1144. if (IS_DIRSYNC(dir))
  1145. err = sync_dirty_buffer(bhs[i]);
  1146. }
  1147. for (i = 0; i < nr_bhs; i++)
  1148. brelse(bhs[i]);
  1149. if (err)
  1150. goto error_remove;
  1151. }
  1152. if (nr_slots) {
  1153. int cluster, nr_cluster;
  1154. /*
  1155. * Third stage: allocate the cluster for new entries.
  1156. * And initialize the cluster with new entries, then
  1157. * add the cluster to dir.
  1158. */
  1159. cluster = fat_add_new_entries(dir, slots, nr_slots, &nr_cluster,
  1160. &de, &bh, &i_pos);
  1161. if (cluster < 0) {
  1162. err = cluster;
  1163. goto error_remove;
  1164. }
  1165. err = fat_chain_add(dir, cluster, nr_cluster);
  1166. if (err) {
  1167. fat_free_clusters(dir, cluster);
  1168. goto error_remove;
  1169. }
  1170. if (dir->i_size & (sbi->cluster_size - 1)) {
  1171. fat_fs_panic(sb, "Odd directory size");
  1172. dir->i_size = (dir->i_size + sbi->cluster_size - 1)
  1173. & ~((loff_t)sbi->cluster_size - 1);
  1174. }
  1175. dir->i_size += nr_cluster << sbi->cluster_bits;
  1176. MSDOS_I(dir)->mmu_private += nr_cluster << sbi->cluster_bits;
  1177. }
  1178. sinfo->slot_off = pos;
  1179. sinfo->de = de;
  1180. sinfo->bh = bh;
  1181. sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
  1182. return 0;
  1183. error:
  1184. brelse(bh);
  1185. for (i = 0; i < nr_bhs; i++)
  1186. brelse(bhs[i]);
  1187. return err;
  1188. error_remove:
  1189. brelse(bh);
  1190. if (free_slots)
  1191. __fat_remove_entries(dir, pos, free_slots);
  1192. return err;
  1193. }
  1194. EXPORT_SYMBOL_GPL(fat_add_entries);