dir.c 30 KB

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