dir.c 34 KB

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