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