dir.c 28 KB

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