dir.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391
  1. /*
  2. * linux/fs/fat/dir.c
  3. *
  4. * directory handling functions for fat-based filesystems
  5. *
  6. * Written 1992,1993 by Werner Almesberger
  7. *
  8. * Hidden files 1995 by Albert Cahalan <albert@ccs.neu.edu> <adc@coe.neu.edu>
  9. *
  10. * VFAT extensions by Gordon Chaffee <chaffee@plateau.cs.berkeley.edu>
  11. * Merged with msdos fs by Henrik Storner <storner@osiris.ping.dk>
  12. * Rewritten for constant inumbers. Plugged buffer overrun in readdir(). AV
  13. * Short name translation 1999, 2001 by Wolfram Pienkoss <wp@bszh.de>
  14. */
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/time.h>
  18. #include <linux/buffer_head.h>
  19. #include <linux/compat.h>
  20. #include <asm/uaccess.h>
  21. #include <linux/kernel.h>
  22. #include "fat.h"
  23. /*
  24. * Maximum buffer size of short name.
  25. * [(MSDOS_NAME + '.') * max one char + nul]
  26. * For msdos style, ['.' (hidden) + MSDOS_NAME + '.' + nul]
  27. */
  28. #define FAT_MAX_SHORT_SIZE ((MSDOS_NAME + 1) * NLS_MAX_CHARSET_SIZE + 1)
  29. /*
  30. * Maximum buffer size of unicode chars from slots.
  31. * [(max longname slots * 13 (size in a slot) + nul) * sizeof(wchar_t)]
  32. */
  33. #define FAT_MAX_UNI_CHARS ((MSDOS_SLOTS - 1) * 13 + 1)
  34. #define FAT_MAX_UNI_SIZE (FAT_MAX_UNI_CHARS * sizeof(wchar_t))
  35. static inline unsigned char fat_tolower(unsigned char c)
  36. {
  37. return ((c >= 'A') && (c <= 'Z')) ? c+32 : c;
  38. }
  39. static inline loff_t fat_make_i_pos(struct super_block *sb,
  40. struct buffer_head *bh,
  41. struct msdos_dir_entry *de)
  42. {
  43. return ((loff_t)bh->b_blocknr << MSDOS_SB(sb)->dir_per_block_bits)
  44. | (de - (struct msdos_dir_entry *)bh->b_data);
  45. }
  46. static inline void fat_dir_readahead(struct inode *dir, sector_t iblock,
  47. sector_t phys)
  48. {
  49. struct super_block *sb = dir->i_sb;
  50. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  51. struct buffer_head *bh;
  52. int sec;
  53. /* This is not a first sector of cluster, or sec_per_clus == 1 */
  54. if ((iblock & (sbi->sec_per_clus - 1)) || sbi->sec_per_clus == 1)
  55. return;
  56. /* root dir of FAT12/FAT16 */
  57. if ((sbi->fat_bits != 32) && (dir->i_ino == MSDOS_ROOT_INO))
  58. return;
  59. bh = sb_find_get_block(sb, phys);
  60. if (bh == NULL || !buffer_uptodate(bh)) {
  61. for (sec = 0; sec < sbi->sec_per_clus; sec++)
  62. sb_breadahead(sb, phys + sec);
  63. }
  64. brelse(bh);
  65. }
  66. /* Returns the inode number of the directory entry at offset pos. If bh is
  67. non-NULL, it is brelse'd before. Pos is incremented. The buffer header is
  68. returned in bh.
  69. AV. Most often we do it item-by-item. Makes sense to optimize.
  70. AV. OK, there we go: if both bh and de are non-NULL we assume that we just
  71. AV. want the next entry (took one explicit de=NULL in vfat/namei.c).
  72. AV. It's done in fat_get_entry() (inlined), here the slow case lives.
  73. AV. Additionally, when we return -1 (i.e. reached the end of directory)
  74. AV. we make bh NULL.
  75. */
  76. static int fat__get_entry(struct inode *dir, loff_t *pos,
  77. struct buffer_head **bh, struct msdos_dir_entry **de)
  78. {
  79. struct super_block *sb = dir->i_sb;
  80. sector_t phys, iblock;
  81. unsigned long mapped_blocks;
  82. int err, offset;
  83. next:
  84. if (*bh)
  85. brelse(*bh);
  86. *bh = NULL;
  87. iblock = *pos >> sb->s_blocksize_bits;
  88. err = fat_bmap(dir, iblock, &phys, &mapped_blocks, 0);
  89. if (err || !phys)
  90. return -1; /* beyond EOF or error */
  91. fat_dir_readahead(dir, iblock, phys);
  92. *bh = sb_bread(sb, phys);
  93. if (*bh == NULL) {
  94. fat_msg_ratelimit(sb, KERN_ERR,
  95. "Directory bread(block %llu) failed", (llu)phys);
  96. /* skip this block */
  97. *pos = (iblock + 1) << sb->s_blocksize_bits;
  98. goto next;
  99. }
  100. offset = *pos & (sb->s_blocksize - 1);
  101. *pos += sizeof(struct msdos_dir_entry);
  102. *de = (struct msdos_dir_entry *)((*bh)->b_data + offset);
  103. return 0;
  104. }
  105. static inline int fat_get_entry(struct inode *dir, loff_t *pos,
  106. struct buffer_head **bh,
  107. struct msdos_dir_entry **de)
  108. {
  109. /* Fast stuff first */
  110. if (*bh && *de &&
  111. (*de - (struct msdos_dir_entry *)(*bh)->b_data) < MSDOS_SB(dir->i_sb)->dir_per_block - 1) {
  112. *pos += sizeof(struct msdos_dir_entry);
  113. (*de)++;
  114. return 0;
  115. }
  116. return fat__get_entry(dir, pos, bh, de);
  117. }
  118. /*
  119. * Convert Unicode 16 to UTF-8, translated Unicode, or ASCII.
  120. * If uni_xlate is enabled and we can't get a 1:1 conversion, use a
  121. * colon as an escape character since it is normally invalid on the vfat
  122. * filesystem. The following four characters are the hexadecimal digits
  123. * of Unicode value. This lets us do a full dump and restore of Unicode
  124. * filenames. We could get into some trouble with long Unicode names,
  125. * but ignore that right now.
  126. * Ahem... Stack smashing in ring 0 isn't fun. Fixed.
  127. */
  128. static int uni16_to_x8(struct super_block *sb, unsigned char *ascii,
  129. const wchar_t *uni, int len, struct nls_table *nls)
  130. {
  131. int uni_xlate = MSDOS_SB(sb)->options.unicode_xlate;
  132. const wchar_t *ip;
  133. wchar_t ec;
  134. unsigned char *op;
  135. int charlen;
  136. ip = uni;
  137. op = ascii;
  138. while (*ip && ((len - NLS_MAX_CHARSET_SIZE) > 0)) {
  139. ec = *ip++;
  140. if ((charlen = nls->uni2char(ec, op, NLS_MAX_CHARSET_SIZE)) > 0) {
  141. op += charlen;
  142. len -= charlen;
  143. } else {
  144. if (uni_xlate == 1) {
  145. *op++ = ':';
  146. op = hex_byte_pack(op, ec >> 8);
  147. op = hex_byte_pack(op, ec);
  148. len -= 5;
  149. } else {
  150. *op++ = '?';
  151. len--;
  152. }
  153. }
  154. }
  155. if (unlikely(*ip)) {
  156. fat_msg(sb, KERN_WARNING, "filename was truncated while "
  157. "converting.");
  158. }
  159. *op = 0;
  160. return (op - ascii);
  161. }
  162. static inline int fat_uni_to_x8(struct super_block *sb, const wchar_t *uni,
  163. unsigned char *buf, int size)
  164. {
  165. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  166. if (sbi->options.utf8)
  167. return utf16s_to_utf8s(uni, FAT_MAX_UNI_CHARS,
  168. UTF16_HOST_ENDIAN, buf, size);
  169. else
  170. return uni16_to_x8(sb, buf, uni, size, sbi->nls_io);
  171. }
  172. static inline int
  173. fat_short2uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
  174. {
  175. int charlen;
  176. charlen = t->char2uni(c, clen, uni);
  177. if (charlen < 0) {
  178. *uni = 0x003f; /* a question mark */
  179. charlen = 1;
  180. }
  181. return charlen;
  182. }
  183. static inline int
  184. fat_short2lower_uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
  185. {
  186. int charlen;
  187. wchar_t wc;
  188. charlen = t->char2uni(c, clen, &wc);
  189. if (charlen < 0) {
  190. *uni = 0x003f; /* a question mark */
  191. charlen = 1;
  192. } else if (charlen <= 1) {
  193. unsigned char nc = t->charset2lower[*c];
  194. if (!nc)
  195. nc = *c;
  196. if ( (charlen = t->char2uni(&nc, 1, uni)) < 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, 0 -> not found, positive -> found,
  411. * value is the total amount of slots, including the shortname entry.
  412. */
  413. int fat_search_long(struct inode *inode, const unsigned char *name,
  414. int name_len, struct fat_slot_info *sinfo)
  415. {
  416. struct super_block *sb = inode->i_sb;
  417. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  418. struct buffer_head *bh = NULL;
  419. struct msdos_dir_entry *de;
  420. unsigned char nr_slots;
  421. wchar_t *unicode = NULL;
  422. unsigned char bufname[FAT_MAX_SHORT_SIZE];
  423. loff_t cpos = 0;
  424. int err, len;
  425. err = -ENOENT;
  426. while (1) {
  427. if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
  428. goto end_of_dir;
  429. parse_record:
  430. nr_slots = 0;
  431. if (de->name[0] == DELETED_FLAG)
  432. continue;
  433. if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
  434. continue;
  435. if (de->attr != ATTR_EXT && IS_FREE(de->name))
  436. continue;
  437. if (de->attr == ATTR_EXT) {
  438. int status = fat_parse_long(inode, &cpos, &bh, &de,
  439. &unicode, &nr_slots);
  440. if (status < 0) {
  441. err = status;
  442. goto end_of_dir;
  443. } else if (status == PARSE_INVALID)
  444. continue;
  445. else if (status == PARSE_NOT_LONGNAME)
  446. goto parse_record;
  447. else if (status == PARSE_EOF)
  448. goto end_of_dir;
  449. }
  450. /* Never prepend '.' to hidden files here.
  451. * That is done only for msdos mounts (and only when
  452. * 'dotsOK=yes'); if we are executing here, it is in the
  453. * context of a vfat mount.
  454. */
  455. len = fat_parse_short(sb, de, bufname, 0);
  456. if (len == 0)
  457. continue;
  458. /* Compare shortname */
  459. if (fat_name_match(sbi, name, name_len, bufname, len))
  460. goto found;
  461. if (nr_slots) {
  462. void *longname = unicode + FAT_MAX_UNI_CHARS;
  463. int size = PATH_MAX - FAT_MAX_UNI_SIZE;
  464. /* Compare longname */
  465. len = fat_uni_to_x8(sb, unicode, longname, size);
  466. if (fat_name_match(sbi, name, name_len, longname, len))
  467. goto found;
  468. }
  469. }
  470. found:
  471. nr_slots++; /* include the de */
  472. sinfo->slot_off = cpos - nr_slots * sizeof(*de);
  473. sinfo->nr_slots = nr_slots;
  474. sinfo->de = de;
  475. sinfo->bh = bh;
  476. sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
  477. err = 0;
  478. end_of_dir:
  479. if (unicode)
  480. __putname(unicode);
  481. return err;
  482. }
  483. EXPORT_SYMBOL_GPL(fat_search_long);
  484. struct fat_ioctl_filldir_callback {
  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 *filp, void *dirent,
  494. filldir_t filldir, int short_only, int both)
  495. {
  496. struct super_block *sb = inode->i_sb;
  497. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  498. struct buffer_head *bh;
  499. struct msdos_dir_entry *de;
  500. unsigned char nr_slots;
  501. wchar_t *unicode = NULL;
  502. unsigned char bufname[FAT_MAX_SHORT_SIZE];
  503. int isvfat = sbi->options.isvfat;
  504. const char *fill_name = NULL;
  505. unsigned long inum;
  506. unsigned long lpos, dummy, *furrfu = &lpos;
  507. loff_t cpos;
  508. int short_len = 0, fill_len = 0;
  509. int ret = 0;
  510. lock_super(sb);
  511. cpos = filp->f_pos;
  512. /* Fake . and .. for the root directory. */
  513. if (inode->i_ino == MSDOS_ROOT_INO) {
  514. while (cpos < 2) {
  515. if (filldir(dirent, "..", cpos+1, cpos, MSDOS_ROOT_INO, DT_DIR) < 0)
  516. goto out;
  517. cpos++;
  518. filp->f_pos++;
  519. }
  520. if (cpos == 2) {
  521. dummy = 2;
  522. furrfu = &dummy;
  523. cpos = 0;
  524. }
  525. }
  526. if (cpos & (sizeof(struct msdos_dir_entry) - 1)) {
  527. ret = -ENOENT;
  528. goto out;
  529. }
  530. bh = NULL;
  531. get_new:
  532. if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
  533. goto end_of_dir;
  534. parse_record:
  535. nr_slots = 0;
  536. /*
  537. * Check for long filename entry, but if short_only, we don't
  538. * need to parse long filename.
  539. */
  540. if (isvfat && !short_only) {
  541. if (de->name[0] == DELETED_FLAG)
  542. goto record_end;
  543. if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
  544. goto record_end;
  545. if (de->attr != ATTR_EXT && IS_FREE(de->name))
  546. goto record_end;
  547. } else {
  548. if ((de->attr & ATTR_VOLUME) || IS_FREE(de->name))
  549. goto record_end;
  550. }
  551. if (isvfat && de->attr == ATTR_EXT) {
  552. int status = fat_parse_long(inode, &cpos, &bh, &de,
  553. &unicode, &nr_slots);
  554. if (status < 0) {
  555. filp->f_pos = cpos;
  556. ret = status;
  557. goto out;
  558. } else if (status == PARSE_INVALID)
  559. goto record_end;
  560. else if (status == PARSE_NOT_LONGNAME)
  561. goto parse_record;
  562. else if (status == PARSE_EOF)
  563. goto end_of_dir;
  564. if (nr_slots) {
  565. void *longname = unicode + FAT_MAX_UNI_CHARS;
  566. int size = PATH_MAX - FAT_MAX_UNI_SIZE;
  567. int len = fat_uni_to_x8(sb, unicode, longname, size);
  568. fill_name = longname;
  569. fill_len = len;
  570. /* !both && !short_only, so we don't need shortname. */
  571. if (!both)
  572. goto start_filldir;
  573. }
  574. }
  575. short_len = fat_parse_short(sb, de, bufname, sbi->options.dotsOK);
  576. if (short_len == 0)
  577. goto record_end;
  578. if (nr_slots) {
  579. /* hack for fat_ioctl_filldir() */
  580. struct fat_ioctl_filldir_callback *p = dirent;
  581. p->longname = fill_name;
  582. p->long_len = fill_len;
  583. p->shortname = bufname;
  584. p->short_len = short_len;
  585. fill_name = NULL;
  586. fill_len = 0;
  587. } else {
  588. fill_name = bufname;
  589. fill_len = short_len;
  590. }
  591. start_filldir:
  592. lpos = cpos - (nr_slots + 1) * sizeof(struct msdos_dir_entry);
  593. if (!memcmp(de->name, MSDOS_DOT, MSDOS_NAME))
  594. inum = inode->i_ino;
  595. else if (!memcmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
  596. inum = parent_ino(filp->f_path.dentry);
  597. } else {
  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. }
  606. if (filldir(dirent, fill_name, fill_len, *furrfu, inum,
  607. (de->attr & ATTR_DIR) ? DT_DIR : DT_REG) < 0)
  608. goto fill_failed;
  609. record_end:
  610. furrfu = &lpos;
  611. filp->f_pos = cpos;
  612. goto get_new;
  613. end_of_dir:
  614. filp->f_pos = cpos;
  615. fill_failed:
  616. brelse(bh);
  617. if (unicode)
  618. __putname(unicode);
  619. out:
  620. unlock_super(sb);
  621. return ret;
  622. }
  623. static int fat_readdir(struct file *filp, void *dirent, filldir_t filldir)
  624. {
  625. struct inode *inode = filp->f_path.dentry->d_inode;
  626. return __fat_readdir(inode, filp, dirent, filldir, 0, 0);
  627. }
  628. #define FAT_IOCTL_FILLDIR_FUNC(func, dirent_type) \
  629. static int func(void *__buf, const char *name, int name_len, \
  630. loff_t offset, u64 ino, unsigned int d_type) \
  631. { \
  632. struct fat_ioctl_filldir_callback *buf = __buf; \
  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 *filp,
  680. void __user *dirent, filldir_t filldir,
  681. int short_only, int both)
  682. {
  683. struct fat_ioctl_filldir_callback buf;
  684. int ret;
  685. buf.dirent = dirent;
  686. buf.result = 0;
  687. mutex_lock(&inode->i_mutex);
  688. ret = -ENOENT;
  689. if (!IS_DEADDIR(inode)) {
  690. ret = __fat_readdir(inode, filp, &buf, filldir,
  691. short_only, both);
  692. }
  693. mutex_unlock(&inode->i_mutex);
  694. if (ret >= 0)
  695. ret = buf.result;
  696. return ret;
  697. }
  698. static int fat_ioctl_volume_id(struct inode *dir)
  699. {
  700. struct super_block *sb = dir->i_sb;
  701. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  702. return sbi->vol_id;
  703. }
  704. static long fat_dir_ioctl(struct file *filp, unsigned int cmd,
  705. unsigned long arg)
  706. {
  707. struct inode *inode = filp->f_path.dentry->d_inode;
  708. struct __fat_dirent __user *d1 = (struct __fat_dirent __user *)arg;
  709. int short_only, both;
  710. switch (cmd) {
  711. case VFAT_IOCTL_READDIR_SHORT:
  712. short_only = 1;
  713. both = 0;
  714. break;
  715. case VFAT_IOCTL_READDIR_BOTH:
  716. short_only = 0;
  717. both = 1;
  718. break;
  719. case VFAT_IOCTL_GET_VOLUME_ID:
  720. return fat_ioctl_volume_id(inode);
  721. default:
  722. return fat_generic_ioctl(filp, cmd, arg);
  723. }
  724. if (!access_ok(VERIFY_WRITE, d1, sizeof(struct __fat_dirent[2])))
  725. return -EFAULT;
  726. /*
  727. * Yes, we don't need this put_user() absolutely. However old
  728. * code didn't return the right value. So, app use this value,
  729. * in order to check whether it is EOF.
  730. */
  731. if (put_user(0, &d1->d_reclen))
  732. return -EFAULT;
  733. return fat_ioctl_readdir(inode, filp, d1, fat_ioctl_filldir,
  734. short_only, both);
  735. }
  736. #ifdef CONFIG_COMPAT
  737. #define VFAT_IOCTL_READDIR_BOTH32 _IOR('r', 1, struct compat_dirent[2])
  738. #define VFAT_IOCTL_READDIR_SHORT32 _IOR('r', 2, struct compat_dirent[2])
  739. FAT_IOCTL_FILLDIR_FUNC(fat_compat_ioctl_filldir, compat_dirent)
  740. static long fat_compat_dir_ioctl(struct file *filp, unsigned cmd,
  741. unsigned long arg)
  742. {
  743. struct inode *inode = filp->f_path.dentry->d_inode;
  744. struct compat_dirent __user *d1 = compat_ptr(arg);
  745. int short_only, both;
  746. switch (cmd) {
  747. case VFAT_IOCTL_READDIR_SHORT32:
  748. short_only = 1;
  749. both = 0;
  750. break;
  751. case VFAT_IOCTL_READDIR_BOTH32:
  752. short_only = 0;
  753. both = 1;
  754. break;
  755. default:
  756. return fat_generic_ioctl(filp, cmd, (unsigned long)arg);
  757. }
  758. if (!access_ok(VERIFY_WRITE, d1, sizeof(struct compat_dirent[2])))
  759. return -EFAULT;
  760. /*
  761. * Yes, we don't need this put_user() absolutely. However old
  762. * code didn't return the right value. So, app use this value,
  763. * in order to check whether it is EOF.
  764. */
  765. if (put_user(0, &d1->d_reclen))
  766. return -EFAULT;
  767. return fat_ioctl_readdir(inode, filp, d1, fat_compat_ioctl_filldir,
  768. short_only, both);
  769. }
  770. #endif /* CONFIG_COMPAT */
  771. const struct file_operations fat_dir_operations = {
  772. .llseek = generic_file_llseek,
  773. .read = generic_read_dir,
  774. .readdir = fat_readdir,
  775. .unlocked_ioctl = fat_dir_ioctl,
  776. #ifdef CONFIG_COMPAT
  777. .compat_ioctl = fat_compat_dir_ioctl,
  778. #endif
  779. .fsync = fat_file_fsync,
  780. };
  781. static int fat_get_short_entry(struct inode *dir, loff_t *pos,
  782. struct buffer_head **bh,
  783. struct msdos_dir_entry **de)
  784. {
  785. while (fat_get_entry(dir, pos, bh, de) >= 0) {
  786. /* free entry or long name entry or volume label */
  787. if (!IS_FREE((*de)->name) && !((*de)->attr & ATTR_VOLUME))
  788. return 0;
  789. }
  790. return -ENOENT;
  791. }
  792. /*
  793. * The ".." entry can not provide the "struct fat_slot_info" informations
  794. * for inode. So, this function provide the some informations only.
  795. */
  796. int fat_get_dotdot_entry(struct inode *dir, struct buffer_head **bh,
  797. struct msdos_dir_entry **de, loff_t *i_pos)
  798. {
  799. loff_t offset;
  800. offset = 0;
  801. *bh = NULL;
  802. while (fat_get_short_entry(dir, &offset, bh, de) >= 0) {
  803. if (!strncmp((*de)->name, MSDOS_DOTDOT, MSDOS_NAME)) {
  804. *i_pos = fat_make_i_pos(dir->i_sb, *bh, *de);
  805. return 0;
  806. }
  807. }
  808. return -ENOENT;
  809. }
  810. EXPORT_SYMBOL_GPL(fat_get_dotdot_entry);
  811. /* See if directory is empty */
  812. int fat_dir_empty(struct inode *dir)
  813. {
  814. struct buffer_head *bh;
  815. struct msdos_dir_entry *de;
  816. loff_t cpos;
  817. int result = 0;
  818. bh = NULL;
  819. cpos = 0;
  820. while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
  821. if (strncmp(de->name, MSDOS_DOT , MSDOS_NAME) &&
  822. strncmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
  823. result = -ENOTEMPTY;
  824. break;
  825. }
  826. }
  827. brelse(bh);
  828. return result;
  829. }
  830. EXPORT_SYMBOL_GPL(fat_dir_empty);
  831. /*
  832. * fat_subdirs counts the number of sub-directories of dir. It can be run
  833. * on directories being created.
  834. */
  835. int fat_subdirs(struct inode *dir)
  836. {
  837. struct buffer_head *bh;
  838. struct msdos_dir_entry *de;
  839. loff_t cpos;
  840. int count = 0;
  841. bh = NULL;
  842. cpos = 0;
  843. while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
  844. if (de->attr & ATTR_DIR)
  845. count++;
  846. }
  847. brelse(bh);
  848. return count;
  849. }
  850. /*
  851. * Scans a directory for a given file (name points to its formatted name).
  852. * Returns an error code or zero.
  853. */
  854. int fat_scan(struct inode *dir, const unsigned char *name,
  855. struct fat_slot_info *sinfo)
  856. {
  857. struct super_block *sb = dir->i_sb;
  858. sinfo->slot_off = 0;
  859. sinfo->bh = NULL;
  860. while (fat_get_short_entry(dir, &sinfo->slot_off, &sinfo->bh,
  861. &sinfo->de) >= 0) {
  862. if (!strncmp(sinfo->de->name, name, MSDOS_NAME)) {
  863. sinfo->slot_off -= sizeof(*sinfo->de);
  864. sinfo->nr_slots = 1;
  865. sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
  866. return 0;
  867. }
  868. }
  869. return -ENOENT;
  870. }
  871. EXPORT_SYMBOL_GPL(fat_scan);
  872. static int __fat_remove_entries(struct inode *dir, loff_t pos, int nr_slots)
  873. {
  874. struct super_block *sb = dir->i_sb;
  875. struct buffer_head *bh;
  876. struct msdos_dir_entry *de, *endp;
  877. int err = 0, orig_slots;
  878. while (nr_slots) {
  879. bh = NULL;
  880. if (fat_get_entry(dir, &pos, &bh, &de) < 0) {
  881. err = -EIO;
  882. break;
  883. }
  884. orig_slots = nr_slots;
  885. endp = (struct msdos_dir_entry *)(bh->b_data + sb->s_blocksize);
  886. while (nr_slots && de < endp) {
  887. de->name[0] = DELETED_FLAG;
  888. de++;
  889. nr_slots--;
  890. }
  891. mark_buffer_dirty_inode_sync(bh, dir);
  892. if (IS_DIRSYNC(dir))
  893. err = sync_dirty_buffer(bh);
  894. brelse(bh);
  895. if (err)
  896. break;
  897. /* pos is *next* de's position, so this does `- sizeof(de)' */
  898. pos += ((orig_slots - nr_slots) * sizeof(*de)) - sizeof(*de);
  899. }
  900. return err;
  901. }
  902. int fat_remove_entries(struct inode *dir, struct fat_slot_info *sinfo)
  903. {
  904. struct super_block *sb = dir->i_sb;
  905. struct msdos_dir_entry *de;
  906. struct buffer_head *bh;
  907. int err = 0, nr_slots;
  908. /*
  909. * First stage: Remove the shortname. By this, the directory
  910. * entry is removed.
  911. */
  912. nr_slots = sinfo->nr_slots;
  913. de = sinfo->de;
  914. sinfo->de = NULL;
  915. bh = sinfo->bh;
  916. sinfo->bh = NULL;
  917. while (nr_slots && de >= (struct msdos_dir_entry *)bh->b_data) {
  918. de->name[0] = DELETED_FLAG;
  919. de--;
  920. nr_slots--;
  921. }
  922. mark_buffer_dirty_inode_sync(bh, dir);
  923. if (IS_DIRSYNC(dir))
  924. err = sync_dirty_buffer(bh);
  925. brelse(bh);
  926. if (err)
  927. return err;
  928. dir->i_version++;
  929. if (nr_slots) {
  930. /*
  931. * Second stage: remove the remaining longname slots.
  932. * (This directory entry is already removed, and so return
  933. * the success)
  934. */
  935. err = __fat_remove_entries(dir, sinfo->slot_off, nr_slots);
  936. if (err) {
  937. fat_msg(sb, KERN_WARNING,
  938. "Couldn't remove the long name slots");
  939. }
  940. }
  941. dir->i_mtime = dir->i_atime = CURRENT_TIME_SEC;
  942. if (IS_DIRSYNC(dir))
  943. (void)fat_sync_inode(dir);
  944. else
  945. mark_inode_dirty(dir);
  946. return 0;
  947. }
  948. EXPORT_SYMBOL_GPL(fat_remove_entries);
  949. static int fat_zeroed_cluster(struct inode *dir, sector_t blknr, int nr_used,
  950. struct buffer_head **bhs, int nr_bhs)
  951. {
  952. struct super_block *sb = dir->i_sb;
  953. sector_t last_blknr = blknr + MSDOS_SB(sb)->sec_per_clus;
  954. int err, i, n;
  955. /* Zeroing the unused blocks on this cluster */
  956. blknr += nr_used;
  957. n = nr_used;
  958. while (blknr < last_blknr) {
  959. bhs[n] = sb_getblk(sb, blknr);
  960. if (!bhs[n]) {
  961. err = -ENOMEM;
  962. goto error;
  963. }
  964. memset(bhs[n]->b_data, 0, sb->s_blocksize);
  965. set_buffer_uptodate(bhs[n]);
  966. mark_buffer_dirty_inode_sync(bhs[n], dir);
  967. n++;
  968. blknr++;
  969. if (n == nr_bhs) {
  970. if (IS_DIRSYNC(dir)) {
  971. err = fat_sync_bhs(bhs, n);
  972. if (err)
  973. goto error;
  974. }
  975. for (i = 0; i < n; i++)
  976. brelse(bhs[i]);
  977. n = 0;
  978. }
  979. }
  980. if (IS_DIRSYNC(dir)) {
  981. err = fat_sync_bhs(bhs, n);
  982. if (err)
  983. goto error;
  984. }
  985. for (i = 0; i < n; i++)
  986. brelse(bhs[i]);
  987. return 0;
  988. error:
  989. for (i = 0; i < n; i++)
  990. bforget(bhs[i]);
  991. return err;
  992. }
  993. int fat_alloc_new_dir(struct inode *dir, struct timespec *ts)
  994. {
  995. struct super_block *sb = dir->i_sb;
  996. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  997. struct buffer_head *bhs[MAX_BUF_PER_PAGE];
  998. struct msdos_dir_entry *de;
  999. sector_t blknr;
  1000. __le16 date, time;
  1001. u8 time_cs;
  1002. int err, cluster;
  1003. err = fat_alloc_clusters(dir, &cluster, 1);
  1004. if (err)
  1005. goto error;
  1006. blknr = fat_clus_to_blknr(sbi, cluster);
  1007. bhs[0] = sb_getblk(sb, blknr);
  1008. if (!bhs[0]) {
  1009. err = -ENOMEM;
  1010. goto error_free;
  1011. }
  1012. fat_time_unix2fat(sbi, ts, &time, &date, &time_cs);
  1013. de = (struct msdos_dir_entry *)bhs[0]->b_data;
  1014. /* filling the new directory slots ("." and ".." entries) */
  1015. memcpy(de[0].name, MSDOS_DOT, MSDOS_NAME);
  1016. memcpy(de[1].name, MSDOS_DOTDOT, MSDOS_NAME);
  1017. de->attr = de[1].attr = ATTR_DIR;
  1018. de[0].lcase = de[1].lcase = 0;
  1019. de[0].time = de[1].time = time;
  1020. de[0].date = de[1].date = date;
  1021. if (sbi->options.isvfat) {
  1022. /* extra timestamps */
  1023. de[0].ctime = de[1].ctime = time;
  1024. de[0].ctime_cs = de[1].ctime_cs = time_cs;
  1025. de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = date;
  1026. } else {
  1027. de[0].ctime = de[1].ctime = 0;
  1028. de[0].ctime_cs = de[1].ctime_cs = 0;
  1029. de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = 0;
  1030. }
  1031. de[0].start = cpu_to_le16(cluster);
  1032. de[0].starthi = cpu_to_le16(cluster >> 16);
  1033. de[1].start = cpu_to_le16(MSDOS_I(dir)->i_logstart);
  1034. de[1].starthi = cpu_to_le16(MSDOS_I(dir)->i_logstart >> 16);
  1035. de[0].size = de[1].size = 0;
  1036. memset(de + 2, 0, sb->s_blocksize - 2 * sizeof(*de));
  1037. set_buffer_uptodate(bhs[0]);
  1038. mark_buffer_dirty_inode_sync(bhs[0], dir);
  1039. err = fat_zeroed_cluster(dir, blknr, 1, bhs, MAX_BUF_PER_PAGE);
  1040. if (err)
  1041. goto error_free;
  1042. return cluster;
  1043. error_free:
  1044. fat_free_clusters(dir, cluster);
  1045. error:
  1046. return err;
  1047. }
  1048. EXPORT_SYMBOL_GPL(fat_alloc_new_dir);
  1049. static int fat_add_new_entries(struct inode *dir, void *slots, int nr_slots,
  1050. int *nr_cluster, struct msdos_dir_entry **de,
  1051. struct buffer_head **bh, loff_t *i_pos)
  1052. {
  1053. struct super_block *sb = dir->i_sb;
  1054. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  1055. struct buffer_head *bhs[MAX_BUF_PER_PAGE];
  1056. sector_t blknr, start_blknr, last_blknr;
  1057. unsigned long size, copy;
  1058. int err, i, n, offset, cluster[2];
  1059. /*
  1060. * The minimum cluster size is 512bytes, and maximum entry
  1061. * size is 32*slots (672bytes). So, iff the cluster size is
  1062. * 512bytes, we may need two clusters.
  1063. */
  1064. size = nr_slots * sizeof(struct msdos_dir_entry);
  1065. *nr_cluster = (size + (sbi->cluster_size - 1)) >> sbi->cluster_bits;
  1066. BUG_ON(*nr_cluster > 2);
  1067. err = fat_alloc_clusters(dir, cluster, *nr_cluster);
  1068. if (err)
  1069. goto error;
  1070. /*
  1071. * First stage: Fill the directory entry. NOTE: This cluster
  1072. * is not referenced from any inode yet, so updates order is
  1073. * not important.
  1074. */
  1075. i = n = copy = 0;
  1076. do {
  1077. start_blknr = blknr = fat_clus_to_blknr(sbi, cluster[i]);
  1078. last_blknr = start_blknr + sbi->sec_per_clus;
  1079. while (blknr < last_blknr) {
  1080. bhs[n] = sb_getblk(sb, blknr);
  1081. if (!bhs[n]) {
  1082. err = -ENOMEM;
  1083. goto error_nomem;
  1084. }
  1085. /* fill the directory entry */
  1086. copy = min(size, sb->s_blocksize);
  1087. memcpy(bhs[n]->b_data, slots, copy);
  1088. slots += copy;
  1089. size -= copy;
  1090. set_buffer_uptodate(bhs[n]);
  1091. mark_buffer_dirty_inode_sync(bhs[n], dir);
  1092. if (!size)
  1093. break;
  1094. n++;
  1095. blknr++;
  1096. }
  1097. } while (++i < *nr_cluster);
  1098. memset(bhs[n]->b_data + copy, 0, sb->s_blocksize - copy);
  1099. offset = copy - sizeof(struct msdos_dir_entry);
  1100. get_bh(bhs[n]);
  1101. *bh = bhs[n];
  1102. *de = (struct msdos_dir_entry *)((*bh)->b_data + offset);
  1103. *i_pos = fat_make_i_pos(sb, *bh, *de);
  1104. /* Second stage: clear the rest of cluster, and write outs */
  1105. err = fat_zeroed_cluster(dir, start_blknr, ++n, bhs, MAX_BUF_PER_PAGE);
  1106. if (err)
  1107. goto error_free;
  1108. return cluster[0];
  1109. error_free:
  1110. brelse(*bh);
  1111. *bh = NULL;
  1112. n = 0;
  1113. error_nomem:
  1114. for (i = 0; i < n; i++)
  1115. bforget(bhs[i]);
  1116. fat_free_clusters(dir, cluster[0]);
  1117. error:
  1118. return err;
  1119. }
  1120. int fat_add_entries(struct inode *dir, void *slots, int nr_slots,
  1121. struct fat_slot_info *sinfo)
  1122. {
  1123. struct super_block *sb = dir->i_sb;
  1124. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  1125. struct buffer_head *bh, *prev, *bhs[3]; /* 32*slots (672bytes) */
  1126. struct msdos_dir_entry *uninitialized_var(de);
  1127. int err, free_slots, i, nr_bhs;
  1128. loff_t pos, i_pos;
  1129. sinfo->nr_slots = nr_slots;
  1130. /* First stage: search free direcotry entries */
  1131. free_slots = nr_bhs = 0;
  1132. bh = prev = NULL;
  1133. pos = 0;
  1134. err = -ENOSPC;
  1135. while (fat_get_entry(dir, &pos, &bh, &de) > -1) {
  1136. /* check the maximum size of directory */
  1137. if (pos >= FAT_MAX_DIR_SIZE)
  1138. goto error;
  1139. if (IS_FREE(de->name)) {
  1140. if (prev != bh) {
  1141. get_bh(bh);
  1142. bhs[nr_bhs] = prev = bh;
  1143. nr_bhs++;
  1144. }
  1145. free_slots++;
  1146. if (free_slots == nr_slots)
  1147. goto found;
  1148. } else {
  1149. for (i = 0; i < nr_bhs; i++)
  1150. brelse(bhs[i]);
  1151. prev = NULL;
  1152. free_slots = nr_bhs = 0;
  1153. }
  1154. }
  1155. if (dir->i_ino == MSDOS_ROOT_INO) {
  1156. if (sbi->fat_bits != 32)
  1157. goto error;
  1158. } else if (MSDOS_I(dir)->i_start == 0) {
  1159. fat_msg(sb, KERN_ERR, "Corrupted directory (i_pos %lld)",
  1160. MSDOS_I(dir)->i_pos);
  1161. err = -EIO;
  1162. goto error;
  1163. }
  1164. found:
  1165. err = 0;
  1166. pos -= free_slots * sizeof(*de);
  1167. nr_slots -= free_slots;
  1168. if (free_slots) {
  1169. /*
  1170. * Second stage: filling the free entries with new entries.
  1171. * NOTE: If this slots has shortname, first, we write
  1172. * the long name slots, then write the short name.
  1173. */
  1174. int size = free_slots * sizeof(*de);
  1175. int offset = pos & (sb->s_blocksize - 1);
  1176. int long_bhs = nr_bhs - (nr_slots == 0);
  1177. /* Fill the long name slots. */
  1178. for (i = 0; i < long_bhs; i++) {
  1179. int copy = min_t(int, sb->s_blocksize - offset, size);
  1180. memcpy(bhs[i]->b_data + offset, slots, copy);
  1181. mark_buffer_dirty_inode_sync(bhs[i], dir);
  1182. offset = 0;
  1183. slots += copy;
  1184. size -= copy;
  1185. }
  1186. if (long_bhs && IS_DIRSYNC(dir))
  1187. err = fat_sync_bhs(bhs, long_bhs);
  1188. if (!err && i < nr_bhs) {
  1189. /* Fill the short name slot. */
  1190. int copy = min_t(int, sb->s_blocksize - offset, size);
  1191. memcpy(bhs[i]->b_data + offset, slots, copy);
  1192. mark_buffer_dirty_inode_sync(bhs[i], dir);
  1193. if (IS_DIRSYNC(dir))
  1194. err = sync_dirty_buffer(bhs[i]);
  1195. }
  1196. for (i = 0; i < nr_bhs; i++)
  1197. brelse(bhs[i]);
  1198. if (err)
  1199. goto error_remove;
  1200. }
  1201. if (nr_slots) {
  1202. int cluster, nr_cluster;
  1203. /*
  1204. * Third stage: allocate the cluster for new entries.
  1205. * And initialize the cluster with new entries, then
  1206. * add the cluster to dir.
  1207. */
  1208. cluster = fat_add_new_entries(dir, slots, nr_slots, &nr_cluster,
  1209. &de, &bh, &i_pos);
  1210. if (cluster < 0) {
  1211. err = cluster;
  1212. goto error_remove;
  1213. }
  1214. err = fat_chain_add(dir, cluster, nr_cluster);
  1215. if (err) {
  1216. fat_free_clusters(dir, cluster);
  1217. goto error_remove;
  1218. }
  1219. if (dir->i_size & (sbi->cluster_size - 1)) {
  1220. fat_fs_error(sb, "Odd directory size");
  1221. dir->i_size = (dir->i_size + sbi->cluster_size - 1)
  1222. & ~((loff_t)sbi->cluster_size - 1);
  1223. }
  1224. dir->i_size += nr_cluster << sbi->cluster_bits;
  1225. MSDOS_I(dir)->mmu_private += nr_cluster << sbi->cluster_bits;
  1226. }
  1227. sinfo->slot_off = pos;
  1228. sinfo->de = de;
  1229. sinfo->bh = bh;
  1230. sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
  1231. return 0;
  1232. error:
  1233. brelse(bh);
  1234. for (i = 0; i < nr_bhs; i++)
  1235. brelse(bhs[i]);
  1236. return err;
  1237. error_remove:
  1238. brelse(bh);
  1239. if (free_slots)
  1240. __fat_remove_entries(dir, pos, free_slots);
  1241. return err;
  1242. }
  1243. EXPORT_SYMBOL_GPL(fat_add_entries);