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. static inline loff_t fat_make_i_pos(struct super_block *sb,
  24. struct buffer_head *bh,
  25. struct msdos_dir_entry *de)
  26. {
  27. return ((loff_t)bh->b_blocknr << MSDOS_SB(sb)->dir_per_block_bits)
  28. | (de - (struct msdos_dir_entry *)bh->b_data);
  29. }
  30. static inline void fat_dir_readahead(struct inode *dir, sector_t iblock,
  31. sector_t phys)
  32. {
  33. struct super_block *sb = dir->i_sb;
  34. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  35. struct buffer_head *bh;
  36. int sec;
  37. /* This is not a first sector of cluster, or sec_per_clus == 1 */
  38. if ((iblock & (sbi->sec_per_clus - 1)) || sbi->sec_per_clus == 1)
  39. return;
  40. /* root dir of FAT12/FAT16 */
  41. if ((sbi->fat_bits != 32) && (dir->i_ino == MSDOS_ROOT_INO))
  42. return;
  43. bh = sb_find_get_block(sb, phys);
  44. if (bh == NULL || !buffer_uptodate(bh)) {
  45. for (sec = 0; sec < sbi->sec_per_clus; sec++)
  46. sb_breadahead(sb, phys + sec);
  47. }
  48. brelse(bh);
  49. }
  50. /* Returns the inode number of the directory entry at offset pos. If bh is
  51. non-NULL, it is brelse'd before. Pos is incremented. The buffer header is
  52. returned in bh.
  53. AV. Most often we do it item-by-item. Makes sense to optimize.
  54. AV. OK, there we go: if both bh and de are non-NULL we assume that we just
  55. AV. want the next entry (took one explicit de=NULL in vfat/namei.c).
  56. AV. It's done in fat_get_entry() (inlined), here the slow case lives.
  57. AV. Additionally, when we return -1 (i.e. reached the end of directory)
  58. AV. we make bh NULL.
  59. */
  60. static int fat__get_entry(struct inode *dir, loff_t *pos,
  61. struct buffer_head **bh, struct msdos_dir_entry **de)
  62. {
  63. struct super_block *sb = dir->i_sb;
  64. sector_t phys, iblock;
  65. unsigned long mapped_blocks;
  66. int err, offset;
  67. next:
  68. if (*bh)
  69. brelse(*bh);
  70. *bh = NULL;
  71. iblock = *pos >> sb->s_blocksize_bits;
  72. err = fat_bmap(dir, iblock, &phys, &mapped_blocks, 0);
  73. if (err || !phys)
  74. return -1; /* beyond EOF or error */
  75. fat_dir_readahead(dir, iblock, phys);
  76. *bh = sb_bread(sb, phys);
  77. if (*bh == NULL) {
  78. printk(KERN_ERR "FAT: Directory bread(block %llu) failed\n",
  79. (llu)phys);
  80. /* skip this block */
  81. *pos = (iblock + 1) << sb->s_blocksize_bits;
  82. goto next;
  83. }
  84. offset = *pos & (sb->s_blocksize - 1);
  85. *pos += sizeof(struct msdos_dir_entry);
  86. *de = (struct msdos_dir_entry *)((*bh)->b_data + offset);
  87. return 0;
  88. }
  89. static inline int fat_get_entry(struct inode *dir, loff_t *pos,
  90. struct buffer_head **bh,
  91. struct msdos_dir_entry **de)
  92. {
  93. /* Fast stuff first */
  94. if (*bh && *de &&
  95. (*de - (struct msdos_dir_entry *)(*bh)->b_data) < MSDOS_SB(dir->i_sb)->dir_per_block - 1) {
  96. *pos += sizeof(struct msdos_dir_entry);
  97. (*de)++;
  98. return 0;
  99. }
  100. return fat__get_entry(dir, pos, bh, de);
  101. }
  102. /*
  103. * Convert Unicode 16 to UTF-8, translated Unicode, or ASCII.
  104. * If uni_xlate is enabled and we can't get a 1:1 conversion, use a
  105. * colon as an escape character since it is normally invalid on the vfat
  106. * filesystem. The following four characters are the hexadecimal digits
  107. * of Unicode value. This lets us do a full dump and restore of Unicode
  108. * filenames. We could get into some trouble with long Unicode names,
  109. * but ignore that right now.
  110. * Ahem... Stack smashing in ring 0 isn't fun. Fixed.
  111. */
  112. static int uni16_to_x8(unsigned char *ascii, const wchar_t *uni, int len,
  113. int uni_xlate, struct nls_table *nls)
  114. {
  115. const wchar_t *ip;
  116. wchar_t ec;
  117. unsigned char *op, nc;
  118. int charlen;
  119. int k;
  120. ip = uni;
  121. op = ascii;
  122. while (*ip && ((len - NLS_MAX_CHARSET_SIZE) > 0)) {
  123. ec = *ip++;
  124. if ( (charlen = nls->uni2char(ec, op, NLS_MAX_CHARSET_SIZE)) > 0) {
  125. op += charlen;
  126. len -= charlen;
  127. } else {
  128. if (uni_xlate == 1) {
  129. *op = ':';
  130. for (k = 4; k > 0; k--) {
  131. nc = ec & 0xF;
  132. op[k] = nc > 9 ? nc + ('a' - 10)
  133. : nc + '0';
  134. ec >>= 4;
  135. }
  136. op += 5;
  137. len -= 5;
  138. } else {
  139. *op++ = '?';
  140. len--;
  141. }
  142. }
  143. }
  144. if (unlikely(*ip)) {
  145. printk(KERN_WARNING "FAT: filename was truncated while "
  146. "converting.");
  147. }
  148. *op = 0;
  149. return (op - ascii);
  150. }
  151. static inline int fat_uni_to_x8(struct msdos_sb_info *sbi, const wchar_t *uni,
  152. unsigned char *buf, int size)
  153. {
  154. if (sbi->options.utf8)
  155. return utf8_wcstombs(buf, uni, size);
  156. else
  157. return uni16_to_x8(buf, uni, size, sbi->options.unicode_xlate,
  158. sbi->nls_io);
  159. }
  160. static inline int
  161. fat_short2uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
  162. {
  163. int charlen;
  164. charlen = t->char2uni(c, clen, uni);
  165. if (charlen < 0) {
  166. *uni = 0x003f; /* a question mark */
  167. charlen = 1;
  168. }
  169. return charlen;
  170. }
  171. static inline int
  172. fat_short2lower_uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
  173. {
  174. int charlen;
  175. wchar_t wc;
  176. charlen = t->char2uni(c, clen, &wc);
  177. if (charlen < 0) {
  178. *uni = 0x003f; /* a question mark */
  179. charlen = 1;
  180. } else if (charlen <= 1) {
  181. unsigned char nc = t->charset2lower[*c];
  182. if (!nc)
  183. nc = *c;
  184. if ( (charlen = t->char2uni(&nc, 1, uni)) < 0) {
  185. *uni = 0x003f; /* a question mark */
  186. charlen = 1;
  187. }
  188. } else
  189. *uni = wc;
  190. return charlen;
  191. }
  192. static inline int
  193. fat_shortname2uni(struct nls_table *nls, unsigned char *buf, int buf_size,
  194. wchar_t *uni_buf, unsigned short opt, int lower)
  195. {
  196. int len = 0;
  197. if (opt & VFAT_SFN_DISPLAY_LOWER)
  198. len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
  199. else if (opt & VFAT_SFN_DISPLAY_WIN95)
  200. len = fat_short2uni(nls, buf, buf_size, uni_buf);
  201. else if (opt & VFAT_SFN_DISPLAY_WINNT) {
  202. if (lower)
  203. len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
  204. else
  205. len = fat_short2uni(nls, buf, buf_size, uni_buf);
  206. } else
  207. len = fat_short2uni(nls, buf, buf_size, uni_buf);
  208. return len;
  209. }
  210. static inline int fat_name_match(struct msdos_sb_info *sbi,
  211. const unsigned char *a, int a_len,
  212. const unsigned char *b, int b_len)
  213. {
  214. if (a_len != b_len)
  215. return 0;
  216. if (sbi->options.name_check != 's')
  217. return !nls_strnicmp(sbi->nls_io, a, b, a_len);
  218. else
  219. return !memcmp(a, b, a_len);
  220. }
  221. enum { PARSE_INVALID = 1, PARSE_NOT_LONGNAME, PARSE_EOF, };
  222. /**
  223. * fat_parse_long - Parse extended directory entry.
  224. *
  225. * This function returns zero on success, negative value on error, or one of
  226. * the following:
  227. *
  228. * %PARSE_INVALID - Directory entry is invalid.
  229. * %PARSE_NOT_LONGNAME - Directory entry does not contain longname.
  230. * %PARSE_EOF - Directory has no more entries.
  231. */
  232. static int fat_parse_long(struct inode *dir, loff_t *pos,
  233. struct buffer_head **bh, struct msdos_dir_entry **de,
  234. wchar_t **unicode, unsigned char *nr_slots)
  235. {
  236. struct msdos_dir_slot *ds;
  237. unsigned char id, slot, slots, alias_checksum;
  238. if (!*unicode) {
  239. *unicode = __getname();
  240. if (!*unicode) {
  241. brelse(*bh);
  242. return -ENOMEM;
  243. }
  244. }
  245. parse_long:
  246. slots = 0;
  247. ds = (struct msdos_dir_slot *)*de;
  248. id = ds->id;
  249. if (!(id & 0x40))
  250. return PARSE_INVALID;
  251. slots = id & ~0x40;
  252. if (slots > 20 || !slots) /* ceil(256 * 2 / 26) */
  253. return PARSE_INVALID;
  254. *nr_slots = slots;
  255. alias_checksum = ds->alias_checksum;
  256. slot = slots;
  257. while (1) {
  258. int offset;
  259. slot--;
  260. offset = slot * 13;
  261. fat16_towchar(*unicode + offset, ds->name0_4, 5);
  262. fat16_towchar(*unicode + offset + 5, ds->name5_10, 6);
  263. fat16_towchar(*unicode + offset + 11, ds->name11_12, 2);
  264. if (ds->id & 0x40)
  265. (*unicode)[offset + 13] = 0;
  266. if (fat_get_entry(dir, pos, bh, de) < 0)
  267. return PARSE_EOF;
  268. if (slot == 0)
  269. break;
  270. ds = (struct msdos_dir_slot *)*de;
  271. if (ds->attr != ATTR_EXT)
  272. return PARSE_NOT_LONGNAME;
  273. if ((ds->id & ~0x40) != slot)
  274. goto parse_long;
  275. if (ds->alias_checksum != alias_checksum)
  276. goto parse_long;
  277. }
  278. if ((*de)->name[0] == DELETED_FLAG)
  279. return PARSE_INVALID;
  280. if ((*de)->attr == ATTR_EXT)
  281. goto parse_long;
  282. if (IS_FREE((*de)->name) || ((*de)->attr & ATTR_VOLUME))
  283. return PARSE_INVALID;
  284. if (fat_checksum((*de)->name) != alias_checksum)
  285. *nr_slots = 0;
  286. return 0;
  287. }
  288. /*
  289. * Maximum buffer size of short name.
  290. * [(MSDOS_NAME + '.') * max one char + nul]
  291. * For msdos style, ['.' (hidden) + MSDOS_NAME + '.' + nul]
  292. */
  293. #define FAT_MAX_SHORT_SIZE ((MSDOS_NAME + 1) * NLS_MAX_CHARSET_SIZE + 1)
  294. /*
  295. * Maximum buffer size of unicode chars from slots.
  296. * [(max longname slots * 13 (size in a slot) + nul) * sizeof(wchar_t)]
  297. */
  298. #define FAT_MAX_UNI_CHARS ((MSDOS_SLOTS - 1) * 13 + 1)
  299. #define FAT_MAX_UNI_SIZE (FAT_MAX_UNI_CHARS * sizeof(wchar_t))
  300. /*
  301. * Return values: negative -> error, 0 -> not found, positive -> found,
  302. * value is the total amount of slots, including the shortname entry.
  303. */
  304. int fat_search_long(struct inode *inode, const unsigned char *name,
  305. int name_len, struct fat_slot_info *sinfo)
  306. {
  307. struct super_block *sb = inode->i_sb;
  308. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  309. struct buffer_head *bh = NULL;
  310. struct msdos_dir_entry *de;
  311. struct nls_table *nls_disk = sbi->nls_disk;
  312. unsigned char nr_slots;
  313. wchar_t bufuname[14];
  314. wchar_t *unicode = NULL;
  315. unsigned char work[MSDOS_NAME];
  316. unsigned char bufname[FAT_MAX_SHORT_SIZE];
  317. unsigned short opt_shortname = sbi->options.shortname;
  318. loff_t cpos = 0;
  319. int chl, i, j, last_u, err, len;
  320. err = -ENOENT;
  321. while (1) {
  322. if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
  323. goto end_of_dir;
  324. parse_record:
  325. nr_slots = 0;
  326. if (de->name[0] == DELETED_FLAG)
  327. continue;
  328. if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
  329. continue;
  330. if (de->attr != ATTR_EXT && IS_FREE(de->name))
  331. continue;
  332. if (de->attr == ATTR_EXT) {
  333. int status = fat_parse_long(inode, &cpos, &bh, &de,
  334. &unicode, &nr_slots);
  335. if (status < 0) {
  336. err = status;
  337. goto end_of_dir;
  338. } else if (status == PARSE_INVALID)
  339. continue;
  340. else if (status == PARSE_NOT_LONGNAME)
  341. goto parse_record;
  342. else if (status == PARSE_EOF)
  343. goto end_of_dir;
  344. }
  345. memcpy(work, de->name, sizeof(de->name));
  346. /* see namei.c, msdos_format_name */
  347. if (work[0] == 0x05)
  348. work[0] = 0xE5;
  349. for (i = 0, j = 0, last_u = 0; i < 8;) {
  350. if (!work[i])
  351. break;
  352. chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
  353. &bufuname[j++], opt_shortname,
  354. de->lcase & CASE_LOWER_BASE);
  355. if (chl <= 1) {
  356. if (work[i] != ' ')
  357. last_u = j;
  358. } else {
  359. last_u = j;
  360. }
  361. i += chl;
  362. }
  363. j = last_u;
  364. fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
  365. for (i = 8; i < MSDOS_NAME;) {
  366. if (!work[i])
  367. break;
  368. chl = fat_shortname2uni(nls_disk, &work[i],
  369. MSDOS_NAME - i,
  370. &bufuname[j++], opt_shortname,
  371. de->lcase & CASE_LOWER_EXT);
  372. if (chl <= 1) {
  373. if (work[i] != ' ')
  374. last_u = j;
  375. } else {
  376. last_u = j;
  377. }
  378. i += chl;
  379. }
  380. if (!last_u)
  381. continue;
  382. /* Compare shortname */
  383. bufuname[last_u] = 0x0000;
  384. len = fat_uni_to_x8(sbi, bufuname, bufname, sizeof(bufname));
  385. if (fat_name_match(sbi, name, name_len, bufname, len))
  386. goto found;
  387. if (nr_slots) {
  388. void *longname = unicode + FAT_MAX_UNI_CHARS;
  389. int size = PATH_MAX - FAT_MAX_UNI_SIZE;
  390. /* Compare longname */
  391. len = fat_uni_to_x8(sbi, unicode, longname, size);
  392. if (fat_name_match(sbi, name, name_len, longname, len))
  393. goto found;
  394. }
  395. }
  396. found:
  397. nr_slots++; /* include the de */
  398. sinfo->slot_off = cpos - nr_slots * sizeof(*de);
  399. sinfo->nr_slots = nr_slots;
  400. sinfo->de = de;
  401. sinfo->bh = bh;
  402. sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
  403. err = 0;
  404. end_of_dir:
  405. if (unicode)
  406. __putname(unicode);
  407. return err;
  408. }
  409. EXPORT_SYMBOL_GPL(fat_search_long);
  410. struct fat_ioctl_filldir_callback {
  411. void __user *dirent;
  412. int result;
  413. /* for dir ioctl */
  414. const char *longname;
  415. int long_len;
  416. const char *shortname;
  417. int short_len;
  418. };
  419. static int __fat_readdir(struct inode *inode, struct file *filp, void *dirent,
  420. filldir_t filldir, int short_only, int both)
  421. {
  422. struct super_block *sb = inode->i_sb;
  423. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  424. struct buffer_head *bh;
  425. struct msdos_dir_entry *de;
  426. struct nls_table *nls_disk = sbi->nls_disk;
  427. unsigned char nr_slots;
  428. wchar_t bufuname[14];
  429. wchar_t *unicode = NULL;
  430. unsigned char c, work[MSDOS_NAME];
  431. unsigned char bufname[FAT_MAX_SHORT_SIZE], *ptname = bufname;
  432. unsigned short opt_shortname = sbi->options.shortname;
  433. int isvfat = sbi->options.isvfat;
  434. int nocase = sbi->options.nocase;
  435. const char *fill_name = NULL;
  436. unsigned long inum;
  437. unsigned long lpos, dummy, *furrfu = &lpos;
  438. loff_t cpos;
  439. int chi, chl, i, i2, j, last, last_u, dotoffset = 0, fill_len = 0;
  440. int ret = 0;
  441. lock_super(sb);
  442. cpos = filp->f_pos;
  443. /* Fake . and .. for the root directory. */
  444. if (inode->i_ino == MSDOS_ROOT_INO) {
  445. while (cpos < 2) {
  446. if (filldir(dirent, "..", cpos+1, cpos, MSDOS_ROOT_INO, DT_DIR) < 0)
  447. goto out;
  448. cpos++;
  449. filp->f_pos++;
  450. }
  451. if (cpos == 2) {
  452. dummy = 2;
  453. furrfu = &dummy;
  454. cpos = 0;
  455. }
  456. }
  457. if (cpos & (sizeof(struct msdos_dir_entry) - 1)) {
  458. ret = -ENOENT;
  459. goto out;
  460. }
  461. bh = NULL;
  462. get_new:
  463. if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
  464. goto end_of_dir;
  465. parse_record:
  466. nr_slots = 0;
  467. /*
  468. * Check for long filename entry, but if short_only, we don't
  469. * need to parse long filename.
  470. */
  471. if (isvfat && !short_only) {
  472. if (de->name[0] == DELETED_FLAG)
  473. goto record_end;
  474. if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
  475. goto record_end;
  476. if (de->attr != ATTR_EXT && IS_FREE(de->name))
  477. goto record_end;
  478. } else {
  479. if ((de->attr & ATTR_VOLUME) || IS_FREE(de->name))
  480. goto record_end;
  481. }
  482. if (isvfat && de->attr == ATTR_EXT) {
  483. int status = fat_parse_long(inode, &cpos, &bh, &de,
  484. &unicode, &nr_slots);
  485. if (status < 0) {
  486. filp->f_pos = cpos;
  487. ret = status;
  488. goto out;
  489. } else if (status == PARSE_INVALID)
  490. goto record_end;
  491. else if (status == PARSE_NOT_LONGNAME)
  492. goto parse_record;
  493. else if (status == PARSE_EOF)
  494. goto end_of_dir;
  495. if (nr_slots) {
  496. void *longname = unicode + FAT_MAX_UNI_CHARS;
  497. int size = PATH_MAX - FAT_MAX_UNI_SIZE;
  498. int len = fat_uni_to_x8(sbi, unicode, longname, size);
  499. fill_name = longname;
  500. fill_len = len;
  501. /* !both && !short_only, so we don't need shortname. */
  502. if (!both)
  503. goto start_filldir;
  504. }
  505. }
  506. if (sbi->options.dotsOK) {
  507. ptname = bufname;
  508. dotoffset = 0;
  509. if (de->attr & ATTR_HIDDEN) {
  510. *ptname++ = '.';
  511. dotoffset = 1;
  512. }
  513. }
  514. memcpy(work, de->name, sizeof(de->name));
  515. /* see namei.c, msdos_format_name */
  516. if (work[0] == 0x05)
  517. work[0] = 0xE5;
  518. for (i = 0, j = 0, last = 0, last_u = 0; i < 8;) {
  519. if (!(c = work[i]))
  520. break;
  521. chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
  522. &bufuname[j++], opt_shortname,
  523. de->lcase & CASE_LOWER_BASE);
  524. if (chl <= 1) {
  525. ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
  526. if (c != ' ') {
  527. last = i;
  528. last_u = j;
  529. }
  530. } else {
  531. last_u = j;
  532. for (chi = 0; chi < chl && i < 8; chi++) {
  533. ptname[i] = work[i];
  534. i++; last = i;
  535. }
  536. }
  537. }
  538. i = last;
  539. j = last_u;
  540. fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
  541. ptname[i++] = '.';
  542. for (i2 = 8; i2 < MSDOS_NAME;) {
  543. if (!(c = work[i2]))
  544. break;
  545. chl = fat_shortname2uni(nls_disk, &work[i2], MSDOS_NAME - i2,
  546. &bufuname[j++], opt_shortname,
  547. de->lcase & CASE_LOWER_EXT);
  548. if (chl <= 1) {
  549. i2++;
  550. ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
  551. if (c != ' ') {
  552. last = i;
  553. last_u = j;
  554. }
  555. } else {
  556. last_u = j;
  557. for (chi = 0; chi < chl && i2 < MSDOS_NAME; chi++) {
  558. ptname[i++] = work[i2++];
  559. last = i;
  560. }
  561. }
  562. }
  563. if (!last)
  564. goto record_end;
  565. i = last + dotoffset;
  566. j = last_u;
  567. if (isvfat) {
  568. bufuname[j] = 0x0000;
  569. i = fat_uni_to_x8(sbi, bufuname, bufname, sizeof(bufname));
  570. }
  571. if (nr_slots) {
  572. /* hack for fat_ioctl_filldir() */
  573. struct fat_ioctl_filldir_callback *p = dirent;
  574. p->longname = fill_name;
  575. p->long_len = fill_len;
  576. p->shortname = bufname;
  577. p->short_len = i;
  578. fill_name = NULL;
  579. fill_len = 0;
  580. } else {
  581. fill_name = bufname;
  582. fill_len = i;
  583. }
  584. start_filldir:
  585. lpos = cpos - (nr_slots + 1) * sizeof(struct msdos_dir_entry);
  586. if (!memcmp(de->name, MSDOS_DOT, MSDOS_NAME))
  587. inum = inode->i_ino;
  588. else if (!memcmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
  589. inum = parent_ino(filp->f_path.dentry);
  590. } else {
  591. loff_t i_pos = fat_make_i_pos(sb, bh, de);
  592. struct inode *tmp = fat_iget(sb, i_pos);
  593. if (tmp) {
  594. inum = tmp->i_ino;
  595. iput(tmp);
  596. } else
  597. inum = iunique(sb, MSDOS_ROOT_INO);
  598. }
  599. if (filldir(dirent, fill_name, fill_len, *furrfu, inum,
  600. (de->attr & ATTR_DIR) ? DT_DIR : DT_REG) < 0)
  601. goto fill_failed;
  602. record_end:
  603. furrfu = &lpos;
  604. filp->f_pos = cpos;
  605. goto get_new;
  606. end_of_dir:
  607. filp->f_pos = cpos;
  608. fill_failed:
  609. brelse(bh);
  610. if (unicode)
  611. __putname(unicode);
  612. out:
  613. unlock_super(sb);
  614. return ret;
  615. }
  616. static int fat_readdir(struct file *filp, void *dirent, filldir_t filldir)
  617. {
  618. struct inode *inode = filp->f_path.dentry->d_inode;
  619. return __fat_readdir(inode, filp, dirent, filldir, 0, 0);
  620. }
  621. #define FAT_IOCTL_FILLDIR_FUNC(func, dirent_type) \
  622. static int func(void *__buf, const char *name, int name_len, \
  623. loff_t offset, u64 ino, unsigned int d_type) \
  624. { \
  625. struct fat_ioctl_filldir_callback *buf = __buf; \
  626. struct dirent_type __user *d1 = buf->dirent; \
  627. struct dirent_type __user *d2 = d1 + 1; \
  628. \
  629. if (buf->result) \
  630. return -EINVAL; \
  631. buf->result++; \
  632. \
  633. if (name != NULL) { \
  634. /* dirent has only short name */ \
  635. if (name_len >= sizeof(d1->d_name)) \
  636. name_len = sizeof(d1->d_name) - 1; \
  637. \
  638. if (put_user(0, d2->d_name) || \
  639. put_user(0, &d2->d_reclen) || \
  640. copy_to_user(d1->d_name, name, name_len) || \
  641. put_user(0, d1->d_name + name_len) || \
  642. put_user(name_len, &d1->d_reclen)) \
  643. goto efault; \
  644. } else { \
  645. /* dirent has short and long name */ \
  646. const char *longname = buf->longname; \
  647. int long_len = buf->long_len; \
  648. const char *shortname = buf->shortname; \
  649. int short_len = buf->short_len; \
  650. \
  651. if (long_len >= sizeof(d1->d_name)) \
  652. long_len = sizeof(d1->d_name) - 1; \
  653. if (short_len >= sizeof(d1->d_name)) \
  654. short_len = sizeof(d1->d_name) - 1; \
  655. \
  656. if (copy_to_user(d2->d_name, longname, long_len) || \
  657. put_user(0, d2->d_name + long_len) || \
  658. put_user(long_len, &d2->d_reclen) || \
  659. put_user(ino, &d2->d_ino) || \
  660. put_user(offset, &d2->d_off) || \
  661. copy_to_user(d1->d_name, shortname, short_len) || \
  662. put_user(0, d1->d_name + short_len) || \
  663. put_user(short_len, &d1->d_reclen)) \
  664. goto efault; \
  665. } \
  666. return 0; \
  667. efault: \
  668. buf->result = -EFAULT; \
  669. return -EFAULT; \
  670. }
  671. FAT_IOCTL_FILLDIR_FUNC(fat_ioctl_filldir, __fat_dirent)
  672. static int fat_ioctl_readdir(struct inode *inode, struct file *filp,
  673. void __user *dirent, filldir_t filldir,
  674. int short_only, int both)
  675. {
  676. struct fat_ioctl_filldir_callback buf;
  677. int ret;
  678. buf.dirent = dirent;
  679. buf.result = 0;
  680. mutex_lock(&inode->i_mutex);
  681. ret = -ENOENT;
  682. if (!IS_DEADDIR(inode)) {
  683. ret = __fat_readdir(inode, filp, &buf, filldir,
  684. short_only, both);
  685. }
  686. mutex_unlock(&inode->i_mutex);
  687. if (ret >= 0)
  688. ret = buf.result;
  689. return ret;
  690. }
  691. static int fat_dir_ioctl(struct inode *inode, struct file *filp,
  692. unsigned int cmd, unsigned long arg)
  693. {
  694. struct __fat_dirent __user *d1 = (struct __fat_dirent __user *)arg;
  695. int short_only, both;
  696. switch (cmd) {
  697. case VFAT_IOCTL_READDIR_SHORT:
  698. short_only = 1;
  699. both = 0;
  700. break;
  701. case VFAT_IOCTL_READDIR_BOTH:
  702. short_only = 0;
  703. both = 1;
  704. break;
  705. default:
  706. return fat_generic_ioctl(inode, filp, cmd, arg);
  707. }
  708. if (!access_ok(VERIFY_WRITE, d1, sizeof(struct __fat_dirent[2])))
  709. return -EFAULT;
  710. /*
  711. * Yes, we don't need this put_user() absolutely. However old
  712. * code didn't return the right value. So, app use this value,
  713. * in order to check whether it is EOF.
  714. */
  715. if (put_user(0, &d1->d_reclen))
  716. return -EFAULT;
  717. return fat_ioctl_readdir(inode, filp, d1, fat_ioctl_filldir,
  718. short_only, both);
  719. }
  720. #ifdef CONFIG_COMPAT
  721. #define VFAT_IOCTL_READDIR_BOTH32 _IOR('r', 1, struct compat_dirent[2])
  722. #define VFAT_IOCTL_READDIR_SHORT32 _IOR('r', 2, struct compat_dirent[2])
  723. FAT_IOCTL_FILLDIR_FUNC(fat_compat_ioctl_filldir, compat_dirent)
  724. static long fat_compat_dir_ioctl(struct file *filp, unsigned cmd,
  725. unsigned long arg)
  726. {
  727. struct inode *inode = filp->f_path.dentry->d_inode;
  728. struct compat_dirent __user *d1 = compat_ptr(arg);
  729. int short_only, both;
  730. switch (cmd) {
  731. case VFAT_IOCTL_READDIR_SHORT32:
  732. short_only = 1;
  733. both = 0;
  734. break;
  735. case VFAT_IOCTL_READDIR_BOTH32:
  736. short_only = 0;
  737. both = 1;
  738. break;
  739. default:
  740. return -ENOIOCTLCMD;
  741. }
  742. if (!access_ok(VERIFY_WRITE, d1, sizeof(struct compat_dirent[2])))
  743. return -EFAULT;
  744. /*
  745. * Yes, we don't need this put_user() absolutely. However old
  746. * code didn't return the right value. So, app use this value,
  747. * in order to check whether it is EOF.
  748. */
  749. if (put_user(0, &d1->d_reclen))
  750. return -EFAULT;
  751. return fat_ioctl_readdir(inode, filp, d1, fat_compat_ioctl_filldir,
  752. short_only, both);
  753. }
  754. #endif /* CONFIG_COMPAT */
  755. const struct file_operations fat_dir_operations = {
  756. .llseek = generic_file_llseek,
  757. .read = generic_read_dir,
  758. .readdir = fat_readdir,
  759. .ioctl = fat_dir_ioctl,
  760. #ifdef CONFIG_COMPAT
  761. .compat_ioctl = fat_compat_dir_ioctl,
  762. #endif
  763. .fsync = file_fsync,
  764. .llseek = generic_file_llseek,
  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(bh);
  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(bh);
  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(bhs[n]);
  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(bhs[0]);
  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(bhs[n]);
  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(bhs[i]);
  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(bhs[i]);
  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_panic(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);