dir.c 34 KB

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