namei.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
  4. */
  5. #include <linux/iversion.h>
  6. #include <linux/namei.h>
  7. #include <linux/slab.h>
  8. #include <linux/buffer_head.h>
  9. #include <linux/nls.h>
  10. #include "exfat_raw.h"
  11. #include "exfat_fs.h"
  12. static inline unsigned long exfat_d_version(struct dentry *dentry)
  13. {
  14. return (unsigned long) dentry->d_fsdata;
  15. }
  16. static inline void exfat_d_version_set(struct dentry *dentry,
  17. unsigned long version)
  18. {
  19. dentry->d_fsdata = (void *) version;
  20. }
  21. /*
  22. * If new entry was created in the parent, it could create the 8.3 alias (the
  23. * shortname of logname). So, the parent may have the negative-dentry which
  24. * matches the created 8.3 alias.
  25. *
  26. * If it happened, the negative dentry isn't actually negative anymore. So,
  27. * drop it.
  28. */
  29. static int exfat_d_revalidate(struct dentry *dentry, unsigned int flags)
  30. {
  31. int ret;
  32. if (flags & LOOKUP_RCU)
  33. return -ECHILD;
  34. /*
  35. * This is not negative dentry. Always valid.
  36. *
  37. * Note, rename() to existing directory entry will have ->d_inode, and
  38. * will use existing name which isn't specified name by user.
  39. *
  40. * We may be able to drop this positive dentry here. But dropping
  41. * positive dentry isn't good idea. So it's unsupported like
  42. * rename("filename", "FILENAME") for now.
  43. */
  44. if (d_really_is_positive(dentry))
  45. return 1;
  46. /*
  47. * Drop the negative dentry, in order to make sure to use the case
  48. * sensitive name which is specified by user if this is for creation.
  49. */
  50. if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
  51. return 0;
  52. spin_lock(&dentry->d_lock);
  53. ret = inode_eq_iversion(d_inode(dentry->d_parent),
  54. exfat_d_version(dentry));
  55. spin_unlock(&dentry->d_lock);
  56. return ret;
  57. }
  58. /* returns the length of a struct qstr, ignoring trailing dots */
  59. static unsigned int exfat_striptail_len(unsigned int len, const char *name)
  60. {
  61. while (len && name[len - 1] == '.')
  62. len--;
  63. return len;
  64. }
  65. /*
  66. * Compute the hash for the exfat name corresponding to the dentry. If the name
  67. * is invalid, we leave the hash code unchanged so that the existing dentry can
  68. * be used. The exfat fs routines will return ENOENT or EINVAL as appropriate.
  69. */
  70. static int exfat_d_hash(const struct dentry *dentry, struct qstr *qstr)
  71. {
  72. struct super_block *sb = dentry->d_sb;
  73. struct nls_table *t = EXFAT_SB(sb)->nls_io;
  74. const unsigned char *name = qstr->name;
  75. unsigned int len = exfat_striptail_len(qstr->len, qstr->name);
  76. unsigned long hash = init_name_hash(dentry);
  77. int i, charlen;
  78. wchar_t c;
  79. for (i = 0; i < len; i += charlen) {
  80. charlen = t->char2uni(&name[i], len - i, &c);
  81. if (charlen < 0)
  82. return charlen;
  83. hash = partial_name_hash(exfat_toupper(sb, c), hash);
  84. }
  85. qstr->hash = end_name_hash(hash);
  86. return 0;
  87. }
  88. static int exfat_d_cmp(const struct dentry *dentry, unsigned int len,
  89. const char *str, const struct qstr *name)
  90. {
  91. struct super_block *sb = dentry->d_sb;
  92. struct nls_table *t = EXFAT_SB(sb)->nls_io;
  93. unsigned int alen = exfat_striptail_len(name->len, name->name);
  94. unsigned int blen = exfat_striptail_len(len, str);
  95. wchar_t c1, c2;
  96. int charlen, i;
  97. if (alen != blen)
  98. return 1;
  99. for (i = 0; i < len; i += charlen) {
  100. charlen = t->char2uni(&name->name[i], alen - i, &c1);
  101. if (charlen < 0)
  102. return 1;
  103. if (charlen != t->char2uni(&str[i], blen - i, &c2))
  104. return 1;
  105. if (exfat_toupper(sb, c1) != exfat_toupper(sb, c2))
  106. return 1;
  107. }
  108. return 0;
  109. }
  110. const struct dentry_operations exfat_dentry_ops = {
  111. .d_revalidate = exfat_d_revalidate,
  112. .d_hash = exfat_d_hash,
  113. .d_compare = exfat_d_cmp,
  114. };
  115. static int exfat_utf8_d_hash(const struct dentry *dentry, struct qstr *qstr)
  116. {
  117. struct super_block *sb = dentry->d_sb;
  118. const unsigned char *name = qstr->name;
  119. unsigned int len = exfat_striptail_len(qstr->len, qstr->name);
  120. unsigned long hash = init_name_hash(dentry);
  121. int i, charlen;
  122. unicode_t u;
  123. for (i = 0; i < len; i += charlen) {
  124. charlen = utf8_to_utf32(&name[i], len - i, &u);
  125. if (charlen < 0)
  126. return charlen;
  127. /*
  128. * exfat_toupper() works only for code points up to the U+FFFF.
  129. */
  130. hash = partial_name_hash(u <= 0xFFFF ? exfat_toupper(sb, u) : u,
  131. hash);
  132. }
  133. qstr->hash = end_name_hash(hash);
  134. return 0;
  135. }
  136. static int exfat_utf8_d_cmp(const struct dentry *dentry, unsigned int len,
  137. const char *str, const struct qstr *name)
  138. {
  139. struct super_block *sb = dentry->d_sb;
  140. unsigned int alen = exfat_striptail_len(name->len, name->name);
  141. unsigned int blen = exfat_striptail_len(len, str);
  142. unicode_t u_a, u_b;
  143. int charlen, i;
  144. if (alen != blen)
  145. return 1;
  146. for (i = 0; i < alen; i += charlen) {
  147. charlen = utf8_to_utf32(&name->name[i], alen - i, &u_a);
  148. if (charlen < 0)
  149. return 1;
  150. if (charlen != utf8_to_utf32(&str[i], blen - i, &u_b))
  151. return 1;
  152. if (u_a <= 0xFFFF && u_b <= 0xFFFF) {
  153. if (exfat_toupper(sb, u_a) != exfat_toupper(sb, u_b))
  154. return 1;
  155. } else {
  156. if (u_a != u_b)
  157. return 1;
  158. }
  159. }
  160. return 0;
  161. }
  162. const struct dentry_operations exfat_utf8_dentry_ops = {
  163. .d_revalidate = exfat_d_revalidate,
  164. .d_hash = exfat_utf8_d_hash,
  165. .d_compare = exfat_utf8_d_cmp,
  166. };
  167. /* used only in search empty_slot() */
  168. #define CNT_UNUSED_NOHIT (-1)
  169. #define CNT_UNUSED_HIT (-2)
  170. /* search EMPTY CONTINUOUS "num_entries" entries */
  171. static int exfat_search_empty_slot(struct super_block *sb,
  172. struct exfat_hint_femp *hint_femp, struct exfat_chain *p_dir,
  173. int num_entries)
  174. {
  175. int i, dentry, num_empty = 0;
  176. int dentries_per_clu;
  177. unsigned int type;
  178. struct exfat_chain clu;
  179. struct exfat_dentry *ep;
  180. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  181. struct buffer_head *bh;
  182. dentries_per_clu = sbi->dentries_per_clu;
  183. if (hint_femp->eidx != EXFAT_HINT_NONE) {
  184. dentry = hint_femp->eidx;
  185. if (num_entries <= hint_femp->count) {
  186. hint_femp->eidx = EXFAT_HINT_NONE;
  187. return dentry;
  188. }
  189. exfat_chain_dup(&clu, &hint_femp->cur);
  190. } else {
  191. exfat_chain_dup(&clu, p_dir);
  192. dentry = 0;
  193. }
  194. while (clu.dir != EXFAT_EOF_CLUSTER) {
  195. i = dentry & (dentries_per_clu - 1);
  196. for (; i < dentries_per_clu; i++, dentry++) {
  197. ep = exfat_get_dentry(sb, &clu, i, &bh, NULL);
  198. if (!ep)
  199. return -EIO;
  200. type = exfat_get_entry_type(ep);
  201. brelse(bh);
  202. if (type == TYPE_UNUSED || type == TYPE_DELETED) {
  203. num_empty++;
  204. if (hint_femp->eidx == EXFAT_HINT_NONE) {
  205. hint_femp->eidx = dentry;
  206. hint_femp->count = CNT_UNUSED_NOHIT;
  207. exfat_chain_set(&hint_femp->cur,
  208. clu.dir, clu.size, clu.flags);
  209. }
  210. if (type == TYPE_UNUSED &&
  211. hint_femp->count != CNT_UNUSED_HIT)
  212. hint_femp->count = CNT_UNUSED_HIT;
  213. } else {
  214. if (hint_femp->eidx != EXFAT_HINT_NONE &&
  215. hint_femp->count == CNT_UNUSED_HIT) {
  216. /* unused empty group means
  217. * an empty group which includes
  218. * unused dentry
  219. */
  220. exfat_fs_error(sb,
  221. "found bogus dentry(%d) beyond unused empty group(%d) (start_clu : %u, cur_clu : %u)",
  222. dentry, hint_femp->eidx,
  223. p_dir->dir, clu.dir);
  224. return -EIO;
  225. }
  226. num_empty = 0;
  227. hint_femp->eidx = EXFAT_HINT_NONE;
  228. }
  229. if (num_empty >= num_entries) {
  230. /* found and invalidate hint_femp */
  231. hint_femp->eidx = EXFAT_HINT_NONE;
  232. return (dentry - (num_entries - 1));
  233. }
  234. }
  235. if (clu.flags == ALLOC_NO_FAT_CHAIN) {
  236. if (--clu.size > 0)
  237. clu.dir++;
  238. else
  239. clu.dir = EXFAT_EOF_CLUSTER;
  240. } else {
  241. if (exfat_get_next_cluster(sb, &clu.dir))
  242. return -EIO;
  243. }
  244. }
  245. return -ENOSPC;
  246. }
  247. static int exfat_check_max_dentries(struct inode *inode)
  248. {
  249. if (EXFAT_B_TO_DEN(i_size_read(inode)) >= MAX_EXFAT_DENTRIES) {
  250. /*
  251. * exFAT spec allows a dir to grow up to 8388608(256MB)
  252. * dentries
  253. */
  254. return -ENOSPC;
  255. }
  256. return 0;
  257. }
  258. /* find empty directory entry.
  259. * if there isn't any empty slot, expand cluster chain.
  260. */
  261. static int exfat_find_empty_entry(struct inode *inode,
  262. struct exfat_chain *p_dir, int num_entries)
  263. {
  264. int dentry;
  265. unsigned int ret, last_clu;
  266. sector_t sector;
  267. loff_t size = 0;
  268. struct exfat_chain clu;
  269. struct exfat_dentry *ep = NULL;
  270. struct super_block *sb = inode->i_sb;
  271. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  272. struct exfat_inode_info *ei = EXFAT_I(inode);
  273. struct exfat_hint_femp hint_femp;
  274. hint_femp.eidx = EXFAT_HINT_NONE;
  275. if (ei->hint_femp.eidx != EXFAT_HINT_NONE) {
  276. hint_femp = ei->hint_femp;
  277. ei->hint_femp.eidx = EXFAT_HINT_NONE;
  278. }
  279. while ((dentry = exfat_search_empty_slot(sb, &hint_femp, p_dir,
  280. num_entries)) < 0) {
  281. if (dentry == -EIO)
  282. break;
  283. if (exfat_check_max_dentries(inode))
  284. return -ENOSPC;
  285. /* we trust p_dir->size regardless of FAT type */
  286. if (exfat_find_last_cluster(sb, p_dir, &last_clu))
  287. return -EIO;
  288. /*
  289. * Allocate new cluster to this directory
  290. */
  291. exfat_chain_set(&clu, last_clu + 1, 0, p_dir->flags);
  292. /* allocate a cluster */
  293. ret = exfat_alloc_cluster(inode, 1, &clu);
  294. if (ret)
  295. return ret;
  296. if (exfat_zeroed_cluster(inode, clu.dir))
  297. return -EIO;
  298. /* append to the FAT chain */
  299. if (clu.flags != p_dir->flags) {
  300. /* no-fat-chain bit is disabled,
  301. * so fat-chain should be synced with alloc-bitmap
  302. */
  303. exfat_chain_cont_cluster(sb, p_dir->dir, p_dir->size);
  304. p_dir->flags = ALLOC_FAT_CHAIN;
  305. hint_femp.cur.flags = ALLOC_FAT_CHAIN;
  306. }
  307. if (clu.flags == ALLOC_FAT_CHAIN)
  308. if (exfat_ent_set(sb, last_clu, clu.dir))
  309. return -EIO;
  310. if (hint_femp.eidx == EXFAT_HINT_NONE) {
  311. /* the special case that new dentry
  312. * should be allocated from the start of new cluster
  313. */
  314. hint_femp.eidx = EXFAT_B_TO_DEN_IDX(p_dir->size, sbi);
  315. hint_femp.count = sbi->dentries_per_clu;
  316. exfat_chain_set(&hint_femp.cur, clu.dir, 0, clu.flags);
  317. }
  318. hint_femp.cur.size++;
  319. p_dir->size++;
  320. size = EXFAT_CLU_TO_B(p_dir->size, sbi);
  321. /* update the directory entry */
  322. if (p_dir->dir != sbi->root_dir) {
  323. struct buffer_head *bh;
  324. ep = exfat_get_dentry(sb,
  325. &(ei->dir), ei->entry + 1, &bh, &sector);
  326. if (!ep)
  327. return -EIO;
  328. ep->dentry.stream.valid_size = cpu_to_le64(size);
  329. ep->dentry.stream.size = ep->dentry.stream.valid_size;
  330. ep->dentry.stream.flags = p_dir->flags;
  331. exfat_update_bh(bh, IS_DIRSYNC(inode));
  332. brelse(bh);
  333. if (exfat_update_dir_chksum(inode, &(ei->dir),
  334. ei->entry))
  335. return -EIO;
  336. }
  337. /* directory inode should be updated in here */
  338. i_size_write(inode, size);
  339. ei->i_size_ondisk += sbi->cluster_size;
  340. ei->i_size_aligned += sbi->cluster_size;
  341. ei->flags = p_dir->flags;
  342. inode->i_blocks += 1 << sbi->sect_per_clus_bits;
  343. }
  344. return dentry;
  345. }
  346. /*
  347. * Name Resolution Functions :
  348. * Zero if it was successful; otherwise nonzero.
  349. */
  350. static int __exfat_resolve_path(struct inode *inode, const unsigned char *path,
  351. struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
  352. int lookup)
  353. {
  354. int namelen;
  355. int lossy = NLS_NAME_NO_LOSSY;
  356. struct super_block *sb = inode->i_sb;
  357. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  358. struct exfat_inode_info *ei = EXFAT_I(inode);
  359. /* strip all trailing periods */
  360. namelen = exfat_striptail_len(strlen(path), path);
  361. if (!namelen)
  362. return -ENOENT;
  363. if (strlen(path) > (MAX_NAME_LENGTH * MAX_CHARSET_SIZE))
  364. return -ENAMETOOLONG;
  365. /*
  366. * strip all leading spaces :
  367. * "MS windows 7" supports leading spaces.
  368. * So we should skip this preprocessing for compatibility.
  369. */
  370. /* file name conversion :
  371. * If lookup case, we allow bad-name for compatibility.
  372. */
  373. namelen = exfat_nls_to_utf16(sb, path, namelen, p_uniname,
  374. &lossy);
  375. if (namelen < 0)
  376. return namelen; /* return error value */
  377. if ((lossy && !lookup) || !namelen)
  378. return -EINVAL;
  379. exfat_chain_set(p_dir, ei->start_clu,
  380. EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags);
  381. return 0;
  382. }
  383. static inline int exfat_resolve_path(struct inode *inode,
  384. const unsigned char *path, struct exfat_chain *dir,
  385. struct exfat_uni_name *uni)
  386. {
  387. return __exfat_resolve_path(inode, path, dir, uni, 0);
  388. }
  389. static inline int exfat_resolve_path_for_lookup(struct inode *inode,
  390. const unsigned char *path, struct exfat_chain *dir,
  391. struct exfat_uni_name *uni)
  392. {
  393. return __exfat_resolve_path(inode, path, dir, uni, 1);
  394. }
  395. static inline loff_t exfat_make_i_pos(struct exfat_dir_entry *info)
  396. {
  397. return ((loff_t) info->dir.dir << 32) | (info->entry & 0xffffffff);
  398. }
  399. static int exfat_add_entry(struct inode *inode, const char *path,
  400. struct exfat_chain *p_dir, unsigned int type,
  401. struct exfat_dir_entry *info)
  402. {
  403. int ret, dentry, num_entries;
  404. struct super_block *sb = inode->i_sb;
  405. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  406. struct exfat_uni_name uniname;
  407. struct exfat_chain clu;
  408. int clu_size = 0;
  409. unsigned int start_clu = EXFAT_FREE_CLUSTER;
  410. ret = exfat_resolve_path(inode, path, p_dir, &uniname);
  411. if (ret)
  412. goto out;
  413. num_entries = exfat_calc_num_entries(&uniname);
  414. if (num_entries < 0) {
  415. ret = num_entries;
  416. goto out;
  417. }
  418. /* exfat_find_empty_entry must be called before alloc_cluster() */
  419. dentry = exfat_find_empty_entry(inode, p_dir, num_entries);
  420. if (dentry < 0) {
  421. ret = dentry; /* -EIO or -ENOSPC */
  422. goto out;
  423. }
  424. if (type == TYPE_DIR) {
  425. ret = exfat_alloc_new_dir(inode, &clu);
  426. if (ret)
  427. goto out;
  428. start_clu = clu.dir;
  429. clu_size = sbi->cluster_size;
  430. }
  431. /* update the directory entry */
  432. /* fill the dos name directory entry information of the created file.
  433. * the first cluster is not determined yet. (0)
  434. */
  435. ret = exfat_init_dir_entry(inode, p_dir, dentry, type,
  436. start_clu, clu_size);
  437. if (ret)
  438. goto out;
  439. ret = exfat_init_ext_entry(inode, p_dir, dentry, num_entries, &uniname);
  440. if (ret)
  441. goto out;
  442. info->dir = *p_dir;
  443. info->entry = dentry;
  444. info->flags = ALLOC_NO_FAT_CHAIN;
  445. info->type = type;
  446. if (type == TYPE_FILE) {
  447. info->attr = ATTR_ARCHIVE;
  448. info->start_clu = EXFAT_EOF_CLUSTER;
  449. info->size = 0;
  450. info->num_subdirs = 0;
  451. } else {
  452. info->attr = ATTR_SUBDIR;
  453. info->start_clu = start_clu;
  454. info->size = clu_size;
  455. info->num_subdirs = EXFAT_MIN_SUBDIR;
  456. }
  457. memset(&info->crtime, 0, sizeof(info->crtime));
  458. memset(&info->mtime, 0, sizeof(info->mtime));
  459. memset(&info->atime, 0, sizeof(info->atime));
  460. out:
  461. return ret;
  462. }
  463. static int exfat_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  464. bool excl)
  465. {
  466. struct super_block *sb = dir->i_sb;
  467. struct inode *inode;
  468. struct exfat_chain cdir;
  469. struct exfat_dir_entry info;
  470. loff_t i_pos;
  471. int err;
  472. mutex_lock(&EXFAT_SB(sb)->s_lock);
  473. exfat_set_volume_dirty(sb);
  474. err = exfat_add_entry(dir, dentry->d_name.name, &cdir, TYPE_FILE,
  475. &info);
  476. exfat_clear_volume_dirty(sb);
  477. if (err)
  478. goto unlock;
  479. inode_inc_iversion(dir);
  480. dir->i_ctime = dir->i_mtime = current_time(dir);
  481. if (IS_DIRSYNC(dir))
  482. exfat_sync_inode(dir);
  483. else
  484. mark_inode_dirty(dir);
  485. i_pos = exfat_make_i_pos(&info);
  486. inode = exfat_build_inode(sb, &info, i_pos);
  487. err = PTR_ERR_OR_ZERO(inode);
  488. if (err)
  489. goto unlock;
  490. inode_inc_iversion(inode);
  491. inode->i_mtime = inode->i_atime = inode->i_ctime =
  492. EXFAT_I(inode)->i_crtime = current_time(inode);
  493. exfat_truncate_atime(&inode->i_atime);
  494. /* timestamp is already written, so mark_inode_dirty() is unneeded. */
  495. d_instantiate(dentry, inode);
  496. unlock:
  497. mutex_unlock(&EXFAT_SB(sb)->s_lock);
  498. return err;
  499. }
  500. /* lookup a file */
  501. static int exfat_find(struct inode *dir, struct qstr *qname,
  502. struct exfat_dir_entry *info)
  503. {
  504. int ret, dentry, num_entries, count;
  505. struct exfat_chain cdir;
  506. struct exfat_uni_name uni_name;
  507. struct super_block *sb = dir->i_sb;
  508. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  509. struct exfat_inode_info *ei = EXFAT_I(dir);
  510. struct exfat_dentry *ep, *ep2;
  511. struct exfat_entry_set_cache *es;
  512. if (qname->len == 0)
  513. return -ENOENT;
  514. /* check the validity of directory name in the given pathname */
  515. ret = exfat_resolve_path_for_lookup(dir, qname->name, &cdir, &uni_name);
  516. if (ret)
  517. return ret;
  518. num_entries = exfat_calc_num_entries(&uni_name);
  519. if (num_entries < 0)
  520. return num_entries;
  521. /* check the validation of hint_stat and initialize it if required */
  522. if (ei->version != (inode_peek_iversion_raw(dir) & 0xffffffff)) {
  523. ei->hint_stat.clu = cdir.dir;
  524. ei->hint_stat.eidx = 0;
  525. ei->version = (inode_peek_iversion_raw(dir) & 0xffffffff);
  526. ei->hint_femp.eidx = EXFAT_HINT_NONE;
  527. }
  528. /* search the file name for directories */
  529. dentry = exfat_find_dir_entry(sb, ei, &cdir, &uni_name,
  530. num_entries, TYPE_ALL);
  531. if (dentry < 0)
  532. return dentry; /* -error value */
  533. info->dir = cdir;
  534. info->entry = dentry;
  535. info->num_subdirs = 0;
  536. es = exfat_get_dentry_set(sb, &cdir, dentry, ES_2_ENTRIES);
  537. if (!es)
  538. return -EIO;
  539. ep = exfat_get_dentry_cached(es, 0);
  540. ep2 = exfat_get_dentry_cached(es, 1);
  541. info->type = exfat_get_entry_type(ep);
  542. info->attr = le16_to_cpu(ep->dentry.file.attr);
  543. info->size = le64_to_cpu(ep2->dentry.stream.valid_size);
  544. if ((info->type == TYPE_FILE) && (info->size == 0)) {
  545. info->flags = ALLOC_NO_FAT_CHAIN;
  546. info->start_clu = EXFAT_EOF_CLUSTER;
  547. } else {
  548. info->flags = ep2->dentry.stream.flags;
  549. info->start_clu =
  550. le32_to_cpu(ep2->dentry.stream.start_clu);
  551. }
  552. exfat_get_entry_time(sbi, &info->crtime,
  553. ep->dentry.file.create_tz,
  554. ep->dentry.file.create_time,
  555. ep->dentry.file.create_date,
  556. ep->dentry.file.create_time_cs);
  557. exfat_get_entry_time(sbi, &info->mtime,
  558. ep->dentry.file.modify_tz,
  559. ep->dentry.file.modify_time,
  560. ep->dentry.file.modify_date,
  561. ep->dentry.file.modify_time_cs);
  562. exfat_get_entry_time(sbi, &info->atime,
  563. ep->dentry.file.access_tz,
  564. ep->dentry.file.access_time,
  565. ep->dentry.file.access_date,
  566. 0);
  567. exfat_free_dentry_set(es, false);
  568. if (ei->start_clu == EXFAT_FREE_CLUSTER) {
  569. exfat_fs_error(sb,
  570. "non-zero size file starts with zero cluster (size : %llu, p_dir : %u, entry : 0x%08x)",
  571. i_size_read(dir), ei->dir.dir, ei->entry);
  572. return -EIO;
  573. }
  574. if (info->type == TYPE_DIR) {
  575. exfat_chain_set(&cdir, info->start_clu,
  576. EXFAT_B_TO_CLU(info->size, sbi), info->flags);
  577. count = exfat_count_dir_entries(sb, &cdir);
  578. if (count < 0)
  579. return -EIO;
  580. info->num_subdirs = count + EXFAT_MIN_SUBDIR;
  581. }
  582. return 0;
  583. }
  584. static int exfat_d_anon_disconn(struct dentry *dentry)
  585. {
  586. return IS_ROOT(dentry) && (dentry->d_flags & DCACHE_DISCONNECTED);
  587. }
  588. static struct dentry *exfat_lookup(struct inode *dir, struct dentry *dentry,
  589. unsigned int flags)
  590. {
  591. struct super_block *sb = dir->i_sb;
  592. struct inode *inode;
  593. struct dentry *alias;
  594. struct exfat_dir_entry info;
  595. int err;
  596. loff_t i_pos;
  597. mode_t i_mode;
  598. mutex_lock(&EXFAT_SB(sb)->s_lock);
  599. err = exfat_find(dir, &dentry->d_name, &info);
  600. if (err) {
  601. if (err == -ENOENT) {
  602. inode = NULL;
  603. goto out;
  604. }
  605. goto unlock;
  606. }
  607. i_pos = exfat_make_i_pos(&info);
  608. inode = exfat_build_inode(sb, &info, i_pos);
  609. err = PTR_ERR_OR_ZERO(inode);
  610. if (err)
  611. goto unlock;
  612. i_mode = inode->i_mode;
  613. alias = d_find_alias(inode);
  614. /*
  615. * Checking "alias->d_parent == dentry->d_parent" to make sure
  616. * FS is not corrupted (especially double linked dir).
  617. */
  618. if (alias && alias->d_parent == dentry->d_parent &&
  619. !exfat_d_anon_disconn(alias)) {
  620. /*
  621. * Unhashed alias is able to exist because of revalidate()
  622. * called by lookup_fast. You can easily make this status
  623. * by calling create and lookup concurrently
  624. * In such case, we reuse an alias instead of new dentry
  625. */
  626. if (d_unhashed(alias)) {
  627. WARN_ON(alias->d_name.hash_len !=
  628. dentry->d_name.hash_len);
  629. exfat_info(sb, "rehashed a dentry(%p) in read lookup",
  630. alias);
  631. d_drop(dentry);
  632. d_rehash(alias);
  633. } else if (!S_ISDIR(i_mode)) {
  634. /*
  635. * This inode has non anonymous-DCACHE_DISCONNECTED
  636. * dentry. This means, the user did ->lookup() by an
  637. * another name (longname vs 8.3 alias of it) in past.
  638. *
  639. * Switch to new one for reason of locality if possible.
  640. */
  641. d_move(alias, dentry);
  642. }
  643. iput(inode);
  644. mutex_unlock(&EXFAT_SB(sb)->s_lock);
  645. return alias;
  646. }
  647. dput(alias);
  648. out:
  649. mutex_unlock(&EXFAT_SB(sb)->s_lock);
  650. if (!inode)
  651. exfat_d_version_set(dentry, inode_query_iversion(dir));
  652. return d_splice_alias(inode, dentry);
  653. unlock:
  654. mutex_unlock(&EXFAT_SB(sb)->s_lock);
  655. return ERR_PTR(err);
  656. }
  657. /* remove an entry, BUT don't truncate */
  658. static int exfat_unlink(struct inode *dir, struct dentry *dentry)
  659. {
  660. struct exfat_chain cdir;
  661. struct exfat_dentry *ep;
  662. struct super_block *sb = dir->i_sb;
  663. struct inode *inode = dentry->d_inode;
  664. struct exfat_inode_info *ei = EXFAT_I(inode);
  665. struct buffer_head *bh;
  666. sector_t sector;
  667. int num_entries, entry, err = 0;
  668. mutex_lock(&EXFAT_SB(sb)->s_lock);
  669. exfat_chain_dup(&cdir, &ei->dir);
  670. entry = ei->entry;
  671. if (ei->dir.dir == DIR_DELETED) {
  672. exfat_err(sb, "abnormal access to deleted dentry");
  673. err = -ENOENT;
  674. goto unlock;
  675. }
  676. ep = exfat_get_dentry(sb, &cdir, entry, &bh, &sector);
  677. if (!ep) {
  678. err = -EIO;
  679. goto unlock;
  680. }
  681. num_entries = exfat_count_ext_entries(sb, &cdir, entry, ep);
  682. if (num_entries < 0) {
  683. err = -EIO;
  684. brelse(bh);
  685. goto unlock;
  686. }
  687. num_entries++;
  688. brelse(bh);
  689. exfat_set_volume_dirty(sb);
  690. /* update the directory entry */
  691. if (exfat_remove_entries(dir, &cdir, entry, 0, num_entries)) {
  692. err = -EIO;
  693. goto unlock;
  694. }
  695. /* This doesn't modify ei */
  696. ei->dir.dir = DIR_DELETED;
  697. exfat_clear_volume_dirty(sb);
  698. inode_inc_iversion(dir);
  699. dir->i_mtime = dir->i_atime = current_time(dir);
  700. exfat_truncate_atime(&dir->i_atime);
  701. if (IS_DIRSYNC(dir))
  702. exfat_sync_inode(dir);
  703. else
  704. mark_inode_dirty(dir);
  705. clear_nlink(inode);
  706. inode->i_mtime = inode->i_atime = current_time(inode);
  707. exfat_truncate_atime(&inode->i_atime);
  708. exfat_unhash_inode(inode);
  709. exfat_d_version_set(dentry, inode_query_iversion(dir));
  710. unlock:
  711. mutex_unlock(&EXFAT_SB(sb)->s_lock);
  712. return err;
  713. }
  714. static int exfat_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  715. {
  716. struct super_block *sb = dir->i_sb;
  717. struct inode *inode;
  718. struct exfat_dir_entry info;
  719. struct exfat_chain cdir;
  720. loff_t i_pos;
  721. int err;
  722. mutex_lock(&EXFAT_SB(sb)->s_lock);
  723. exfat_set_volume_dirty(sb);
  724. err = exfat_add_entry(dir, dentry->d_name.name, &cdir, TYPE_DIR,
  725. &info);
  726. exfat_clear_volume_dirty(sb);
  727. if (err)
  728. goto unlock;
  729. inode_inc_iversion(dir);
  730. dir->i_ctime = dir->i_mtime = current_time(dir);
  731. if (IS_DIRSYNC(dir))
  732. exfat_sync_inode(dir);
  733. else
  734. mark_inode_dirty(dir);
  735. inc_nlink(dir);
  736. i_pos = exfat_make_i_pos(&info);
  737. inode = exfat_build_inode(sb, &info, i_pos);
  738. err = PTR_ERR_OR_ZERO(inode);
  739. if (err)
  740. goto unlock;
  741. inode_inc_iversion(inode);
  742. inode->i_mtime = inode->i_atime = inode->i_ctime =
  743. EXFAT_I(inode)->i_crtime = current_time(inode);
  744. exfat_truncate_atime(&inode->i_atime);
  745. /* timestamp is already written, so mark_inode_dirty() is unneeded. */
  746. d_instantiate(dentry, inode);
  747. unlock:
  748. mutex_unlock(&EXFAT_SB(sb)->s_lock);
  749. return err;
  750. }
  751. static int exfat_check_dir_empty(struct super_block *sb,
  752. struct exfat_chain *p_dir)
  753. {
  754. int i, dentries_per_clu;
  755. unsigned int type;
  756. struct exfat_chain clu;
  757. struct exfat_dentry *ep;
  758. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  759. struct buffer_head *bh;
  760. dentries_per_clu = sbi->dentries_per_clu;
  761. exfat_chain_dup(&clu, p_dir);
  762. while (clu.dir != EXFAT_EOF_CLUSTER) {
  763. for (i = 0; i < dentries_per_clu; i++) {
  764. ep = exfat_get_dentry(sb, &clu, i, &bh, NULL);
  765. if (!ep)
  766. return -EIO;
  767. type = exfat_get_entry_type(ep);
  768. brelse(bh);
  769. if (type == TYPE_UNUSED)
  770. return 0;
  771. if (type != TYPE_FILE && type != TYPE_DIR)
  772. continue;
  773. return -ENOTEMPTY;
  774. }
  775. if (clu.flags == ALLOC_NO_FAT_CHAIN) {
  776. if (--clu.size > 0)
  777. clu.dir++;
  778. else
  779. clu.dir = EXFAT_EOF_CLUSTER;
  780. } else {
  781. if (exfat_get_next_cluster(sb, &(clu.dir)))
  782. return -EIO;
  783. }
  784. }
  785. return 0;
  786. }
  787. static int exfat_rmdir(struct inode *dir, struct dentry *dentry)
  788. {
  789. struct inode *inode = dentry->d_inode;
  790. struct exfat_dentry *ep;
  791. struct exfat_chain cdir, clu_to_free;
  792. struct super_block *sb = inode->i_sb;
  793. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  794. struct exfat_inode_info *ei = EXFAT_I(inode);
  795. struct buffer_head *bh;
  796. sector_t sector;
  797. int num_entries, entry, err;
  798. mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock);
  799. exfat_chain_dup(&cdir, &ei->dir);
  800. entry = ei->entry;
  801. if (ei->dir.dir == DIR_DELETED) {
  802. exfat_err(sb, "abnormal access to deleted dentry");
  803. err = -ENOENT;
  804. goto unlock;
  805. }
  806. exfat_chain_set(&clu_to_free, ei->start_clu,
  807. EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi), ei->flags);
  808. err = exfat_check_dir_empty(sb, &clu_to_free);
  809. if (err) {
  810. if (err == -EIO)
  811. exfat_err(sb, "failed to exfat_check_dir_empty : err(%d)",
  812. err);
  813. goto unlock;
  814. }
  815. ep = exfat_get_dentry(sb, &cdir, entry, &bh, &sector);
  816. if (!ep) {
  817. err = -EIO;
  818. goto unlock;
  819. }
  820. num_entries = exfat_count_ext_entries(sb, &cdir, entry, ep);
  821. if (num_entries < 0) {
  822. err = -EIO;
  823. brelse(bh);
  824. goto unlock;
  825. }
  826. num_entries++;
  827. brelse(bh);
  828. exfat_set_volume_dirty(sb);
  829. err = exfat_remove_entries(dir, &cdir, entry, 0, num_entries);
  830. if (err) {
  831. exfat_err(sb, "failed to exfat_remove_entries : err(%d)", err);
  832. goto unlock;
  833. }
  834. ei->dir.dir = DIR_DELETED;
  835. exfat_clear_volume_dirty(sb);
  836. inode_inc_iversion(dir);
  837. dir->i_mtime = dir->i_atime = current_time(dir);
  838. exfat_truncate_atime(&dir->i_atime);
  839. if (IS_DIRSYNC(dir))
  840. exfat_sync_inode(dir);
  841. else
  842. mark_inode_dirty(dir);
  843. drop_nlink(dir);
  844. clear_nlink(inode);
  845. inode->i_mtime = inode->i_atime = current_time(inode);
  846. exfat_truncate_atime(&inode->i_atime);
  847. exfat_unhash_inode(inode);
  848. exfat_d_version_set(dentry, inode_query_iversion(dir));
  849. unlock:
  850. mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
  851. return err;
  852. }
  853. static int exfat_rename_file(struct inode *inode, struct exfat_chain *p_dir,
  854. int oldentry, struct exfat_uni_name *p_uniname,
  855. struct exfat_inode_info *ei)
  856. {
  857. int ret, num_old_entries, num_new_entries;
  858. sector_t sector_old, sector_new;
  859. struct exfat_dentry *epold, *epnew;
  860. struct super_block *sb = inode->i_sb;
  861. struct buffer_head *new_bh, *old_bh;
  862. int sync = IS_DIRSYNC(inode);
  863. epold = exfat_get_dentry(sb, p_dir, oldentry, &old_bh, &sector_old);
  864. if (!epold)
  865. return -EIO;
  866. num_old_entries = exfat_count_ext_entries(sb, p_dir, oldentry, epold);
  867. if (num_old_entries < 0)
  868. return -EIO;
  869. num_old_entries++;
  870. num_new_entries = exfat_calc_num_entries(p_uniname);
  871. if (num_new_entries < 0)
  872. return num_new_entries;
  873. if (num_old_entries < num_new_entries) {
  874. int newentry;
  875. newentry =
  876. exfat_find_empty_entry(inode, p_dir, num_new_entries);
  877. if (newentry < 0)
  878. return newentry; /* -EIO or -ENOSPC */
  879. epnew = exfat_get_dentry(sb, p_dir, newentry, &new_bh,
  880. &sector_new);
  881. if (!epnew)
  882. return -EIO;
  883. *epnew = *epold;
  884. if (exfat_get_entry_type(epnew) == TYPE_FILE) {
  885. epnew->dentry.file.attr |= cpu_to_le16(ATTR_ARCHIVE);
  886. ei->attr |= ATTR_ARCHIVE;
  887. }
  888. exfat_update_bh(new_bh, sync);
  889. brelse(old_bh);
  890. brelse(new_bh);
  891. epold = exfat_get_dentry(sb, p_dir, oldentry + 1, &old_bh,
  892. &sector_old);
  893. if (!epold)
  894. return -EIO;
  895. epnew = exfat_get_dentry(sb, p_dir, newentry + 1, &new_bh,
  896. &sector_new);
  897. if (!epnew) {
  898. brelse(old_bh);
  899. return -EIO;
  900. }
  901. *epnew = *epold;
  902. exfat_update_bh(new_bh, sync);
  903. brelse(old_bh);
  904. brelse(new_bh);
  905. ret = exfat_init_ext_entry(inode, p_dir, newentry,
  906. num_new_entries, p_uniname);
  907. if (ret)
  908. return ret;
  909. exfat_remove_entries(inode, p_dir, oldentry, 0,
  910. num_old_entries);
  911. ei->entry = newentry;
  912. } else {
  913. if (exfat_get_entry_type(epold) == TYPE_FILE) {
  914. epold->dentry.file.attr |= cpu_to_le16(ATTR_ARCHIVE);
  915. ei->attr |= ATTR_ARCHIVE;
  916. }
  917. exfat_update_bh(old_bh, sync);
  918. brelse(old_bh);
  919. ret = exfat_init_ext_entry(inode, p_dir, oldentry,
  920. num_new_entries, p_uniname);
  921. if (ret)
  922. return ret;
  923. exfat_remove_entries(inode, p_dir, oldentry, num_new_entries,
  924. num_old_entries);
  925. }
  926. return 0;
  927. }
  928. static int exfat_move_file(struct inode *inode, struct exfat_chain *p_olddir,
  929. int oldentry, struct exfat_chain *p_newdir,
  930. struct exfat_uni_name *p_uniname, struct exfat_inode_info *ei)
  931. {
  932. int ret, newentry, num_new_entries, num_old_entries;
  933. sector_t sector_mov, sector_new;
  934. struct exfat_dentry *epmov, *epnew;
  935. struct super_block *sb = inode->i_sb;
  936. struct buffer_head *mov_bh, *new_bh;
  937. epmov = exfat_get_dentry(sb, p_olddir, oldentry, &mov_bh, &sector_mov);
  938. if (!epmov)
  939. return -EIO;
  940. num_old_entries = exfat_count_ext_entries(sb, p_olddir, oldentry,
  941. epmov);
  942. if (num_old_entries < 0)
  943. return -EIO;
  944. num_old_entries++;
  945. num_new_entries = exfat_calc_num_entries(p_uniname);
  946. if (num_new_entries < 0)
  947. return num_new_entries;
  948. newentry = exfat_find_empty_entry(inode, p_newdir, num_new_entries);
  949. if (newentry < 0)
  950. return newentry; /* -EIO or -ENOSPC */
  951. epnew = exfat_get_dentry(sb, p_newdir, newentry, &new_bh, &sector_new);
  952. if (!epnew)
  953. return -EIO;
  954. *epnew = *epmov;
  955. if (exfat_get_entry_type(epnew) == TYPE_FILE) {
  956. epnew->dentry.file.attr |= cpu_to_le16(ATTR_ARCHIVE);
  957. ei->attr |= ATTR_ARCHIVE;
  958. }
  959. exfat_update_bh(new_bh, IS_DIRSYNC(inode));
  960. brelse(mov_bh);
  961. brelse(new_bh);
  962. epmov = exfat_get_dentry(sb, p_olddir, oldentry + 1, &mov_bh,
  963. &sector_mov);
  964. if (!epmov)
  965. return -EIO;
  966. epnew = exfat_get_dentry(sb, p_newdir, newentry + 1, &new_bh,
  967. &sector_new);
  968. if (!epnew) {
  969. brelse(mov_bh);
  970. return -EIO;
  971. }
  972. *epnew = *epmov;
  973. exfat_update_bh(new_bh, IS_DIRSYNC(inode));
  974. brelse(mov_bh);
  975. brelse(new_bh);
  976. ret = exfat_init_ext_entry(inode, p_newdir, newentry, num_new_entries,
  977. p_uniname);
  978. if (ret)
  979. return ret;
  980. exfat_remove_entries(inode, p_olddir, oldentry, 0, num_old_entries);
  981. exfat_chain_set(&ei->dir, p_newdir->dir, p_newdir->size,
  982. p_newdir->flags);
  983. ei->entry = newentry;
  984. return 0;
  985. }
  986. static void exfat_update_parent_info(struct exfat_inode_info *ei,
  987. struct inode *parent_inode)
  988. {
  989. struct exfat_sb_info *sbi = EXFAT_SB(parent_inode->i_sb);
  990. struct exfat_inode_info *parent_ei = EXFAT_I(parent_inode);
  991. loff_t parent_isize = i_size_read(parent_inode);
  992. /*
  993. * the problem that struct exfat_inode_info caches wrong parent info.
  994. *
  995. * because of flag-mismatch of ei->dir,
  996. * there is abnormal traversing cluster chain.
  997. */
  998. if (unlikely(parent_ei->flags != ei->dir.flags ||
  999. parent_isize != EXFAT_CLU_TO_B(ei->dir.size, sbi) ||
  1000. parent_ei->start_clu != ei->dir.dir)) {
  1001. exfat_chain_set(&ei->dir, parent_ei->start_clu,
  1002. EXFAT_B_TO_CLU_ROUND_UP(parent_isize, sbi),
  1003. parent_ei->flags);
  1004. }
  1005. }
  1006. /* rename or move a old file into a new file */
  1007. static int __exfat_rename(struct inode *old_parent_inode,
  1008. struct exfat_inode_info *ei, struct inode *new_parent_inode,
  1009. struct dentry *new_dentry)
  1010. {
  1011. int ret;
  1012. int dentry;
  1013. struct exfat_chain olddir, newdir;
  1014. struct exfat_chain *p_dir = NULL;
  1015. struct exfat_uni_name uni_name;
  1016. struct exfat_dentry *ep;
  1017. struct super_block *sb = old_parent_inode->i_sb;
  1018. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  1019. const unsigned char *new_path = new_dentry->d_name.name;
  1020. struct inode *new_inode = new_dentry->d_inode;
  1021. int num_entries;
  1022. struct exfat_inode_info *new_ei = NULL;
  1023. unsigned int new_entry_type = TYPE_UNUSED;
  1024. int new_entry = 0;
  1025. struct buffer_head *old_bh, *new_bh = NULL;
  1026. /* check the validity of pointer parameters */
  1027. if (new_path == NULL || strlen(new_path) == 0)
  1028. return -EINVAL;
  1029. if (ei->dir.dir == DIR_DELETED) {
  1030. exfat_err(sb, "abnormal access to deleted source dentry");
  1031. return -ENOENT;
  1032. }
  1033. exfat_update_parent_info(ei, old_parent_inode);
  1034. exfat_chain_dup(&olddir, &ei->dir);
  1035. dentry = ei->entry;
  1036. ep = exfat_get_dentry(sb, &olddir, dentry, &old_bh, NULL);
  1037. if (!ep) {
  1038. ret = -EIO;
  1039. goto out;
  1040. }
  1041. brelse(old_bh);
  1042. /* check whether new dir is existing directory and empty */
  1043. if (new_inode) {
  1044. ret = -EIO;
  1045. new_ei = EXFAT_I(new_inode);
  1046. if (new_ei->dir.dir == DIR_DELETED) {
  1047. exfat_err(sb, "abnormal access to deleted target dentry");
  1048. goto out;
  1049. }
  1050. exfat_update_parent_info(new_ei, new_parent_inode);
  1051. p_dir = &(new_ei->dir);
  1052. new_entry = new_ei->entry;
  1053. ep = exfat_get_dentry(sb, p_dir, new_entry, &new_bh, NULL);
  1054. if (!ep)
  1055. goto out;
  1056. new_entry_type = exfat_get_entry_type(ep);
  1057. brelse(new_bh);
  1058. /* if new_inode exists, update ei */
  1059. if (new_entry_type == TYPE_DIR) {
  1060. struct exfat_chain new_clu;
  1061. new_clu.dir = new_ei->start_clu;
  1062. new_clu.size =
  1063. EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode),
  1064. sbi);
  1065. new_clu.flags = new_ei->flags;
  1066. ret = exfat_check_dir_empty(sb, &new_clu);
  1067. if (ret)
  1068. goto out;
  1069. }
  1070. }
  1071. /* check the validity of directory name in the given new pathname */
  1072. ret = exfat_resolve_path(new_parent_inode, new_path, &newdir,
  1073. &uni_name);
  1074. if (ret)
  1075. goto out;
  1076. exfat_set_volume_dirty(sb);
  1077. if (olddir.dir == newdir.dir)
  1078. ret = exfat_rename_file(new_parent_inode, &olddir, dentry,
  1079. &uni_name, ei);
  1080. else
  1081. ret = exfat_move_file(new_parent_inode, &olddir, dentry,
  1082. &newdir, &uni_name, ei);
  1083. if (!ret && new_inode) {
  1084. /* delete entries of new_dir */
  1085. ep = exfat_get_dentry(sb, p_dir, new_entry, &new_bh, NULL);
  1086. if (!ep) {
  1087. ret = -EIO;
  1088. goto del_out;
  1089. }
  1090. num_entries = exfat_count_ext_entries(sb, p_dir, new_entry, ep);
  1091. if (num_entries < 0) {
  1092. ret = -EIO;
  1093. goto del_out;
  1094. }
  1095. brelse(new_bh);
  1096. if (exfat_remove_entries(new_inode, p_dir, new_entry, 0,
  1097. num_entries + 1)) {
  1098. ret = -EIO;
  1099. goto del_out;
  1100. }
  1101. /* Free the clusters if new_inode is a dir(as if exfat_rmdir) */
  1102. if (new_entry_type == TYPE_DIR) {
  1103. /* new_ei, new_clu_to_free */
  1104. struct exfat_chain new_clu_to_free;
  1105. exfat_chain_set(&new_clu_to_free, new_ei->start_clu,
  1106. EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode),
  1107. sbi), new_ei->flags);
  1108. if (exfat_free_cluster(new_inode, &new_clu_to_free)) {
  1109. /* just set I/O error only */
  1110. ret = -EIO;
  1111. }
  1112. i_size_write(new_inode, 0);
  1113. new_ei->start_clu = EXFAT_EOF_CLUSTER;
  1114. new_ei->flags = ALLOC_NO_FAT_CHAIN;
  1115. }
  1116. del_out:
  1117. /* Update new_inode ei
  1118. * Prevent syncing removed new_inode
  1119. * (new_ei is already initialized above code ("if (new_inode)")
  1120. */
  1121. new_ei->dir.dir = DIR_DELETED;
  1122. }
  1123. exfat_clear_volume_dirty(sb);
  1124. out:
  1125. return ret;
  1126. }
  1127. static int exfat_rename(struct inode *old_dir, struct dentry *old_dentry,
  1128. struct inode *new_dir, struct dentry *new_dentry,
  1129. unsigned int flags)
  1130. {
  1131. struct inode *old_inode, *new_inode;
  1132. struct super_block *sb = old_dir->i_sb;
  1133. loff_t i_pos;
  1134. int err;
  1135. /*
  1136. * The VFS already checks for existence, so for local filesystems
  1137. * the RENAME_NOREPLACE implementation is equivalent to plain rename.
  1138. * Don't support any other flags
  1139. */
  1140. if (flags & ~RENAME_NOREPLACE)
  1141. return -EINVAL;
  1142. mutex_lock(&EXFAT_SB(sb)->s_lock);
  1143. old_inode = old_dentry->d_inode;
  1144. new_inode = new_dentry->d_inode;
  1145. err = __exfat_rename(old_dir, EXFAT_I(old_inode), new_dir, new_dentry);
  1146. if (err)
  1147. goto unlock;
  1148. inode_inc_iversion(new_dir);
  1149. new_dir->i_ctime = new_dir->i_mtime = new_dir->i_atime =
  1150. EXFAT_I(new_dir)->i_crtime = current_time(new_dir);
  1151. exfat_truncate_atime(&new_dir->i_atime);
  1152. if (IS_DIRSYNC(new_dir))
  1153. exfat_sync_inode(new_dir);
  1154. else
  1155. mark_inode_dirty(new_dir);
  1156. i_pos = ((loff_t)EXFAT_I(old_inode)->dir.dir << 32) |
  1157. (EXFAT_I(old_inode)->entry & 0xffffffff);
  1158. exfat_unhash_inode(old_inode);
  1159. exfat_hash_inode(old_inode, i_pos);
  1160. if (IS_DIRSYNC(new_dir))
  1161. exfat_sync_inode(old_inode);
  1162. else
  1163. mark_inode_dirty(old_inode);
  1164. if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
  1165. drop_nlink(old_dir);
  1166. if (!new_inode)
  1167. inc_nlink(new_dir);
  1168. }
  1169. inode_inc_iversion(old_dir);
  1170. old_dir->i_ctime = old_dir->i_mtime = current_time(old_dir);
  1171. if (IS_DIRSYNC(old_dir))
  1172. exfat_sync_inode(old_dir);
  1173. else
  1174. mark_inode_dirty(old_dir);
  1175. if (new_inode) {
  1176. exfat_unhash_inode(new_inode);
  1177. /* skip drop_nlink if new_inode already has been dropped */
  1178. if (new_inode->i_nlink) {
  1179. drop_nlink(new_inode);
  1180. if (S_ISDIR(new_inode->i_mode))
  1181. drop_nlink(new_inode);
  1182. } else {
  1183. exfat_warn(sb, "abnormal access to an inode dropped");
  1184. WARN_ON(new_inode->i_nlink == 0);
  1185. }
  1186. new_inode->i_ctime = EXFAT_I(new_inode)->i_crtime =
  1187. current_time(new_inode);
  1188. }
  1189. unlock:
  1190. mutex_unlock(&EXFAT_SB(sb)->s_lock);
  1191. return err;
  1192. }
  1193. const struct inode_operations exfat_dir_inode_operations = {
  1194. .create = exfat_create,
  1195. .lookup = exfat_lookup,
  1196. .unlink = exfat_unlink,
  1197. .mkdir = exfat_mkdir,
  1198. .rmdir = exfat_rmdir,
  1199. .rename = exfat_rename,
  1200. .setattr = exfat_setattr,
  1201. .getattr = exfat_getattr,
  1202. };