dir.c 34 KB

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