namei.c 34 KB

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