dir.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  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(sb, KERN_ERR, "Directory bread(block %llu) failed",
  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(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 long fat_dir_ioctl(struct file *filp, unsigned int cmd,
  688. unsigned long arg)
  689. {
  690. struct inode *inode = filp->f_path.dentry->d_inode;
  691. struct __fat_dirent __user *d1 = (struct __fat_dirent __user *)arg;
  692. int short_only, both;
  693. switch (cmd) {
  694. case VFAT_IOCTL_READDIR_SHORT:
  695. short_only = 1;
  696. both = 0;
  697. break;
  698. case VFAT_IOCTL_READDIR_BOTH:
  699. short_only = 0;
  700. both = 1;
  701. break;
  702. default:
  703. return fat_generic_ioctl(filp, cmd, arg);
  704. }
  705. if (!access_ok(VERIFY_WRITE, d1, sizeof(struct __fat_dirent[2])))
  706. return -EFAULT;
  707. /*
  708. * Yes, we don't need this put_user() absolutely. However old
  709. * code didn't return the right value. So, app use this value,
  710. * in order to check whether it is EOF.
  711. */
  712. if (put_user(0, &d1->d_reclen))
  713. return -EFAULT;
  714. return fat_ioctl_readdir(inode, filp, d1, fat_ioctl_filldir,
  715. short_only, both);
  716. }
  717. #ifdef CONFIG_COMPAT
  718. #define VFAT_IOCTL_READDIR_BOTH32 _IOR('r', 1, struct compat_dirent[2])
  719. #define VFAT_IOCTL_READDIR_SHORT32 _IOR('r', 2, struct compat_dirent[2])
  720. FAT_IOCTL_FILLDIR_FUNC(fat_compat_ioctl_filldir, compat_dirent)
  721. static long fat_compat_dir_ioctl(struct file *filp, unsigned cmd,
  722. unsigned long arg)
  723. {
  724. struct inode *inode = filp->f_path.dentry->d_inode;
  725. struct compat_dirent __user *d1 = compat_ptr(arg);
  726. int short_only, both;
  727. switch (cmd) {
  728. case VFAT_IOCTL_READDIR_SHORT32:
  729. short_only = 1;
  730. both = 0;
  731. break;
  732. case VFAT_IOCTL_READDIR_BOTH32:
  733. short_only = 0;
  734. both = 1;
  735. break;
  736. default:
  737. return fat_generic_ioctl(filp, cmd, (unsigned long)arg);
  738. }
  739. if (!access_ok(VERIFY_WRITE, d1, sizeof(struct compat_dirent[2])))
  740. return -EFAULT;
  741. /*
  742. * Yes, we don't need this put_user() absolutely. However old
  743. * code didn't return the right value. So, app use this value,
  744. * in order to check whether it is EOF.
  745. */
  746. if (put_user(0, &d1->d_reclen))
  747. return -EFAULT;
  748. return fat_ioctl_readdir(inode, filp, d1, fat_compat_ioctl_filldir,
  749. short_only, both);
  750. }
  751. #endif /* CONFIG_COMPAT */
  752. const struct file_operations fat_dir_operations = {
  753. .llseek = generic_file_llseek,
  754. .read = generic_read_dir,
  755. .readdir = fat_readdir,
  756. .unlocked_ioctl = fat_dir_ioctl,
  757. #ifdef CONFIG_COMPAT
  758. .compat_ioctl = fat_compat_dir_ioctl,
  759. #endif
  760. .fsync = fat_file_fsync,
  761. };
  762. static int fat_get_short_entry(struct inode *dir, loff_t *pos,
  763. struct buffer_head **bh,
  764. struct msdos_dir_entry **de)
  765. {
  766. while (fat_get_entry(dir, pos, bh, de) >= 0) {
  767. /* free entry or long name entry or volume label */
  768. if (!IS_FREE((*de)->name) && !((*de)->attr & ATTR_VOLUME))
  769. return 0;
  770. }
  771. return -ENOENT;
  772. }
  773. /*
  774. * The ".." entry can not provide the "struct fat_slot_info" informations
  775. * for inode. So, this function provide the some informations only.
  776. */
  777. int fat_get_dotdot_entry(struct inode *dir, struct buffer_head **bh,
  778. struct msdos_dir_entry **de, loff_t *i_pos)
  779. {
  780. loff_t offset;
  781. offset = 0;
  782. *bh = NULL;
  783. while (fat_get_short_entry(dir, &offset, bh, de) >= 0) {
  784. if (!strncmp((*de)->name, MSDOS_DOTDOT, MSDOS_NAME)) {
  785. *i_pos = fat_make_i_pos(dir->i_sb, *bh, *de);
  786. return 0;
  787. }
  788. }
  789. return -ENOENT;
  790. }
  791. EXPORT_SYMBOL_GPL(fat_get_dotdot_entry);
  792. /* See if directory is empty */
  793. int fat_dir_empty(struct inode *dir)
  794. {
  795. struct buffer_head *bh;
  796. struct msdos_dir_entry *de;
  797. loff_t cpos;
  798. int result = 0;
  799. bh = NULL;
  800. cpos = 0;
  801. while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
  802. if (strncmp(de->name, MSDOS_DOT , MSDOS_NAME) &&
  803. strncmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
  804. result = -ENOTEMPTY;
  805. break;
  806. }
  807. }
  808. brelse(bh);
  809. return result;
  810. }
  811. EXPORT_SYMBOL_GPL(fat_dir_empty);
  812. /*
  813. * fat_subdirs counts the number of sub-directories of dir. It can be run
  814. * on directories being created.
  815. */
  816. int fat_subdirs(struct inode *dir)
  817. {
  818. struct buffer_head *bh;
  819. struct msdos_dir_entry *de;
  820. loff_t cpos;
  821. int count = 0;
  822. bh = NULL;
  823. cpos = 0;
  824. while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
  825. if (de->attr & ATTR_DIR)
  826. count++;
  827. }
  828. brelse(bh);
  829. return count;
  830. }
  831. /*
  832. * Scans a directory for a given file (name points to its formatted name).
  833. * Returns an error code or zero.
  834. */
  835. int fat_scan(struct inode *dir, const unsigned char *name,
  836. struct fat_slot_info *sinfo)
  837. {
  838. struct super_block *sb = dir->i_sb;
  839. sinfo->slot_off = 0;
  840. sinfo->bh = NULL;
  841. while (fat_get_short_entry(dir, &sinfo->slot_off, &sinfo->bh,
  842. &sinfo->de) >= 0) {
  843. if (!strncmp(sinfo->de->name, name, MSDOS_NAME)) {
  844. sinfo->slot_off -= sizeof(*sinfo->de);
  845. sinfo->nr_slots = 1;
  846. sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
  847. return 0;
  848. }
  849. }
  850. return -ENOENT;
  851. }
  852. EXPORT_SYMBOL_GPL(fat_scan);
  853. static int __fat_remove_entries(struct inode *dir, loff_t pos, int nr_slots)
  854. {
  855. struct super_block *sb = dir->i_sb;
  856. struct buffer_head *bh;
  857. struct msdos_dir_entry *de, *endp;
  858. int err = 0, orig_slots;
  859. while (nr_slots) {
  860. bh = NULL;
  861. if (fat_get_entry(dir, &pos, &bh, &de) < 0) {
  862. err = -EIO;
  863. break;
  864. }
  865. orig_slots = nr_slots;
  866. endp = (struct msdos_dir_entry *)(bh->b_data + sb->s_blocksize);
  867. while (nr_slots && de < endp) {
  868. de->name[0] = DELETED_FLAG;
  869. de++;
  870. nr_slots--;
  871. }
  872. mark_buffer_dirty_inode(bh, dir);
  873. if (IS_DIRSYNC(dir))
  874. err = sync_dirty_buffer(bh);
  875. brelse(bh);
  876. if (err)
  877. break;
  878. /* pos is *next* de's position, so this does `- sizeof(de)' */
  879. pos += ((orig_slots - nr_slots) * sizeof(*de)) - sizeof(*de);
  880. }
  881. return err;
  882. }
  883. int fat_remove_entries(struct inode *dir, struct fat_slot_info *sinfo)
  884. {
  885. struct super_block *sb = dir->i_sb;
  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_inode(bh, dir);
  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. fat_msg(sb, KERN_WARNING,
  919. "Couldn't remove the long name slots");
  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_inode(bhs[n], dir);
  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. u8 time_cs;
  983. int err, cluster;
  984. err = fat_alloc_clusters(dir, &cluster, 1);
  985. if (err)
  986. goto error;
  987. blknr = fat_clus_to_blknr(sbi, cluster);
  988. bhs[0] = sb_getblk(sb, blknr);
  989. if (!bhs[0]) {
  990. err = -ENOMEM;
  991. goto error_free;
  992. }
  993. fat_time_unix2fat(sbi, ts, &time, &date, &time_cs);
  994. de = (struct msdos_dir_entry *)bhs[0]->b_data;
  995. /* filling the new directory slots ("." and ".." entries) */
  996. memcpy(de[0].name, MSDOS_DOT, MSDOS_NAME);
  997. memcpy(de[1].name, MSDOS_DOTDOT, MSDOS_NAME);
  998. de->attr = de[1].attr = ATTR_DIR;
  999. de[0].lcase = de[1].lcase = 0;
  1000. de[0].time = de[1].time = time;
  1001. de[0].date = de[1].date = date;
  1002. if (sbi->options.isvfat) {
  1003. /* extra timestamps */
  1004. de[0].ctime = de[1].ctime = time;
  1005. de[0].ctime_cs = de[1].ctime_cs = time_cs;
  1006. de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = date;
  1007. } else {
  1008. de[0].ctime = de[1].ctime = 0;
  1009. de[0].ctime_cs = de[1].ctime_cs = 0;
  1010. de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = 0;
  1011. }
  1012. de[0].start = cpu_to_le16(cluster);
  1013. de[0].starthi = cpu_to_le16(cluster >> 16);
  1014. de[1].start = cpu_to_le16(MSDOS_I(dir)->i_logstart);
  1015. de[1].starthi = cpu_to_le16(MSDOS_I(dir)->i_logstart >> 16);
  1016. de[0].size = de[1].size = 0;
  1017. memset(de + 2, 0, sb->s_blocksize - 2 * sizeof(*de));
  1018. set_buffer_uptodate(bhs[0]);
  1019. mark_buffer_dirty_inode(bhs[0], dir);
  1020. err = fat_zeroed_cluster(dir, blknr, 1, bhs, MAX_BUF_PER_PAGE);
  1021. if (err)
  1022. goto error_free;
  1023. return cluster;
  1024. error_free:
  1025. fat_free_clusters(dir, cluster);
  1026. error:
  1027. return err;
  1028. }
  1029. EXPORT_SYMBOL_GPL(fat_alloc_new_dir);
  1030. static int fat_add_new_entries(struct inode *dir, void *slots, int nr_slots,
  1031. int *nr_cluster, struct msdos_dir_entry **de,
  1032. struct buffer_head **bh, loff_t *i_pos)
  1033. {
  1034. struct super_block *sb = dir->i_sb;
  1035. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  1036. struct buffer_head *bhs[MAX_BUF_PER_PAGE];
  1037. sector_t blknr, start_blknr, last_blknr;
  1038. unsigned long size, copy;
  1039. int err, i, n, offset, cluster[2];
  1040. /*
  1041. * The minimum cluster size is 512bytes, and maximum entry
  1042. * size is 32*slots (672bytes). So, iff the cluster size is
  1043. * 512bytes, we may need two clusters.
  1044. */
  1045. size = nr_slots * sizeof(struct msdos_dir_entry);
  1046. *nr_cluster = (size + (sbi->cluster_size - 1)) >> sbi->cluster_bits;
  1047. BUG_ON(*nr_cluster > 2);
  1048. err = fat_alloc_clusters(dir, cluster, *nr_cluster);
  1049. if (err)
  1050. goto error;
  1051. /*
  1052. * First stage: Fill the directory entry. NOTE: This cluster
  1053. * is not referenced from any inode yet, so updates order is
  1054. * not important.
  1055. */
  1056. i = n = copy = 0;
  1057. do {
  1058. start_blknr = blknr = fat_clus_to_blknr(sbi, cluster[i]);
  1059. last_blknr = start_blknr + sbi->sec_per_clus;
  1060. while (blknr < last_blknr) {
  1061. bhs[n] = sb_getblk(sb, blknr);
  1062. if (!bhs[n]) {
  1063. err = -ENOMEM;
  1064. goto error_nomem;
  1065. }
  1066. /* fill the directory entry */
  1067. copy = min(size, sb->s_blocksize);
  1068. memcpy(bhs[n]->b_data, slots, copy);
  1069. slots += copy;
  1070. size -= copy;
  1071. set_buffer_uptodate(bhs[n]);
  1072. mark_buffer_dirty_inode(bhs[n], dir);
  1073. if (!size)
  1074. break;
  1075. n++;
  1076. blknr++;
  1077. }
  1078. } while (++i < *nr_cluster);
  1079. memset(bhs[n]->b_data + copy, 0, sb->s_blocksize - copy);
  1080. offset = copy - sizeof(struct msdos_dir_entry);
  1081. get_bh(bhs[n]);
  1082. *bh = bhs[n];
  1083. *de = (struct msdos_dir_entry *)((*bh)->b_data + offset);
  1084. *i_pos = fat_make_i_pos(sb, *bh, *de);
  1085. /* Second stage: clear the rest of cluster, and write outs */
  1086. err = fat_zeroed_cluster(dir, start_blknr, ++n, bhs, MAX_BUF_PER_PAGE);
  1087. if (err)
  1088. goto error_free;
  1089. return cluster[0];
  1090. error_free:
  1091. brelse(*bh);
  1092. *bh = NULL;
  1093. n = 0;
  1094. error_nomem:
  1095. for (i = 0; i < n; i++)
  1096. bforget(bhs[i]);
  1097. fat_free_clusters(dir, cluster[0]);
  1098. error:
  1099. return err;
  1100. }
  1101. int fat_add_entries(struct inode *dir, void *slots, int nr_slots,
  1102. struct fat_slot_info *sinfo)
  1103. {
  1104. struct super_block *sb = dir->i_sb;
  1105. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  1106. struct buffer_head *bh, *prev, *bhs[3]; /* 32*slots (672bytes) */
  1107. struct msdos_dir_entry *uninitialized_var(de);
  1108. int err, free_slots, i, nr_bhs;
  1109. loff_t pos, i_pos;
  1110. sinfo->nr_slots = nr_slots;
  1111. /* First stage: search free direcotry entries */
  1112. free_slots = nr_bhs = 0;
  1113. bh = prev = NULL;
  1114. pos = 0;
  1115. err = -ENOSPC;
  1116. while (fat_get_entry(dir, &pos, &bh, &de) > -1) {
  1117. /* check the maximum size of directory */
  1118. if (pos >= FAT_MAX_DIR_SIZE)
  1119. goto error;
  1120. if (IS_FREE(de->name)) {
  1121. if (prev != bh) {
  1122. get_bh(bh);
  1123. bhs[nr_bhs] = prev = bh;
  1124. nr_bhs++;
  1125. }
  1126. free_slots++;
  1127. if (free_slots == nr_slots)
  1128. goto found;
  1129. } else {
  1130. for (i = 0; i < nr_bhs; i++)
  1131. brelse(bhs[i]);
  1132. prev = NULL;
  1133. free_slots = nr_bhs = 0;
  1134. }
  1135. }
  1136. if (dir->i_ino == MSDOS_ROOT_INO) {
  1137. if (sbi->fat_bits != 32)
  1138. goto error;
  1139. } else if (MSDOS_I(dir)->i_start == 0) {
  1140. fat_msg(sb, KERN_ERR, "Corrupted directory (i_pos %lld)",
  1141. MSDOS_I(dir)->i_pos);
  1142. err = -EIO;
  1143. goto error;
  1144. }
  1145. found:
  1146. err = 0;
  1147. pos -= free_slots * sizeof(*de);
  1148. nr_slots -= free_slots;
  1149. if (free_slots) {
  1150. /*
  1151. * Second stage: filling the free entries with new entries.
  1152. * NOTE: If this slots has shortname, first, we write
  1153. * the long name slots, then write the short name.
  1154. */
  1155. int size = free_slots * sizeof(*de);
  1156. int offset = pos & (sb->s_blocksize - 1);
  1157. int long_bhs = nr_bhs - (nr_slots == 0);
  1158. /* Fill the long name slots. */
  1159. for (i = 0; i < long_bhs; i++) {
  1160. int copy = min_t(int, sb->s_blocksize - offset, size);
  1161. memcpy(bhs[i]->b_data + offset, slots, copy);
  1162. mark_buffer_dirty_inode(bhs[i], dir);
  1163. offset = 0;
  1164. slots += copy;
  1165. size -= copy;
  1166. }
  1167. if (long_bhs && IS_DIRSYNC(dir))
  1168. err = fat_sync_bhs(bhs, long_bhs);
  1169. if (!err && i < nr_bhs) {
  1170. /* Fill the short name slot. */
  1171. int copy = min_t(int, sb->s_blocksize - offset, size);
  1172. memcpy(bhs[i]->b_data + offset, slots, copy);
  1173. mark_buffer_dirty_inode(bhs[i], dir);
  1174. if (IS_DIRSYNC(dir))
  1175. err = sync_dirty_buffer(bhs[i]);
  1176. }
  1177. for (i = 0; i < nr_bhs; i++)
  1178. brelse(bhs[i]);
  1179. if (err)
  1180. goto error_remove;
  1181. }
  1182. if (nr_slots) {
  1183. int cluster, nr_cluster;
  1184. /*
  1185. * Third stage: allocate the cluster for new entries.
  1186. * And initialize the cluster with new entries, then
  1187. * add the cluster to dir.
  1188. */
  1189. cluster = fat_add_new_entries(dir, slots, nr_slots, &nr_cluster,
  1190. &de, &bh, &i_pos);
  1191. if (cluster < 0) {
  1192. err = cluster;
  1193. goto error_remove;
  1194. }
  1195. err = fat_chain_add(dir, cluster, nr_cluster);
  1196. if (err) {
  1197. fat_free_clusters(dir, cluster);
  1198. goto error_remove;
  1199. }
  1200. if (dir->i_size & (sbi->cluster_size - 1)) {
  1201. fat_fs_error(sb, "Odd directory size");
  1202. dir->i_size = (dir->i_size + sbi->cluster_size - 1)
  1203. & ~((loff_t)sbi->cluster_size - 1);
  1204. }
  1205. dir->i_size += nr_cluster << sbi->cluster_bits;
  1206. MSDOS_I(dir)->mmu_private += nr_cluster << sbi->cluster_bits;
  1207. }
  1208. sinfo->slot_off = pos;
  1209. sinfo->de = de;
  1210. sinfo->bh = bh;
  1211. sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
  1212. return 0;
  1213. error:
  1214. brelse(bh);
  1215. for (i = 0; i < nr_bhs; i++)
  1216. brelse(bhs[i]);
  1217. return err;
  1218. error_remove:
  1219. brelse(bh);
  1220. if (free_slots)
  1221. __fat_remove_entries(dir, pos, free_slots);
  1222. return err;
  1223. }
  1224. EXPORT_SYMBOL_GPL(fat_add_entries);