dir.c 35 KB

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