dir.c 28 KB

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