dir.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
  4. */
  5. #include <linux/slab.h>
  6. #include <linux/compat.h>
  7. #include <linux/bio.h>
  8. #include <linux/buffer_head.h>
  9. #include <linux/filelock.h>
  10. #include "exfat_raw.h"
  11. #include "exfat_fs.h"
  12. static int exfat_extract_uni_name(struct exfat_dentry *ep,
  13. unsigned short *uniname)
  14. {
  15. int i, len = 0;
  16. for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) {
  17. *uniname = le16_to_cpu(ep->dentry.name.unicode_0_14[i]);
  18. if (*uniname == 0x0)
  19. return len;
  20. uniname++;
  21. len++;
  22. }
  23. *uniname = 0x0;
  24. return len;
  25. }
  26. static int exfat_get_uniname_from_ext_entry(struct super_block *sb,
  27. struct exfat_chain *p_dir, int entry, unsigned short *uniname)
  28. {
  29. int i, err;
  30. struct exfat_entry_set_cache es;
  31. unsigned int uni_len = 0, len;
  32. err = exfat_get_dentry_set(&es, sb, p_dir, entry, ES_ALL_ENTRIES);
  33. if (err)
  34. return err;
  35. /*
  36. * First entry : file entry
  37. * Second entry : stream-extension entry
  38. * Third entry : first file-name entry
  39. * So, the index of first file-name dentry should start from 2.
  40. */
  41. for (i = ES_IDX_FIRST_FILENAME; i < es.num_entries; i++) {
  42. struct exfat_dentry *ep = exfat_get_dentry_cached(&es, i);
  43. /* end of name entry */
  44. if (exfat_get_entry_type(ep) != TYPE_EXTEND)
  45. break;
  46. len = exfat_extract_uni_name(ep, uniname);
  47. uni_len += len;
  48. if (len != EXFAT_FILE_NAME_LEN || uni_len >= MAX_NAME_LENGTH)
  49. break;
  50. uniname += EXFAT_FILE_NAME_LEN;
  51. }
  52. exfat_put_dentry_set(&es, false);
  53. return 0;
  54. }
  55. /* read a directory entry from the opened directory */
  56. static int exfat_readdir(struct inode *inode, loff_t *cpos, struct exfat_dir_entry *dir_entry)
  57. {
  58. int i, dentries_per_clu, num_ext, err;
  59. unsigned int type, clu_offset, max_dentries;
  60. struct exfat_chain dir, clu;
  61. struct exfat_uni_name uni_name;
  62. struct exfat_dentry *ep;
  63. struct super_block *sb = inode->i_sb;
  64. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  65. struct exfat_inode_info *ei = EXFAT_I(inode);
  66. unsigned int dentry = EXFAT_B_TO_DEN(*cpos) & 0xFFFFFFFF;
  67. struct buffer_head *bh;
  68. /* check if the given file ID is opened */
  69. if (ei->type != TYPE_DIR)
  70. return -EPERM;
  71. exfat_chain_set(&dir, ei->start_clu,
  72. EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags);
  73. dentries_per_clu = sbi->dentries_per_clu;
  74. max_dentries = (unsigned int)min_t(u64, MAX_EXFAT_DENTRIES,
  75. (u64)EXFAT_CLU_TO_DEN(sbi->num_clusters, sbi));
  76. clu_offset = EXFAT_DEN_TO_CLU(dentry, sbi);
  77. exfat_chain_dup(&clu, &dir);
  78. if (clu.flags == ALLOC_NO_FAT_CHAIN) {
  79. clu.dir += clu_offset;
  80. clu.size -= clu_offset;
  81. } else {
  82. /* hint_information */
  83. if (clu_offset > 0 && ei->hint_bmap.off != EXFAT_EOF_CLUSTER &&
  84. ei->hint_bmap.off > 0 && clu_offset >= ei->hint_bmap.off) {
  85. clu_offset -= ei->hint_bmap.off;
  86. clu.dir = ei->hint_bmap.clu;
  87. }
  88. while (clu_offset > 0 && clu.dir != EXFAT_EOF_CLUSTER) {
  89. if (exfat_get_next_cluster(sb, &(clu.dir)))
  90. return -EIO;
  91. clu_offset--;
  92. }
  93. }
  94. while (clu.dir != EXFAT_EOF_CLUSTER && dentry < max_dentries) {
  95. i = dentry & (dentries_per_clu - 1);
  96. for ( ; i < dentries_per_clu; i++, dentry++) {
  97. ep = exfat_get_dentry(sb, &clu, i, &bh);
  98. if (!ep)
  99. return -EIO;
  100. type = exfat_get_entry_type(ep);
  101. if (type == TYPE_UNUSED) {
  102. brelse(bh);
  103. goto out;
  104. }
  105. if (type != TYPE_FILE && type != TYPE_DIR) {
  106. brelse(bh);
  107. continue;
  108. }
  109. num_ext = ep->dentry.file.num_ext;
  110. dir_entry->attr = le16_to_cpu(ep->dentry.file.attr);
  111. *uni_name.name = 0x0;
  112. err = exfat_get_uniname_from_ext_entry(sb, &clu, i,
  113. uni_name.name);
  114. if (err) {
  115. brelse(bh);
  116. continue;
  117. }
  118. exfat_utf16_to_nls(sb, &uni_name,
  119. dir_entry->namebuf.lfn,
  120. dir_entry->namebuf.lfnbuf_len);
  121. brelse(bh);
  122. ep = exfat_get_dentry(sb, &clu, i + 1, &bh);
  123. if (!ep)
  124. return -EIO;
  125. dir_entry->entry = i;
  126. dir_entry->dir = clu;
  127. brelse(bh);
  128. ei->hint_bmap.off = EXFAT_DEN_TO_CLU(dentry, sbi);
  129. ei->hint_bmap.clu = clu.dir;
  130. *cpos = EXFAT_DEN_TO_B(dentry + 1 + num_ext);
  131. return 0;
  132. }
  133. if (clu.flags == ALLOC_NO_FAT_CHAIN) {
  134. if (--clu.size > 0)
  135. clu.dir++;
  136. else
  137. clu.dir = EXFAT_EOF_CLUSTER;
  138. } else {
  139. if (exfat_get_next_cluster(sb, &(clu.dir)))
  140. return -EIO;
  141. }
  142. }
  143. out:
  144. dir_entry->namebuf.lfn[0] = '\0';
  145. *cpos = EXFAT_DEN_TO_B(dentry);
  146. return 0;
  147. }
  148. static void exfat_init_namebuf(struct exfat_dentry_namebuf *nb)
  149. {
  150. nb->lfn = NULL;
  151. nb->lfnbuf_len = 0;
  152. }
  153. static int exfat_alloc_namebuf(struct exfat_dentry_namebuf *nb)
  154. {
  155. nb->lfn = __getname();
  156. if (!nb->lfn)
  157. return -ENOMEM;
  158. nb->lfnbuf_len = MAX_VFSNAME_BUF_SIZE;
  159. return 0;
  160. }
  161. static void exfat_free_namebuf(struct exfat_dentry_namebuf *nb)
  162. {
  163. if (!nb->lfn)
  164. return;
  165. __putname(nb->lfn);
  166. exfat_init_namebuf(nb);
  167. }
  168. /*
  169. * Before calling dir_emit*(), sbi->s_lock should be released
  170. * because page fault can occur in dir_emit*().
  171. */
  172. #define ITER_POS_FILLED_DOTS (2)
  173. static int exfat_iterate(struct file *file, struct dir_context *ctx)
  174. {
  175. struct inode *inode = file_inode(file);
  176. struct super_block *sb = inode->i_sb;
  177. struct inode *tmp;
  178. struct exfat_dir_entry de;
  179. struct exfat_dentry_namebuf *nb = &(de.namebuf);
  180. struct exfat_inode_info *ei = EXFAT_I(inode);
  181. unsigned long inum;
  182. loff_t cpos, i_pos;
  183. int err = 0, fake_offset = 0;
  184. exfat_init_namebuf(nb);
  185. cpos = ctx->pos;
  186. if (!dir_emit_dots(file, ctx))
  187. goto out;
  188. if (ctx->pos == ITER_POS_FILLED_DOTS) {
  189. cpos = 0;
  190. fake_offset = 1;
  191. }
  192. cpos = round_up(cpos, DENTRY_SIZE);
  193. /* name buffer should be allocated before use */
  194. err = exfat_alloc_namebuf(nb);
  195. if (err)
  196. goto out;
  197. get_new:
  198. mutex_lock(&EXFAT_SB(sb)->s_lock);
  199. if (ei->flags == ALLOC_NO_FAT_CHAIN && cpos >= i_size_read(inode))
  200. goto end_of_dir;
  201. err = exfat_readdir(inode, &cpos, &de);
  202. if (err) {
  203. /*
  204. * At least we tried to read a sector.
  205. * Move cpos to next sector position (should be aligned).
  206. */
  207. if (err == -EIO) {
  208. cpos += 1 << (sb->s_blocksize_bits);
  209. cpos &= ~(sb->s_blocksize - 1);
  210. }
  211. err = -EIO;
  212. goto end_of_dir;
  213. }
  214. if (!nb->lfn[0])
  215. goto end_of_dir;
  216. i_pos = ((loff_t)de.dir.dir << 32) | (de.entry & 0xffffffff);
  217. tmp = exfat_iget(sb, i_pos);
  218. if (tmp) {
  219. inum = tmp->i_ino;
  220. iput(tmp);
  221. } else {
  222. inum = iunique(sb, EXFAT_ROOT_INO);
  223. }
  224. mutex_unlock(&EXFAT_SB(sb)->s_lock);
  225. if (!dir_emit(ctx, nb->lfn, strlen(nb->lfn), inum,
  226. (de.attr & EXFAT_ATTR_SUBDIR) ? DT_DIR : DT_REG))
  227. goto out;
  228. ctx->pos = cpos;
  229. goto get_new;
  230. end_of_dir:
  231. if (!cpos && fake_offset)
  232. cpos = ITER_POS_FILLED_DOTS;
  233. ctx->pos = cpos;
  234. mutex_unlock(&EXFAT_SB(sb)->s_lock);
  235. out:
  236. /*
  237. * To improve performance, free namebuf after unlock sb_lock.
  238. * If namebuf is not allocated, this function do nothing
  239. */
  240. exfat_free_namebuf(nb);
  241. return err;
  242. }
  243. WRAP_DIR_ITER(exfat_iterate) // FIXME!
  244. const struct file_operations exfat_dir_operations = {
  245. .llseek = generic_file_llseek,
  246. .read = generic_read_dir,
  247. .iterate_shared = shared_exfat_iterate,
  248. .unlocked_ioctl = exfat_ioctl,
  249. #ifdef CONFIG_COMPAT
  250. .compat_ioctl = exfat_compat_ioctl,
  251. #endif
  252. .fsync = exfat_file_fsync,
  253. .setlease = generic_setlease,
  254. };
  255. int exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu)
  256. {
  257. int ret;
  258. exfat_chain_set(clu, EXFAT_EOF_CLUSTER, 0, ALLOC_NO_FAT_CHAIN);
  259. ret = exfat_alloc_cluster(inode, 1, clu, IS_DIRSYNC(inode));
  260. if (ret)
  261. return ret;
  262. return exfat_zeroed_cluster(inode, clu->dir);
  263. }
  264. int exfat_calc_num_entries(struct exfat_uni_name *p_uniname)
  265. {
  266. int len;
  267. len = p_uniname->name_len;
  268. if (len == 0)
  269. return -EINVAL;
  270. /* 1 file entry + 1 stream entry + name entries */
  271. return ES_ENTRY_NUM(len);
  272. }
  273. unsigned int exfat_get_entry_type(struct exfat_dentry *ep)
  274. {
  275. if (ep->type == EXFAT_UNUSED)
  276. return TYPE_UNUSED;
  277. if (IS_EXFAT_DELETED(ep->type))
  278. return TYPE_DELETED;
  279. if (ep->type == EXFAT_INVAL)
  280. return TYPE_INVALID;
  281. if (IS_EXFAT_CRITICAL_PRI(ep->type)) {
  282. if (ep->type == EXFAT_BITMAP)
  283. return TYPE_BITMAP;
  284. if (ep->type == EXFAT_UPCASE)
  285. return TYPE_UPCASE;
  286. if (ep->type == EXFAT_VOLUME)
  287. return TYPE_VOLUME;
  288. if (ep->type == EXFAT_FILE) {
  289. if (le16_to_cpu(ep->dentry.file.attr) & EXFAT_ATTR_SUBDIR)
  290. return TYPE_DIR;
  291. return TYPE_FILE;
  292. }
  293. return TYPE_CRITICAL_PRI;
  294. }
  295. if (IS_EXFAT_BENIGN_PRI(ep->type)) {
  296. if (ep->type == EXFAT_GUID)
  297. return TYPE_GUID;
  298. if (ep->type == EXFAT_PADDING)
  299. return TYPE_PADDING;
  300. if (ep->type == EXFAT_ACLTAB)
  301. return TYPE_ACLTAB;
  302. return TYPE_BENIGN_PRI;
  303. }
  304. if (IS_EXFAT_CRITICAL_SEC(ep->type)) {
  305. if (ep->type == EXFAT_STREAM)
  306. return TYPE_STREAM;
  307. if (ep->type == EXFAT_NAME)
  308. return TYPE_EXTEND;
  309. if (ep->type == EXFAT_ACL)
  310. return TYPE_ACL;
  311. return TYPE_CRITICAL_SEC;
  312. }
  313. if (ep->type == EXFAT_VENDOR_EXT)
  314. return TYPE_VENDOR_EXT;
  315. if (ep->type == EXFAT_VENDOR_ALLOC)
  316. return TYPE_VENDOR_ALLOC;
  317. return TYPE_BENIGN_SEC;
  318. }
  319. static void exfat_set_entry_type(struct exfat_dentry *ep, unsigned int type)
  320. {
  321. if (type == TYPE_UNUSED) {
  322. ep->type = EXFAT_UNUSED;
  323. } else if (type == TYPE_DELETED) {
  324. ep->type &= EXFAT_DELETE;
  325. } else if (type == TYPE_STREAM) {
  326. ep->type = EXFAT_STREAM;
  327. } else if (type == TYPE_EXTEND) {
  328. ep->type = EXFAT_NAME;
  329. } else if (type == TYPE_BITMAP) {
  330. ep->type = EXFAT_BITMAP;
  331. } else if (type == TYPE_UPCASE) {
  332. ep->type = EXFAT_UPCASE;
  333. } else if (type == TYPE_VOLUME) {
  334. ep->type = EXFAT_VOLUME;
  335. } else if (type == TYPE_DIR) {
  336. ep->type = EXFAT_FILE;
  337. ep->dentry.file.attr = cpu_to_le16(EXFAT_ATTR_SUBDIR);
  338. } else if (type == TYPE_FILE) {
  339. ep->type = EXFAT_FILE;
  340. ep->dentry.file.attr = cpu_to_le16(EXFAT_ATTR_ARCHIVE);
  341. }
  342. }
  343. static void exfat_init_stream_entry(struct exfat_dentry *ep,
  344. unsigned int start_clu, unsigned long long size)
  345. {
  346. memset(ep, 0, sizeof(*ep));
  347. exfat_set_entry_type(ep, TYPE_STREAM);
  348. if (size == 0)
  349. ep->dentry.stream.flags = ALLOC_FAT_CHAIN;
  350. else
  351. ep->dentry.stream.flags = ALLOC_NO_FAT_CHAIN;
  352. ep->dentry.stream.start_clu = cpu_to_le32(start_clu);
  353. ep->dentry.stream.valid_size = cpu_to_le64(size);
  354. ep->dentry.stream.size = cpu_to_le64(size);
  355. }
  356. static void exfat_init_name_entry(struct exfat_dentry *ep,
  357. unsigned short *uniname)
  358. {
  359. int i;
  360. exfat_set_entry_type(ep, TYPE_EXTEND);
  361. ep->dentry.name.flags = 0x0;
  362. for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) {
  363. if (*uniname != 0x0) {
  364. ep->dentry.name.unicode_0_14[i] = cpu_to_le16(*uniname);
  365. uniname++;
  366. } else {
  367. ep->dentry.name.unicode_0_14[i] = 0x0;
  368. }
  369. }
  370. }
  371. void exfat_init_dir_entry(struct exfat_entry_set_cache *es,
  372. unsigned int type, unsigned int start_clu,
  373. unsigned long long size, struct timespec64 *ts)
  374. {
  375. struct super_block *sb = es->sb;
  376. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  377. struct exfat_dentry *ep;
  378. ep = exfat_get_dentry_cached(es, ES_IDX_FILE);
  379. memset(ep, 0, sizeof(*ep));
  380. exfat_set_entry_type(ep, type);
  381. exfat_set_entry_time(sbi, ts,
  382. &ep->dentry.file.create_tz,
  383. &ep->dentry.file.create_time,
  384. &ep->dentry.file.create_date,
  385. &ep->dentry.file.create_time_cs);
  386. exfat_set_entry_time(sbi, ts,
  387. &ep->dentry.file.modify_tz,
  388. &ep->dentry.file.modify_time,
  389. &ep->dentry.file.modify_date,
  390. &ep->dentry.file.modify_time_cs);
  391. exfat_set_entry_time(sbi, ts,
  392. &ep->dentry.file.access_tz,
  393. &ep->dentry.file.access_time,
  394. &ep->dentry.file.access_date,
  395. NULL);
  396. ep = exfat_get_dentry_cached(es, ES_IDX_STREAM);
  397. exfat_init_stream_entry(ep, start_clu, size);
  398. }
  399. static void exfat_free_benign_secondary_clusters(struct inode *inode,
  400. struct exfat_dentry *ep)
  401. {
  402. struct super_block *sb = inode->i_sb;
  403. struct exfat_chain dir;
  404. unsigned int start_clu =
  405. le32_to_cpu(ep->dentry.generic_secondary.start_clu);
  406. u64 size = le64_to_cpu(ep->dentry.generic_secondary.size);
  407. unsigned char flags = ep->dentry.generic_secondary.flags;
  408. if (!(flags & ALLOC_POSSIBLE) || !start_clu || !size)
  409. return;
  410. exfat_chain_set(&dir, start_clu,
  411. EXFAT_B_TO_CLU_ROUND_UP(size, EXFAT_SB(sb)),
  412. flags);
  413. exfat_free_cluster(inode, &dir);
  414. }
  415. void exfat_init_ext_entry(struct exfat_entry_set_cache *es, int num_entries,
  416. struct exfat_uni_name *p_uniname)
  417. {
  418. int i;
  419. unsigned short *uniname = p_uniname->name;
  420. struct exfat_dentry *ep;
  421. ep = exfat_get_dentry_cached(es, ES_IDX_FILE);
  422. ep->dentry.file.num_ext = (unsigned char)(num_entries - 1);
  423. ep = exfat_get_dentry_cached(es, ES_IDX_STREAM);
  424. ep->dentry.stream.name_len = p_uniname->name_len;
  425. ep->dentry.stream.name_hash = cpu_to_le16(p_uniname->name_hash);
  426. for (i = ES_IDX_FIRST_FILENAME; i < num_entries; i++) {
  427. ep = exfat_get_dentry_cached(es, i);
  428. exfat_init_name_entry(ep, uniname);
  429. uniname += EXFAT_FILE_NAME_LEN;
  430. }
  431. exfat_update_dir_chksum(es);
  432. }
  433. void exfat_remove_entries(struct inode *inode, struct exfat_entry_set_cache *es,
  434. int order)
  435. {
  436. int i;
  437. struct exfat_dentry *ep;
  438. for (i = order; i < es->num_entries; i++) {
  439. ep = exfat_get_dentry_cached(es, i);
  440. if (exfat_get_entry_type(ep) & TYPE_BENIGN_SEC)
  441. exfat_free_benign_secondary_clusters(inode, ep);
  442. exfat_set_entry_type(ep, TYPE_DELETED);
  443. }
  444. if (order < es->num_entries)
  445. es->modified = true;
  446. }
  447. void exfat_update_dir_chksum(struct exfat_entry_set_cache *es)
  448. {
  449. int chksum_type = CS_DIR_ENTRY, i;
  450. unsigned short chksum = 0;
  451. struct exfat_dentry *ep;
  452. for (i = ES_IDX_FILE; i < es->num_entries; i++) {
  453. ep = exfat_get_dentry_cached(es, i);
  454. chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
  455. chksum_type);
  456. chksum_type = CS_DEFAULT;
  457. }
  458. ep = exfat_get_dentry_cached(es, ES_IDX_FILE);
  459. ep->dentry.file.checksum = cpu_to_le16(chksum);
  460. es->modified = true;
  461. }
  462. int exfat_put_dentry_set(struct exfat_entry_set_cache *es, int sync)
  463. {
  464. int i, err = 0;
  465. if (es->modified)
  466. err = exfat_update_bhs(es->bh, es->num_bh, sync);
  467. for (i = 0; i < es->num_bh; i++)
  468. if (err)
  469. bforget(es->bh[i]);
  470. else
  471. brelse(es->bh[i]);
  472. if (IS_DYNAMIC_ES(es))
  473. kfree(es->bh);
  474. return err;
  475. }
  476. static int exfat_walk_fat_chain(struct super_block *sb,
  477. struct exfat_chain *p_dir, unsigned int byte_offset,
  478. unsigned int *clu)
  479. {
  480. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  481. unsigned int clu_offset;
  482. unsigned int cur_clu;
  483. clu_offset = EXFAT_B_TO_CLU(byte_offset, sbi);
  484. cur_clu = p_dir->dir;
  485. if (p_dir->flags == ALLOC_NO_FAT_CHAIN) {
  486. cur_clu += clu_offset;
  487. } else {
  488. while (clu_offset > 0) {
  489. if (exfat_get_next_cluster(sb, &cur_clu))
  490. return -EIO;
  491. if (cur_clu == EXFAT_EOF_CLUSTER) {
  492. exfat_fs_error(sb,
  493. "invalid dentry access beyond EOF (clu : %u, eidx : %d)",
  494. p_dir->dir,
  495. EXFAT_B_TO_DEN(byte_offset));
  496. return -EIO;
  497. }
  498. clu_offset--;
  499. }
  500. }
  501. *clu = cur_clu;
  502. return 0;
  503. }
  504. static int exfat_find_location(struct super_block *sb, struct exfat_chain *p_dir,
  505. int entry, sector_t *sector, int *offset)
  506. {
  507. int ret;
  508. unsigned int off, clu = 0;
  509. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  510. off = EXFAT_DEN_TO_B(entry);
  511. ret = exfat_walk_fat_chain(sb, p_dir, off, &clu);
  512. if (ret)
  513. return ret;
  514. if (!exfat_test_bitmap(sb, clu)) {
  515. exfat_err(sb, "failed to test cluster bit(%u)", clu);
  516. return -EIO;
  517. }
  518. /* byte offset in cluster */
  519. off = EXFAT_CLU_OFFSET(off, sbi);
  520. /* byte offset in sector */
  521. *offset = EXFAT_BLK_OFFSET(off, sb);
  522. /* sector offset in cluster */
  523. *sector = EXFAT_B_TO_BLK(off, sb);
  524. *sector += exfat_cluster_to_sector(sbi, clu);
  525. return 0;
  526. }
  527. #define EXFAT_MAX_RA_SIZE (128*1024)
  528. static int exfat_dir_readahead(struct super_block *sb, sector_t sec)
  529. {
  530. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  531. struct buffer_head *bh;
  532. unsigned int max_ra_count = EXFAT_MAX_RA_SIZE >> sb->s_blocksize_bits;
  533. unsigned int page_ra_count = PAGE_SIZE >> sb->s_blocksize_bits;
  534. unsigned int adj_ra_count = max(sbi->sect_per_clus, page_ra_count);
  535. unsigned int ra_count = min(adj_ra_count, max_ra_count);
  536. /* Read-ahead is not required */
  537. if (sbi->sect_per_clus == 1)
  538. return 0;
  539. if (sec < sbi->data_start_sector) {
  540. exfat_err(sb, "requested sector is invalid(sect:%llu, root:%llu)",
  541. (unsigned long long)sec, sbi->data_start_sector);
  542. return -EIO;
  543. }
  544. /* Not sector aligned with ra_count, resize ra_count to page size */
  545. if ((sec - sbi->data_start_sector) & (ra_count - 1))
  546. ra_count = page_ra_count;
  547. bh = sb_find_get_block(sb, sec);
  548. if (!bh || !buffer_uptodate(bh)) {
  549. unsigned int i;
  550. for (i = 0; i < ra_count; i++)
  551. sb_breadahead(sb, (sector_t)(sec + i));
  552. }
  553. brelse(bh);
  554. return 0;
  555. }
  556. struct exfat_dentry *exfat_get_dentry(struct super_block *sb,
  557. struct exfat_chain *p_dir, int entry, struct buffer_head **bh)
  558. {
  559. unsigned int dentries_per_page = EXFAT_B_TO_DEN(PAGE_SIZE);
  560. int off;
  561. sector_t sec;
  562. if (p_dir->dir == DIR_DELETED) {
  563. exfat_err(sb, "abnormal access to deleted dentry");
  564. return NULL;
  565. }
  566. if (exfat_find_location(sb, p_dir, entry, &sec, &off))
  567. return NULL;
  568. if (p_dir->dir != EXFAT_FREE_CLUSTER &&
  569. !(entry & (dentries_per_page - 1)))
  570. exfat_dir_readahead(sb, sec);
  571. *bh = sb_bread(sb, sec);
  572. if (!*bh)
  573. return NULL;
  574. return (struct exfat_dentry *)((*bh)->b_data + off);
  575. }
  576. enum exfat_validate_dentry_mode {
  577. ES_MODE_GET_FILE_ENTRY,
  578. ES_MODE_GET_STRM_ENTRY,
  579. ES_MODE_GET_NAME_ENTRY,
  580. ES_MODE_GET_CRITICAL_SEC_ENTRY,
  581. ES_MODE_GET_BENIGN_SEC_ENTRY,
  582. };
  583. static bool exfat_validate_entry(unsigned int type,
  584. enum exfat_validate_dentry_mode *mode)
  585. {
  586. if (type == TYPE_UNUSED || type == TYPE_DELETED)
  587. return false;
  588. switch (*mode) {
  589. case ES_MODE_GET_FILE_ENTRY:
  590. if (type != TYPE_STREAM)
  591. return false;
  592. *mode = ES_MODE_GET_STRM_ENTRY;
  593. break;
  594. case ES_MODE_GET_STRM_ENTRY:
  595. if (type != TYPE_EXTEND)
  596. return false;
  597. *mode = ES_MODE_GET_NAME_ENTRY;
  598. break;
  599. case ES_MODE_GET_NAME_ENTRY:
  600. if (type & TYPE_BENIGN_SEC)
  601. *mode = ES_MODE_GET_BENIGN_SEC_ENTRY;
  602. else if (type != TYPE_EXTEND)
  603. return false;
  604. break;
  605. case ES_MODE_GET_BENIGN_SEC_ENTRY:
  606. /* Assume unreconized benign secondary entry */
  607. if (!(type & TYPE_BENIGN_SEC))
  608. return false;
  609. break;
  610. default:
  611. return false;
  612. }
  613. return true;
  614. }
  615. struct exfat_dentry *exfat_get_dentry_cached(
  616. struct exfat_entry_set_cache *es, int num)
  617. {
  618. int off = es->start_off + num * DENTRY_SIZE;
  619. struct buffer_head *bh = es->bh[EXFAT_B_TO_BLK(off, es->sb)];
  620. char *p = bh->b_data + EXFAT_BLK_OFFSET(off, es->sb);
  621. return (struct exfat_dentry *)p;
  622. }
  623. /*
  624. * Returns a set of dentries.
  625. *
  626. * Note It provides a direct pointer to bh->data via exfat_get_dentry_cached().
  627. * User should call exfat_get_dentry_set() after setting 'modified' to apply
  628. * changes made in this entry set to the real device.
  629. *
  630. * in:
  631. * sb+p_dir+entry: indicates a file/dir
  632. * num_entries: specifies how many dentries should be included.
  633. * It will be set to es->num_entries if it is not 0.
  634. * If num_entries is 0, es->num_entries will be obtained
  635. * from the first dentry.
  636. * out:
  637. * es: pointer of entry set on success.
  638. * return:
  639. * 0 on success
  640. * -error code on failure
  641. */
  642. static int __exfat_get_dentry_set(struct exfat_entry_set_cache *es,
  643. struct super_block *sb, struct exfat_chain *p_dir, int entry,
  644. unsigned int num_entries)
  645. {
  646. int ret, i, num_bh;
  647. unsigned int off;
  648. sector_t sec;
  649. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  650. struct buffer_head *bh;
  651. if (p_dir->dir == DIR_DELETED) {
  652. exfat_err(sb, "access to deleted dentry");
  653. return -EIO;
  654. }
  655. ret = exfat_find_location(sb, p_dir, entry, &sec, &off);
  656. if (ret)
  657. return ret;
  658. memset(es, 0, sizeof(*es));
  659. es->sb = sb;
  660. es->modified = false;
  661. es->start_off = off;
  662. es->bh = es->__bh;
  663. bh = sb_bread(sb, sec);
  664. if (!bh)
  665. return -EIO;
  666. es->bh[es->num_bh++] = bh;
  667. if (num_entries == ES_ALL_ENTRIES) {
  668. struct exfat_dentry *ep;
  669. ep = exfat_get_dentry_cached(es, ES_IDX_FILE);
  670. if (ep->type != EXFAT_FILE) {
  671. brelse(bh);
  672. return -EIO;
  673. }
  674. num_entries = ep->dentry.file.num_ext + 1;
  675. }
  676. es->num_entries = num_entries;
  677. num_bh = EXFAT_B_TO_BLK_ROUND_UP(off + num_entries * DENTRY_SIZE, sb);
  678. if (num_bh > ARRAY_SIZE(es->__bh)) {
  679. es->bh = kmalloc_objs(*es->bh, num_bh, GFP_NOFS);
  680. if (!es->bh) {
  681. brelse(bh);
  682. return -ENOMEM;
  683. }
  684. es->bh[0] = bh;
  685. }
  686. for (i = 1; i < num_bh; i++) {
  687. /* get the next sector */
  688. if (exfat_is_last_sector_in_cluster(sbi, sec)) {
  689. unsigned int clu = exfat_sector_to_cluster(sbi, sec);
  690. if (p_dir->flags == ALLOC_NO_FAT_CHAIN)
  691. clu++;
  692. else if (exfat_get_next_cluster(sb, &clu))
  693. goto put_es;
  694. sec = exfat_cluster_to_sector(sbi, clu);
  695. } else {
  696. sec++;
  697. }
  698. bh = sb_bread(sb, sec);
  699. if (!bh)
  700. goto put_es;
  701. es->bh[es->num_bh++] = bh;
  702. }
  703. return 0;
  704. put_es:
  705. exfat_put_dentry_set(es, false);
  706. return -EIO;
  707. }
  708. int exfat_get_dentry_set(struct exfat_entry_set_cache *es,
  709. struct super_block *sb, struct exfat_chain *p_dir,
  710. int entry, unsigned int num_entries)
  711. {
  712. int ret, i;
  713. struct exfat_dentry *ep;
  714. enum exfat_validate_dentry_mode mode = ES_MODE_GET_FILE_ENTRY;
  715. ret = __exfat_get_dentry_set(es, sb, p_dir, entry, num_entries);
  716. if (ret < 0)
  717. return ret;
  718. /* validate cached dentries */
  719. for (i = ES_IDX_STREAM; i < es->num_entries; i++) {
  720. ep = exfat_get_dentry_cached(es, i);
  721. if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
  722. goto put_es;
  723. }
  724. return 0;
  725. put_es:
  726. exfat_put_dentry_set(es, false);
  727. return -EIO;
  728. }
  729. static int exfat_validate_empty_dentry_set(struct exfat_entry_set_cache *es)
  730. {
  731. struct exfat_dentry *ep;
  732. struct buffer_head *bh;
  733. int i, off;
  734. bool unused_hit = false;
  735. /*
  736. * ONLY UNUSED OR DELETED DENTRIES ARE ALLOWED:
  737. * Although it violates the specification for a deleted entry to
  738. * follow an unused entry, some exFAT implementations could work
  739. * like this. Therefore, to improve compatibility, let's allow it.
  740. */
  741. for (i = 0; i < es->num_entries; i++) {
  742. ep = exfat_get_dentry_cached(es, i);
  743. if (ep->type == EXFAT_UNUSED) {
  744. unused_hit = true;
  745. } else if (!IS_EXFAT_DELETED(ep->type)) {
  746. if (unused_hit)
  747. goto err_used_follow_unused;
  748. i++;
  749. goto count_skip_entries;
  750. }
  751. }
  752. return 0;
  753. err_used_follow_unused:
  754. off = es->start_off + (i << DENTRY_SIZE_BITS);
  755. bh = es->bh[EXFAT_B_TO_BLK(off, es->sb)];
  756. exfat_fs_error(es->sb,
  757. "in sector %lld, dentry %d should be unused, but 0x%x",
  758. bh->b_blocknr, off >> DENTRY_SIZE_BITS, ep->type);
  759. return -EIO;
  760. count_skip_entries:
  761. es->num_entries = EXFAT_B_TO_DEN(EXFAT_BLK_TO_B(es->num_bh, es->sb) - es->start_off);
  762. for (; i < es->num_entries; i++) {
  763. ep = exfat_get_dentry_cached(es, i);
  764. if (IS_EXFAT_DELETED(ep->type))
  765. break;
  766. }
  767. return i;
  768. }
  769. /*
  770. * Get an empty dentry set.
  771. *
  772. * in:
  773. * sb+p_dir+entry: indicates the empty dentry location
  774. * num_entries: specifies how many empty dentries should be included.
  775. * out:
  776. * es: pointer of empty dentry set on success.
  777. * return:
  778. * 0 : on success
  779. * >0 : the dentries are not empty, the return value is the number of
  780. * dentries to be skipped for the next lookup.
  781. * <0 : on failure
  782. */
  783. int exfat_get_empty_dentry_set(struct exfat_entry_set_cache *es,
  784. struct super_block *sb, struct exfat_chain *p_dir,
  785. int entry, unsigned int num_entries)
  786. {
  787. int ret;
  788. ret = __exfat_get_dentry_set(es, sb, p_dir, entry, num_entries);
  789. if (ret < 0)
  790. return ret;
  791. ret = exfat_validate_empty_dentry_set(es);
  792. if (ret)
  793. exfat_put_dentry_set(es, false);
  794. return ret;
  795. }
  796. static inline void exfat_reset_empty_hint(struct exfat_hint_femp *hint_femp)
  797. {
  798. hint_femp->eidx = EXFAT_HINT_NONE;
  799. hint_femp->count = 0;
  800. }
  801. static inline void exfat_set_empty_hint(struct exfat_inode_info *ei,
  802. struct exfat_hint_femp *candi_empty, struct exfat_chain *clu,
  803. int dentry, int num_entries, int entry_type)
  804. {
  805. if (ei->hint_femp.eidx == EXFAT_HINT_NONE ||
  806. ei->hint_femp.eidx > dentry) {
  807. int total_entries = EXFAT_B_TO_DEN(i_size_read(&ei->vfs_inode));
  808. if (candi_empty->count == 0) {
  809. candi_empty->cur = *clu;
  810. candi_empty->eidx = dentry;
  811. }
  812. if (entry_type == TYPE_UNUSED)
  813. candi_empty->count += total_entries - dentry;
  814. else
  815. candi_empty->count++;
  816. if (candi_empty->count == num_entries ||
  817. candi_empty->count + candi_empty->eidx == total_entries)
  818. ei->hint_femp = *candi_empty;
  819. }
  820. }
  821. enum {
  822. DIRENT_STEP_FILE,
  823. DIRENT_STEP_STRM,
  824. DIRENT_STEP_NAME,
  825. DIRENT_STEP_SECD,
  826. };
  827. /*
  828. * @ei: inode info of parent directory
  829. * @p_dir: directory structure of parent directory
  830. * @num_entries:entry size of p_uniname
  831. * @hint_opt: If p_uniname is found, filled with optimized dir/entry
  832. * for traversing cluster chain.
  833. * @return:
  834. * >= 0: file directory entry position where the name exists
  835. * -ENOENT: entry with the name does not exist
  836. * -EIO: I/O error
  837. */
  838. int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
  839. struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
  840. struct exfat_hint *hint_opt)
  841. {
  842. int i, rewind = 0, dentry = 0, end_eidx = 0, num_ext = 0, len;
  843. int order, step, name_len = 0;
  844. int dentries_per_clu;
  845. unsigned int entry_type;
  846. unsigned short *uniname = NULL;
  847. struct exfat_chain clu;
  848. struct exfat_hint *hint_stat = &ei->hint_stat;
  849. struct exfat_hint_femp candi_empty;
  850. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  851. int num_entries = exfat_calc_num_entries(p_uniname);
  852. unsigned int clu_count = 0;
  853. if (num_entries < 0)
  854. return num_entries;
  855. dentries_per_clu = sbi->dentries_per_clu;
  856. exfat_chain_dup(&clu, p_dir);
  857. if (hint_stat->eidx) {
  858. clu.dir = hint_stat->clu;
  859. dentry = hint_stat->eidx;
  860. end_eidx = dentry;
  861. }
  862. exfat_reset_empty_hint(&ei->hint_femp);
  863. rewind:
  864. order = 0;
  865. step = DIRENT_STEP_FILE;
  866. exfat_reset_empty_hint(&candi_empty);
  867. while (clu.dir != EXFAT_EOF_CLUSTER) {
  868. i = dentry & (dentries_per_clu - 1);
  869. for (; i < dentries_per_clu; i++, dentry++) {
  870. struct exfat_dentry *ep;
  871. struct buffer_head *bh;
  872. if (rewind && dentry == end_eidx)
  873. goto not_found;
  874. ep = exfat_get_dentry(sb, &clu, i, &bh);
  875. if (!ep)
  876. return -EIO;
  877. entry_type = exfat_get_entry_type(ep);
  878. if (entry_type == TYPE_UNUSED ||
  879. entry_type == TYPE_DELETED) {
  880. step = DIRENT_STEP_FILE;
  881. exfat_set_empty_hint(ei, &candi_empty, &clu,
  882. dentry, num_entries,
  883. entry_type);
  884. brelse(bh);
  885. if (entry_type == TYPE_UNUSED)
  886. goto not_found;
  887. continue;
  888. }
  889. exfat_reset_empty_hint(&candi_empty);
  890. if (entry_type == TYPE_FILE || entry_type == TYPE_DIR) {
  891. step = DIRENT_STEP_FILE;
  892. hint_opt->clu = clu.dir;
  893. hint_opt->eidx = i;
  894. num_ext = ep->dentry.file.num_ext;
  895. step = DIRENT_STEP_STRM;
  896. brelse(bh);
  897. continue;
  898. }
  899. if (entry_type == TYPE_STREAM) {
  900. u16 name_hash;
  901. if (step != DIRENT_STEP_STRM) {
  902. step = DIRENT_STEP_FILE;
  903. brelse(bh);
  904. continue;
  905. }
  906. step = DIRENT_STEP_FILE;
  907. name_hash = le16_to_cpu(
  908. ep->dentry.stream.name_hash);
  909. if (p_uniname->name_hash == name_hash &&
  910. p_uniname->name_len ==
  911. ep->dentry.stream.name_len) {
  912. step = DIRENT_STEP_NAME;
  913. order = 1;
  914. name_len = 0;
  915. }
  916. brelse(bh);
  917. continue;
  918. }
  919. brelse(bh);
  920. if (entry_type == TYPE_EXTEND) {
  921. unsigned short entry_uniname[16], unichar;
  922. if (step != DIRENT_STEP_NAME ||
  923. name_len >= MAX_NAME_LENGTH) {
  924. step = DIRENT_STEP_FILE;
  925. continue;
  926. }
  927. if (++order == 2)
  928. uniname = p_uniname->name;
  929. else
  930. uniname += EXFAT_FILE_NAME_LEN;
  931. len = exfat_extract_uni_name(ep, entry_uniname);
  932. name_len += len;
  933. unichar = *(uniname+len);
  934. *(uniname+len) = 0x0;
  935. if (exfat_uniname_ncmp(sb, uniname,
  936. entry_uniname, len)) {
  937. step = DIRENT_STEP_FILE;
  938. } else if (p_uniname->name_len == name_len) {
  939. if (order == num_ext)
  940. goto found;
  941. step = DIRENT_STEP_SECD;
  942. }
  943. *(uniname+len) = unichar;
  944. continue;
  945. }
  946. if (entry_type &
  947. (TYPE_CRITICAL_SEC | TYPE_BENIGN_SEC)) {
  948. if (step == DIRENT_STEP_SECD) {
  949. if (++order == num_ext)
  950. goto found;
  951. continue;
  952. }
  953. }
  954. step = DIRENT_STEP_FILE;
  955. }
  956. if (clu.flags == ALLOC_NO_FAT_CHAIN) {
  957. if (--clu.size > 0)
  958. clu.dir++;
  959. else
  960. clu.dir = EXFAT_EOF_CLUSTER;
  961. } else {
  962. if (exfat_get_next_cluster(sb, &clu.dir))
  963. return -EIO;
  964. /* break if the cluster chain includes a loop */
  965. if (unlikely(++clu_count > EXFAT_DATA_CLUSTER_COUNT(sbi)))
  966. goto not_found;
  967. }
  968. }
  969. not_found:
  970. /*
  971. * We started at not 0 index,so we should try to find target
  972. * from 0 index to the index we started at.
  973. */
  974. if (!rewind && end_eidx) {
  975. rewind = 1;
  976. dentry = 0;
  977. clu.dir = p_dir->dir;
  978. goto rewind;
  979. }
  980. /*
  981. * set the EXFAT_EOF_CLUSTER flag to avoid search
  982. * from the beginning again when allocated a new cluster
  983. */
  984. if (ei->hint_femp.eidx == EXFAT_HINT_NONE) {
  985. ei->hint_femp.cur.dir = EXFAT_EOF_CLUSTER;
  986. ei->hint_femp.eidx = p_dir->size * dentries_per_clu;
  987. ei->hint_femp.count = 0;
  988. }
  989. /* initialized hint_stat */
  990. hint_stat->clu = p_dir->dir;
  991. hint_stat->eidx = 0;
  992. return -ENOENT;
  993. found:
  994. /* next dentry we'll find is out of this cluster */
  995. if (!((dentry + 1) & (dentries_per_clu - 1))) {
  996. int ret = 0;
  997. if (clu.flags == ALLOC_NO_FAT_CHAIN) {
  998. if (--clu.size > 0)
  999. clu.dir++;
  1000. else
  1001. clu.dir = EXFAT_EOF_CLUSTER;
  1002. } else {
  1003. ret = exfat_get_next_cluster(sb, &clu.dir);
  1004. }
  1005. if (ret || clu.dir == EXFAT_EOF_CLUSTER) {
  1006. /* just initialized hint_stat */
  1007. hint_stat->clu = p_dir->dir;
  1008. hint_stat->eidx = 0;
  1009. return (dentry - num_ext);
  1010. }
  1011. }
  1012. hint_stat->clu = clu.dir;
  1013. hint_stat->eidx = dentry + 1;
  1014. return dentry - num_ext;
  1015. }
  1016. int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
  1017. {
  1018. int i, count = 0;
  1019. int dentries_per_clu;
  1020. unsigned int entry_type;
  1021. unsigned int clu_count = 0;
  1022. struct exfat_chain clu;
  1023. struct exfat_dentry *ep;
  1024. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  1025. struct buffer_head *bh;
  1026. dentries_per_clu = sbi->dentries_per_clu;
  1027. exfat_chain_dup(&clu, p_dir);
  1028. while (clu.dir != EXFAT_EOF_CLUSTER) {
  1029. for (i = 0; i < dentries_per_clu; i++) {
  1030. ep = exfat_get_dentry(sb, &clu, i, &bh);
  1031. if (!ep)
  1032. return -EIO;
  1033. entry_type = exfat_get_entry_type(ep);
  1034. brelse(bh);
  1035. if (entry_type == TYPE_UNUSED)
  1036. return count;
  1037. if (entry_type != TYPE_DIR)
  1038. continue;
  1039. count++;
  1040. }
  1041. if (clu.flags == ALLOC_NO_FAT_CHAIN) {
  1042. if (--clu.size > 0)
  1043. clu.dir++;
  1044. else
  1045. clu.dir = EXFAT_EOF_CLUSTER;
  1046. } else {
  1047. if (exfat_get_next_cluster(sb, &(clu.dir)))
  1048. return -EIO;
  1049. if (unlikely(++clu_count > sbi->used_clusters)) {
  1050. exfat_fs_error(sb, "FAT or bitmap is corrupted");
  1051. return -EIO;
  1052. }
  1053. }
  1054. }
  1055. return count;
  1056. }
  1057. static int exfat_get_volume_label_dentry(struct super_block *sb,
  1058. struct exfat_entry_set_cache *es)
  1059. {
  1060. int i;
  1061. int dentry = 0;
  1062. unsigned int type;
  1063. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  1064. struct exfat_hint_femp hint_femp;
  1065. struct exfat_inode_info *ei = EXFAT_I(sb->s_root->d_inode);
  1066. struct exfat_chain clu;
  1067. struct exfat_dentry *ep;
  1068. struct buffer_head *bh;
  1069. hint_femp.eidx = EXFAT_HINT_NONE;
  1070. exfat_chain_set(&clu, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
  1071. while (clu.dir != EXFAT_EOF_CLUSTER) {
  1072. for (i = 0; i < sbi->dentries_per_clu; i++, dentry++) {
  1073. ep = exfat_get_dentry(sb, &clu, i, &bh);
  1074. if (!ep)
  1075. return -EIO;
  1076. type = exfat_get_entry_type(ep);
  1077. if (hint_femp.eidx == EXFAT_HINT_NONE) {
  1078. if (type == TYPE_DELETED || type == TYPE_UNUSED) {
  1079. hint_femp.cur = clu;
  1080. hint_femp.eidx = dentry;
  1081. hint_femp.count = 1;
  1082. }
  1083. }
  1084. if (type == TYPE_UNUSED) {
  1085. brelse(bh);
  1086. goto not_found;
  1087. }
  1088. if (type != TYPE_VOLUME) {
  1089. brelse(bh);
  1090. continue;
  1091. }
  1092. memset(es, 0, sizeof(*es));
  1093. es->sb = sb;
  1094. es->bh = es->__bh;
  1095. es->bh[0] = bh;
  1096. es->num_bh = 1;
  1097. es->start_off = EXFAT_DEN_TO_B(i) % sb->s_blocksize;
  1098. return 0;
  1099. }
  1100. if (exfat_get_next_cluster(sb, &(clu.dir)))
  1101. return -EIO;
  1102. }
  1103. not_found:
  1104. if (hint_femp.eidx == EXFAT_HINT_NONE) {
  1105. hint_femp.cur.dir = EXFAT_EOF_CLUSTER;
  1106. hint_femp.eidx = dentry;
  1107. hint_femp.count = 0;
  1108. }
  1109. ei->hint_femp = hint_femp;
  1110. return -ENOENT;
  1111. }
  1112. int exfat_read_volume_label(struct super_block *sb, struct exfat_uni_name *label_out)
  1113. {
  1114. int ret, i;
  1115. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  1116. struct exfat_entry_set_cache es;
  1117. struct exfat_dentry *ep;
  1118. mutex_lock(&sbi->s_lock);
  1119. memset(label_out, 0, sizeof(*label_out));
  1120. ret = exfat_get_volume_label_dentry(sb, &es);
  1121. if (ret < 0) {
  1122. /*
  1123. * ENOENT signifies that a volume label dentry doesn't exist
  1124. * We will treat this as an empty volume label and not fail.
  1125. */
  1126. if (ret == -ENOENT)
  1127. ret = 0;
  1128. goto unlock;
  1129. }
  1130. ep = exfat_get_dentry_cached(&es, 0);
  1131. label_out->name_len = ep->dentry.volume_label.char_count;
  1132. if (label_out->name_len > EXFAT_VOLUME_LABEL_LEN) {
  1133. ret = -EIO;
  1134. exfat_put_dentry_set(&es, false);
  1135. goto unlock;
  1136. }
  1137. for (i = 0; i < label_out->name_len; i++)
  1138. label_out->name[i] = le16_to_cpu(ep->dentry.volume_label.volume_label[i]);
  1139. exfat_put_dentry_set(&es, false);
  1140. unlock:
  1141. mutex_unlock(&sbi->s_lock);
  1142. return ret;
  1143. }
  1144. int exfat_write_volume_label(struct super_block *sb,
  1145. struct exfat_uni_name *label)
  1146. {
  1147. int ret, i;
  1148. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  1149. struct inode *root_inode = sb->s_root->d_inode;
  1150. struct exfat_entry_set_cache es;
  1151. struct exfat_chain clu;
  1152. struct exfat_dentry *ep;
  1153. if (label->name_len > EXFAT_VOLUME_LABEL_LEN)
  1154. return -EINVAL;
  1155. mutex_lock(&sbi->s_lock);
  1156. ret = exfat_get_volume_label_dentry(sb, &es);
  1157. if (ret == -ENOENT) {
  1158. if (label->name_len == 0) {
  1159. /* No volume label dentry, no need to clear */
  1160. ret = 0;
  1161. goto unlock;
  1162. }
  1163. ret = exfat_find_empty_entry(root_inode, &clu, 1, &es);
  1164. }
  1165. if (ret < 0)
  1166. goto unlock;
  1167. ep = exfat_get_dentry_cached(&es, 0);
  1168. if (label->name_len == 0 && ep->dentry.volume_label.char_count == 0) {
  1169. /* volume label had been cleared */
  1170. exfat_put_dentry_set(&es, 0);
  1171. goto unlock;
  1172. }
  1173. memset(ep, 0, sizeof(*ep));
  1174. ep->type = EXFAT_VOLUME;
  1175. for (i = 0; i < label->name_len; i++)
  1176. ep->dentry.volume_label.volume_label[i] =
  1177. cpu_to_le16(label->name[i]);
  1178. ep->dentry.volume_label.char_count = label->name_len;
  1179. es.modified = true;
  1180. ret = exfat_put_dentry_set(&es, IS_DIRSYNC(root_inode));
  1181. unlock:
  1182. mutex_unlock(&sbi->s_lock);
  1183. return ret;
  1184. }