dir.c 31 KB

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