dir.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409
  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, false);
  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. bh = NULL;
  551. ret = status;
  552. goto end_of_dir;
  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. ctx->pos = cpos - (nr_slots + 1) * sizeof(struct msdos_dir_entry);
  589. if (fake_offset && ctx->pos < 2)
  590. ctx->pos = 2;
  591. if (!memcmp(de->name, MSDOS_DOT, MSDOS_NAME)) {
  592. if (!dir_emit_dot(file, ctx))
  593. goto fill_failed;
  594. } else if (!memcmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
  595. if (!dir_emit_dotdot(file, ctx))
  596. goto fill_failed;
  597. } else {
  598. unsigned long inum;
  599. loff_t i_pos = fat_make_i_pos(sb, bh, de);
  600. struct inode *tmp = fat_iget(sb, i_pos);
  601. if (tmp) {
  602. inum = tmp->i_ino;
  603. iput(tmp);
  604. } else
  605. inum = iunique(sb, MSDOS_ROOT_INO);
  606. if (!dir_emit(ctx, fill_name, fill_len, inum,
  607. (de->attr & ATTR_DIR) ? DT_DIR : DT_REG))
  608. goto fill_failed;
  609. }
  610. record_end:
  611. fake_offset = 0;
  612. ctx->pos = cpos;
  613. goto get_new;
  614. end_of_dir:
  615. if (fake_offset && cpos < 2)
  616. ctx->pos = 2;
  617. else
  618. ctx->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 *file, struct dir_context *ctx)
  628. {
  629. return __fat_readdir(file_inode(file), file, ctx, 0, NULL);
  630. }
  631. #define FAT_IOCTL_FILLDIR_FUNC(func, dirent_type) \
  632. static int func(struct dir_context *ctx, const char *name, int name_len, \
  633. loff_t offset, u64 ino, unsigned int d_type) \
  634. { \
  635. struct fat_ioctl_filldir_callback *buf = \
  636. container_of(ctx, struct fat_ioctl_filldir_callback, ctx); \
  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 *file,
  684. void __user *dirent, filldir_t filldir,
  685. int short_only, int both)
  686. {
  687. struct fat_ioctl_filldir_callback buf = {
  688. .ctx.actor = filldir,
  689. .dirent = dirent
  690. };
  691. int ret;
  692. buf.dirent = dirent;
  693. buf.result = 0;
  694. inode_lock_shared(inode);
  695. buf.ctx.pos = file->f_pos;
  696. ret = -ENOENT;
  697. if (!IS_DEADDIR(inode)) {
  698. ret = __fat_readdir(inode, file, &buf.ctx,
  699. short_only, both ? &buf : NULL);
  700. file->f_pos = buf.ctx.pos;
  701. }
  702. inode_unlock_shared(inode);
  703. if (ret >= 0)
  704. ret = buf.result;
  705. return ret;
  706. }
  707. static long fat_dir_ioctl(struct file *filp, unsigned int cmd,
  708. unsigned long arg)
  709. {
  710. struct inode *inode = file_inode(filp);
  711. struct __fat_dirent __user *d1 = (struct __fat_dirent __user *)arg;
  712. int short_only, both;
  713. switch (cmd) {
  714. case VFAT_IOCTL_READDIR_SHORT:
  715. short_only = 1;
  716. both = 0;
  717. break;
  718. case VFAT_IOCTL_READDIR_BOTH:
  719. short_only = 0;
  720. both = 1;
  721. break;
  722. default:
  723. return fat_generic_ioctl(filp, cmd, arg);
  724. }
  725. if (!access_ok(VERIFY_WRITE, d1, sizeof(struct __fat_dirent[2])))
  726. return -EFAULT;
  727. /*
  728. * Yes, we don't need this put_user() absolutely. However old
  729. * code didn't return the right value. So, app use this value,
  730. * in order to check whether it is EOF.
  731. */
  732. if (put_user(0, &d1->d_reclen))
  733. return -EFAULT;
  734. return fat_ioctl_readdir(inode, filp, d1, fat_ioctl_filldir,
  735. short_only, both);
  736. }
  737. #ifdef CONFIG_COMPAT
  738. #define VFAT_IOCTL_READDIR_BOTH32 _IOR('r', 1, struct compat_dirent[2])
  739. #define VFAT_IOCTL_READDIR_SHORT32 _IOR('r', 2, struct compat_dirent[2])
  740. FAT_IOCTL_FILLDIR_FUNC(fat_compat_ioctl_filldir, compat_dirent)
  741. static long fat_compat_dir_ioctl(struct file *filp, unsigned cmd,
  742. unsigned long arg)
  743. {
  744. struct inode *inode = file_inode(filp);
  745. struct compat_dirent __user *d1 = compat_ptr(arg);
  746. int short_only, both;
  747. switch (cmd) {
  748. case VFAT_IOCTL_READDIR_SHORT32:
  749. short_only = 1;
  750. both = 0;
  751. break;
  752. case VFAT_IOCTL_READDIR_BOTH32:
  753. short_only = 0;
  754. both = 1;
  755. break;
  756. default:
  757. return fat_generic_ioctl(filp, cmd, (unsigned long)arg);
  758. }
  759. if (!access_ok(VERIFY_WRITE, d1, sizeof(struct compat_dirent[2])))
  760. return -EFAULT;
  761. /*
  762. * Yes, we don't need this put_user() absolutely. However old
  763. * code didn't return the right value. So, app use this value,
  764. * in order to check whether it is EOF.
  765. */
  766. if (put_user(0, &d1->d_reclen))
  767. return -EFAULT;
  768. return fat_ioctl_readdir(inode, filp, d1, fat_compat_ioctl_filldir,
  769. short_only, both);
  770. }
  771. #endif /* CONFIG_COMPAT */
  772. const struct file_operations fat_dir_operations = {
  773. .llseek = generic_file_llseek,
  774. .read = generic_read_dir,
  775. .iterate_shared = fat_readdir,
  776. .unlocked_ioctl = fat_dir_ioctl,
  777. #ifdef CONFIG_COMPAT
  778. .compat_ioctl = fat_compat_dir_ioctl,
  779. #endif
  780. .fsync = fat_file_fsync,
  781. };
  782. static int fat_get_short_entry(struct inode *dir, loff_t *pos,
  783. struct buffer_head **bh,
  784. struct msdos_dir_entry **de)
  785. {
  786. while (fat_get_entry(dir, pos, bh, de) >= 0) {
  787. /* free entry or long name entry or volume label */
  788. if (!IS_FREE((*de)->name) && !((*de)->attr & ATTR_VOLUME))
  789. return 0;
  790. }
  791. return -ENOENT;
  792. }
  793. /*
  794. * The ".." entry can not provide the "struct fat_slot_info" information
  795. * for inode, nor a usable i_pos. So, this function provides some information
  796. * only.
  797. *
  798. * Since this function walks through the on-disk inodes within a directory,
  799. * callers are responsible for taking any locks necessary to prevent the
  800. * directory from changing.
  801. */
  802. int fat_get_dotdot_entry(struct inode *dir, struct buffer_head **bh,
  803. struct msdos_dir_entry **de)
  804. {
  805. loff_t offset = 0;
  806. *de = NULL;
  807. while (fat_get_short_entry(dir, &offset, bh, de) >= 0) {
  808. if (!strncmp((*de)->name, MSDOS_DOTDOT, MSDOS_NAME))
  809. return 0;
  810. }
  811. return -ENOENT;
  812. }
  813. EXPORT_SYMBOL_GPL(fat_get_dotdot_entry);
  814. /* See if directory is empty */
  815. int fat_dir_empty(struct inode *dir)
  816. {
  817. struct buffer_head *bh;
  818. struct msdos_dir_entry *de;
  819. loff_t cpos;
  820. int result = 0;
  821. bh = NULL;
  822. cpos = 0;
  823. while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
  824. if (strncmp(de->name, MSDOS_DOT , MSDOS_NAME) &&
  825. strncmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
  826. result = -ENOTEMPTY;
  827. break;
  828. }
  829. }
  830. brelse(bh);
  831. return result;
  832. }
  833. EXPORT_SYMBOL_GPL(fat_dir_empty);
  834. /*
  835. * fat_subdirs counts the number of sub-directories of dir. It can be run
  836. * on directories being created.
  837. */
  838. int fat_subdirs(struct inode *dir)
  839. {
  840. struct buffer_head *bh;
  841. struct msdos_dir_entry *de;
  842. loff_t cpos;
  843. int count = 0;
  844. bh = NULL;
  845. cpos = 0;
  846. while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
  847. if (de->attr & ATTR_DIR)
  848. count++;
  849. }
  850. brelse(bh);
  851. return count;
  852. }
  853. /*
  854. * Scans a directory for a given file (name points to its formatted name).
  855. * Returns an error code or zero.
  856. */
  857. int fat_scan(struct inode *dir, const unsigned char *name,
  858. struct fat_slot_info *sinfo)
  859. {
  860. struct super_block *sb = dir->i_sb;
  861. sinfo->slot_off = 0;
  862. sinfo->bh = NULL;
  863. while (fat_get_short_entry(dir, &sinfo->slot_off, &sinfo->bh,
  864. &sinfo->de) >= 0) {
  865. if (!strncmp(sinfo->de->name, name, MSDOS_NAME)) {
  866. sinfo->slot_off -= sizeof(*sinfo->de);
  867. sinfo->nr_slots = 1;
  868. sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
  869. return 0;
  870. }
  871. }
  872. return -ENOENT;
  873. }
  874. EXPORT_SYMBOL_GPL(fat_scan);
  875. /*
  876. * Scans a directory for a given logstart.
  877. * Returns an error code or zero.
  878. */
  879. int fat_scan_logstart(struct inode *dir, int i_logstart,
  880. struct fat_slot_info *sinfo)
  881. {
  882. struct super_block *sb = dir->i_sb;
  883. sinfo->slot_off = 0;
  884. sinfo->bh = NULL;
  885. while (fat_get_short_entry(dir, &sinfo->slot_off, &sinfo->bh,
  886. &sinfo->de) >= 0) {
  887. if (fat_get_start(MSDOS_SB(sb), sinfo->de) == i_logstart) {
  888. sinfo->slot_off -= sizeof(*sinfo->de);
  889. sinfo->nr_slots = 1;
  890. sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
  891. return 0;
  892. }
  893. }
  894. return -ENOENT;
  895. }
  896. static int __fat_remove_entries(struct inode *dir, loff_t pos, int nr_slots)
  897. {
  898. struct super_block *sb = dir->i_sb;
  899. struct buffer_head *bh;
  900. struct msdos_dir_entry *de, *endp;
  901. int err = 0, orig_slots;
  902. while (nr_slots) {
  903. bh = NULL;
  904. if (fat_get_entry(dir, &pos, &bh, &de) < 0) {
  905. err = -EIO;
  906. break;
  907. }
  908. orig_slots = nr_slots;
  909. endp = (struct msdos_dir_entry *)(bh->b_data + sb->s_blocksize);
  910. while (nr_slots && de < endp) {
  911. de->name[0] = DELETED_FLAG;
  912. de++;
  913. nr_slots--;
  914. }
  915. mark_buffer_dirty_inode(bh, dir);
  916. if (IS_DIRSYNC(dir))
  917. err = sync_dirty_buffer(bh);
  918. brelse(bh);
  919. if (err)
  920. break;
  921. /* pos is *next* de's position, so this does `- sizeof(de)' */
  922. pos += ((orig_slots - nr_slots) * sizeof(*de)) - sizeof(*de);
  923. }
  924. return err;
  925. }
  926. int fat_remove_entries(struct inode *dir, struct fat_slot_info *sinfo)
  927. {
  928. struct super_block *sb = dir->i_sb;
  929. struct msdos_dir_entry *de;
  930. struct buffer_head *bh;
  931. int err = 0, nr_slots;
  932. /*
  933. * First stage: Remove the shortname. By this, the directory
  934. * entry is removed.
  935. */
  936. nr_slots = sinfo->nr_slots;
  937. de = sinfo->de;
  938. sinfo->de = NULL;
  939. bh = sinfo->bh;
  940. sinfo->bh = NULL;
  941. while (nr_slots && de >= (struct msdos_dir_entry *)bh->b_data) {
  942. de->name[0] = DELETED_FLAG;
  943. de--;
  944. nr_slots--;
  945. }
  946. mark_buffer_dirty_inode(bh, dir);
  947. if (IS_DIRSYNC(dir))
  948. err = sync_dirty_buffer(bh);
  949. brelse(bh);
  950. if (err)
  951. return err;
  952. dir->i_version++;
  953. if (nr_slots) {
  954. /*
  955. * Second stage: remove the remaining longname slots.
  956. * (This directory entry is already removed, and so return
  957. * the success)
  958. */
  959. err = __fat_remove_entries(dir, sinfo->slot_off, nr_slots);
  960. if (err) {
  961. fat_msg(sb, KERN_WARNING,
  962. "Couldn't remove the long name slots");
  963. }
  964. }
  965. dir->i_mtime = dir->i_atime = current_time(dir);
  966. if (IS_DIRSYNC(dir))
  967. (void)fat_sync_inode(dir);
  968. else
  969. mark_inode_dirty(dir);
  970. return 0;
  971. }
  972. EXPORT_SYMBOL_GPL(fat_remove_entries);
  973. static int fat_zeroed_cluster(struct inode *dir, sector_t blknr, int nr_used,
  974. struct buffer_head **bhs, int nr_bhs)
  975. {
  976. struct super_block *sb = dir->i_sb;
  977. sector_t last_blknr = blknr + MSDOS_SB(sb)->sec_per_clus;
  978. int err, i, n;
  979. /* Zeroing the unused blocks on this cluster */
  980. blknr += nr_used;
  981. n = nr_used;
  982. while (blknr < last_blknr) {
  983. bhs[n] = sb_getblk(sb, blknr);
  984. if (!bhs[n]) {
  985. err = -ENOMEM;
  986. goto error;
  987. }
  988. memset(bhs[n]->b_data, 0, sb->s_blocksize);
  989. set_buffer_uptodate(bhs[n]);
  990. mark_buffer_dirty_inode(bhs[n], dir);
  991. n++;
  992. blknr++;
  993. if (n == nr_bhs) {
  994. if (IS_DIRSYNC(dir)) {
  995. err = fat_sync_bhs(bhs, n);
  996. if (err)
  997. goto error;
  998. }
  999. for (i = 0; i < n; i++)
  1000. brelse(bhs[i]);
  1001. n = 0;
  1002. }
  1003. }
  1004. if (IS_DIRSYNC(dir)) {
  1005. err = fat_sync_bhs(bhs, n);
  1006. if (err)
  1007. goto error;
  1008. }
  1009. for (i = 0; i < n; i++)
  1010. brelse(bhs[i]);
  1011. return 0;
  1012. error:
  1013. for (i = 0; i < n; i++)
  1014. bforget(bhs[i]);
  1015. return err;
  1016. }
  1017. int fat_alloc_new_dir(struct inode *dir, struct timespec *ts)
  1018. {
  1019. struct super_block *sb = dir->i_sb;
  1020. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  1021. struct buffer_head *bhs[MAX_BUF_PER_PAGE];
  1022. struct msdos_dir_entry *de;
  1023. sector_t blknr;
  1024. __le16 date, time;
  1025. u8 time_cs;
  1026. int err, cluster;
  1027. err = fat_alloc_clusters(dir, &cluster, 1);
  1028. if (err)
  1029. goto error;
  1030. blknr = fat_clus_to_blknr(sbi, cluster);
  1031. bhs[0] = sb_getblk(sb, blknr);
  1032. if (!bhs[0]) {
  1033. err = -ENOMEM;
  1034. goto error_free;
  1035. }
  1036. fat_time_unix2fat(sbi, ts, &time, &date, &time_cs);
  1037. de = (struct msdos_dir_entry *)bhs[0]->b_data;
  1038. /* filling the new directory slots ("." and ".." entries) */
  1039. memcpy(de[0].name, MSDOS_DOT, MSDOS_NAME);
  1040. memcpy(de[1].name, MSDOS_DOTDOT, MSDOS_NAME);
  1041. de->attr = de[1].attr = ATTR_DIR;
  1042. de[0].lcase = de[1].lcase = 0;
  1043. de[0].time = de[1].time = time;
  1044. de[0].date = de[1].date = date;
  1045. if (sbi->options.isvfat) {
  1046. /* extra timestamps */
  1047. de[0].ctime = de[1].ctime = time;
  1048. de[0].ctime_cs = de[1].ctime_cs = time_cs;
  1049. de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = date;
  1050. } else {
  1051. de[0].ctime = de[1].ctime = 0;
  1052. de[0].ctime_cs = de[1].ctime_cs = 0;
  1053. de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = 0;
  1054. }
  1055. fat_set_start(&de[0], cluster);
  1056. fat_set_start(&de[1], MSDOS_I(dir)->i_logstart);
  1057. de[0].size = de[1].size = 0;
  1058. memset(de + 2, 0, sb->s_blocksize - 2 * sizeof(*de));
  1059. set_buffer_uptodate(bhs[0]);
  1060. mark_buffer_dirty_inode(bhs[0], dir);
  1061. err = fat_zeroed_cluster(dir, blknr, 1, bhs, MAX_BUF_PER_PAGE);
  1062. if (err)
  1063. goto error_free;
  1064. return cluster;
  1065. error_free:
  1066. fat_free_clusters(dir, cluster);
  1067. error:
  1068. return err;
  1069. }
  1070. EXPORT_SYMBOL_GPL(fat_alloc_new_dir);
  1071. static int fat_add_new_entries(struct inode *dir, void *slots, int nr_slots,
  1072. int *nr_cluster, struct msdos_dir_entry **de,
  1073. struct buffer_head **bh, loff_t *i_pos)
  1074. {
  1075. struct super_block *sb = dir->i_sb;
  1076. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  1077. struct buffer_head *bhs[MAX_BUF_PER_PAGE];
  1078. sector_t blknr, start_blknr, last_blknr;
  1079. unsigned long size, copy;
  1080. int err, i, n, offset, cluster[2];
  1081. /*
  1082. * The minimum cluster size is 512bytes, and maximum entry
  1083. * size is 32*slots (672bytes). So, iff the cluster size is
  1084. * 512bytes, we may need two clusters.
  1085. */
  1086. size = nr_slots * sizeof(struct msdos_dir_entry);
  1087. *nr_cluster = (size + (sbi->cluster_size - 1)) >> sbi->cluster_bits;
  1088. BUG_ON(*nr_cluster > 2);
  1089. err = fat_alloc_clusters(dir, cluster, *nr_cluster);
  1090. if (err)
  1091. goto error;
  1092. /*
  1093. * First stage: Fill the directory entry. NOTE: This cluster
  1094. * is not referenced from any inode yet, so updates order is
  1095. * not important.
  1096. */
  1097. i = n = copy = 0;
  1098. do {
  1099. start_blknr = blknr = fat_clus_to_blknr(sbi, cluster[i]);
  1100. last_blknr = start_blknr + sbi->sec_per_clus;
  1101. while (blknr < last_blknr) {
  1102. bhs[n] = sb_getblk(sb, blknr);
  1103. if (!bhs[n]) {
  1104. err = -ENOMEM;
  1105. goto error_nomem;
  1106. }
  1107. /* fill the directory entry */
  1108. copy = min(size, sb->s_blocksize);
  1109. memcpy(bhs[n]->b_data, slots, copy);
  1110. slots += copy;
  1111. size -= copy;
  1112. set_buffer_uptodate(bhs[n]);
  1113. mark_buffer_dirty_inode(bhs[n], dir);
  1114. if (!size)
  1115. break;
  1116. n++;
  1117. blknr++;
  1118. }
  1119. } while (++i < *nr_cluster);
  1120. memset(bhs[n]->b_data + copy, 0, sb->s_blocksize - copy);
  1121. offset = copy - sizeof(struct msdos_dir_entry);
  1122. get_bh(bhs[n]);
  1123. *bh = bhs[n];
  1124. *de = (struct msdos_dir_entry *)((*bh)->b_data + offset);
  1125. *i_pos = fat_make_i_pos(sb, *bh, *de);
  1126. /* Second stage: clear the rest of cluster, and write outs */
  1127. err = fat_zeroed_cluster(dir, start_blknr, ++n, bhs, MAX_BUF_PER_PAGE);
  1128. if (err)
  1129. goto error_free;
  1130. return cluster[0];
  1131. error_free:
  1132. brelse(*bh);
  1133. *bh = NULL;
  1134. n = 0;
  1135. error_nomem:
  1136. for (i = 0; i < n; i++)
  1137. bforget(bhs[i]);
  1138. fat_free_clusters(dir, cluster[0]);
  1139. error:
  1140. return err;
  1141. }
  1142. int fat_add_entries(struct inode *dir, void *slots, int nr_slots,
  1143. struct fat_slot_info *sinfo)
  1144. {
  1145. struct super_block *sb = dir->i_sb;
  1146. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  1147. struct buffer_head *bh, *prev, *bhs[3]; /* 32*slots (672bytes) */
  1148. struct msdos_dir_entry *uninitialized_var(de);
  1149. int err, free_slots, i, nr_bhs;
  1150. loff_t pos, i_pos;
  1151. sinfo->nr_slots = nr_slots;
  1152. /* First stage: search free directory entries */
  1153. free_slots = nr_bhs = 0;
  1154. bh = prev = NULL;
  1155. pos = 0;
  1156. err = -ENOSPC;
  1157. while (fat_get_entry(dir, &pos, &bh, &de) > -1) {
  1158. /* check the maximum size of directory */
  1159. if (pos >= FAT_MAX_DIR_SIZE)
  1160. goto error;
  1161. if (IS_FREE(de->name)) {
  1162. if (prev != bh) {
  1163. get_bh(bh);
  1164. bhs[nr_bhs] = prev = bh;
  1165. nr_bhs++;
  1166. }
  1167. free_slots++;
  1168. if (free_slots == nr_slots)
  1169. goto found;
  1170. } else {
  1171. for (i = 0; i < nr_bhs; i++)
  1172. brelse(bhs[i]);
  1173. prev = NULL;
  1174. free_slots = nr_bhs = 0;
  1175. }
  1176. }
  1177. if (dir->i_ino == MSDOS_ROOT_INO) {
  1178. if (sbi->fat_bits != 32)
  1179. goto error;
  1180. } else if (MSDOS_I(dir)->i_start == 0) {
  1181. fat_msg(sb, KERN_ERR, "Corrupted directory (i_pos %lld)",
  1182. MSDOS_I(dir)->i_pos);
  1183. err = -EIO;
  1184. goto error;
  1185. }
  1186. found:
  1187. err = 0;
  1188. pos -= free_slots * sizeof(*de);
  1189. nr_slots -= free_slots;
  1190. if (free_slots) {
  1191. /*
  1192. * Second stage: filling the free entries with new entries.
  1193. * NOTE: If this slots has shortname, first, we write
  1194. * the long name slots, then write the short name.
  1195. */
  1196. int size = free_slots * sizeof(*de);
  1197. int offset = pos & (sb->s_blocksize - 1);
  1198. int long_bhs = nr_bhs - (nr_slots == 0);
  1199. /* Fill the long name slots. */
  1200. for (i = 0; i < long_bhs; i++) {
  1201. int copy = min_t(int, sb->s_blocksize - offset, size);
  1202. memcpy(bhs[i]->b_data + offset, slots, copy);
  1203. mark_buffer_dirty_inode(bhs[i], dir);
  1204. offset = 0;
  1205. slots += copy;
  1206. size -= copy;
  1207. }
  1208. if (long_bhs && IS_DIRSYNC(dir))
  1209. err = fat_sync_bhs(bhs, long_bhs);
  1210. if (!err && i < nr_bhs) {
  1211. /* Fill the short name slot. */
  1212. int copy = min_t(int, sb->s_blocksize - offset, size);
  1213. memcpy(bhs[i]->b_data + offset, slots, copy);
  1214. mark_buffer_dirty_inode(bhs[i], dir);
  1215. if (IS_DIRSYNC(dir))
  1216. err = sync_dirty_buffer(bhs[i]);
  1217. }
  1218. for (i = 0; i < nr_bhs; i++)
  1219. brelse(bhs[i]);
  1220. if (err)
  1221. goto error_remove;
  1222. }
  1223. if (nr_slots) {
  1224. int cluster, nr_cluster;
  1225. /*
  1226. * Third stage: allocate the cluster for new entries.
  1227. * And initialize the cluster with new entries, then
  1228. * add the cluster to dir.
  1229. */
  1230. cluster = fat_add_new_entries(dir, slots, nr_slots, &nr_cluster,
  1231. &de, &bh, &i_pos);
  1232. if (cluster < 0) {
  1233. err = cluster;
  1234. goto error_remove;
  1235. }
  1236. err = fat_chain_add(dir, cluster, nr_cluster);
  1237. if (err) {
  1238. fat_free_clusters(dir, cluster);
  1239. goto error_remove;
  1240. }
  1241. if (dir->i_size & (sbi->cluster_size - 1)) {
  1242. fat_fs_error(sb, "Odd directory size");
  1243. dir->i_size = (dir->i_size + sbi->cluster_size - 1)
  1244. & ~((loff_t)sbi->cluster_size - 1);
  1245. }
  1246. dir->i_size += nr_cluster << sbi->cluster_bits;
  1247. MSDOS_I(dir)->mmu_private += nr_cluster << sbi->cluster_bits;
  1248. }
  1249. sinfo->slot_off = pos;
  1250. sinfo->de = de;
  1251. sinfo->bh = bh;
  1252. sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
  1253. return 0;
  1254. error:
  1255. brelse(bh);
  1256. for (i = 0; i < nr_bhs; i++)
  1257. brelse(bhs[i]);
  1258. return err;
  1259. error_remove:
  1260. brelse(bh);
  1261. if (free_slots)
  1262. __fat_remove_entries(dir, pos, free_slots);
  1263. return err;
  1264. }
  1265. EXPORT_SYMBOL_GPL(fat_add_entries);