dir.c 34 KB

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