dir.c 36 KB

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