dir.c 36 KB

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