dir.c 34 KB

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