namei.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. *
  4. * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
  5. *
  6. */
  7. #include <linux/fs.h>
  8. #include <linux/nls.h>
  9. #include <linux/ctype.h>
  10. #include <linux/posix_acl.h>
  11. #include "debug.h"
  12. #include "ntfs.h"
  13. #include "ntfs_fs.h"
  14. /*
  15. * fill_name_de - Format NTFS_DE in @buf.
  16. */
  17. int fill_name_de(struct ntfs_sb_info *sbi, void *buf, const struct qstr *name,
  18. const struct cpu_str *uni)
  19. {
  20. int err;
  21. struct NTFS_DE *e = buf;
  22. u16 data_size;
  23. struct ATTR_FILE_NAME *fname = (struct ATTR_FILE_NAME *)(e + 1);
  24. #ifndef CONFIG_NTFS3_64BIT_CLUSTER
  25. e->ref.high = fname->home.high = 0;
  26. #endif
  27. if (uni) {
  28. #ifdef __BIG_ENDIAN
  29. int ulen = uni->len;
  30. __le16 *uname = fname->name;
  31. const u16 *name_cpu = uni->name;
  32. while (ulen--)
  33. *uname++ = cpu_to_le16(*name_cpu++);
  34. #else
  35. memcpy(fname->name, uni->name, uni->len * sizeof(u16));
  36. #endif
  37. fname->name_len = uni->len;
  38. } else {
  39. /* Convert input string to unicode. */
  40. err = ntfs_nls_to_utf16(sbi, name->name, name->len,
  41. (struct cpu_str *)&fname->name_len,
  42. NTFS_NAME_LEN, UTF16_LITTLE_ENDIAN);
  43. if (err < 0)
  44. return err;
  45. }
  46. fname->type = FILE_NAME_POSIX;
  47. data_size = fname_full_size(fname);
  48. e->size = cpu_to_le16(ALIGN(data_size, 8) + sizeof(struct NTFS_DE));
  49. e->key_size = cpu_to_le16(data_size);
  50. e->flags = 0;
  51. e->res = 0;
  52. return 0;
  53. }
  54. /*
  55. * ntfs_lookup - inode_operations::lookup
  56. */
  57. static struct dentry *ntfs_lookup(struct inode *dir, struct dentry *dentry,
  58. u32 flags)
  59. {
  60. struct ntfs_inode *ni = ntfs_i(dir);
  61. struct cpu_str *uni = __getname();
  62. struct inode *inode;
  63. int err;
  64. if (!uni)
  65. inode = ERR_PTR(-ENOMEM);
  66. else {
  67. err = ntfs_nls_to_utf16(ni->mi.sbi, dentry->d_name.name,
  68. dentry->d_name.len, uni, NTFS_NAME_LEN,
  69. UTF16_HOST_ENDIAN);
  70. if (err < 0)
  71. inode = ERR_PTR(err);
  72. else {
  73. ni_lock_dir(ni);
  74. inode = dir_search_u(dir, uni, NULL);
  75. ni_unlock(ni);
  76. }
  77. __putname(uni);
  78. }
  79. /*
  80. * Check for a null pointer
  81. * If the MFT record of ntfs inode is not a base record, inode->i_op can be NULL.
  82. * This causes null pointer dereference in d_splice_alias().
  83. */
  84. if (!IS_ERR_OR_NULL(inode) && !inode->i_op) {
  85. iput(inode);
  86. inode = ERR_PTR(-EINVAL);
  87. }
  88. return d_splice_alias(inode, dentry);
  89. }
  90. /*
  91. * ntfs_create - inode_operations::create
  92. */
  93. static int ntfs_create(struct mnt_idmap *idmap, struct inode *dir,
  94. struct dentry *dentry, umode_t mode, bool excl)
  95. {
  96. return ntfs_create_inode(idmap, dir, dentry, NULL, S_IFREG | mode, 0,
  97. NULL, 0, NULL);
  98. }
  99. /*
  100. * ntfs_mknod - inode_operations::mknod
  101. */
  102. static int ntfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
  103. struct dentry *dentry, umode_t mode, dev_t rdev)
  104. {
  105. return ntfs_create_inode(idmap, dir, dentry, NULL, mode, rdev, NULL, 0,
  106. NULL);
  107. }
  108. /*
  109. * ntfs_link - inode_operations::link
  110. */
  111. static int ntfs_link(struct dentry *ode, struct inode *dir, struct dentry *de)
  112. {
  113. int err;
  114. struct inode *inode = d_inode(ode);
  115. struct ntfs_inode *ni = ntfs_i(inode);
  116. if (S_ISDIR(inode->i_mode))
  117. return -EPERM;
  118. if (inode->i_nlink >= NTFS_LINK_MAX)
  119. return -EMLINK;
  120. ni_lock_dir(ntfs_i(dir));
  121. if (inode != dir)
  122. ni_lock(ni);
  123. inc_nlink(inode);
  124. ihold(inode);
  125. err = ntfs_link_inode(inode, de);
  126. if (!err) {
  127. inode_set_ctime_current(inode);
  128. inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
  129. mark_inode_dirty(inode);
  130. mark_inode_dirty(dir);
  131. d_instantiate(de, inode);
  132. } else {
  133. drop_nlink(inode);
  134. iput(inode);
  135. }
  136. if (inode != dir)
  137. ni_unlock(ni);
  138. ni_unlock(ntfs_i(dir));
  139. return err;
  140. }
  141. /*
  142. * ntfs_unlink - inode_operations::unlink
  143. */
  144. static int ntfs_unlink(struct inode *dir, struct dentry *dentry)
  145. {
  146. struct ntfs_inode *ni = ntfs_i(dir);
  147. int err;
  148. if (unlikely(ntfs3_forced_shutdown(dir->i_sb)))
  149. return -EIO;
  150. ni_lock_dir(ni);
  151. err = ntfs_unlink_inode(dir, dentry);
  152. ni_unlock(ni);
  153. return err;
  154. }
  155. /*
  156. * ntfs_symlink - inode_operations::symlink
  157. */
  158. static int ntfs_symlink(struct mnt_idmap *idmap, struct inode *dir,
  159. struct dentry *dentry, const char *symname)
  160. {
  161. u32 size = strlen(symname);
  162. if (unlikely(ntfs3_forced_shutdown(dir->i_sb)))
  163. return -EIO;
  164. return ntfs_create_inode(idmap, dir, dentry, NULL, S_IFLNK | 0777, 0,
  165. symname, size, NULL);
  166. }
  167. /*
  168. * ntfs_mkdir- inode_operations::mkdir
  169. */
  170. static int ntfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
  171. struct dentry *dentry, umode_t mode)
  172. {
  173. return ntfs_create_inode(idmap, dir, dentry, NULL, S_IFDIR | mode, 0,
  174. NULL, 0, NULL);
  175. }
  176. /*
  177. * ntfs_rmdir - inode_operations::rmdir
  178. */
  179. static int ntfs_rmdir(struct inode *dir, struct dentry *dentry)
  180. {
  181. struct ntfs_inode *ni = ntfs_i(dir);
  182. int err;
  183. if (unlikely(ntfs3_forced_shutdown(dir->i_sb)))
  184. return -EIO;
  185. ni_lock_dir(ni);
  186. err = ntfs_unlink_inode(dir, dentry);
  187. ni_unlock(ni);
  188. return err;
  189. }
  190. /*
  191. * ntfs_rename - inode_operations::rename
  192. */
  193. static int ntfs_rename(struct mnt_idmap *idmap, struct inode *dir,
  194. struct dentry *dentry, struct inode *new_dir,
  195. struct dentry *new_dentry, u32 flags)
  196. {
  197. int err;
  198. struct super_block *sb = dir->i_sb;
  199. struct ntfs_sb_info *sbi = sb->s_fs_info;
  200. struct ntfs_inode *dir_ni = ntfs_i(dir);
  201. struct ntfs_inode *new_dir_ni = ntfs_i(new_dir);
  202. struct inode *inode = d_inode(dentry);
  203. struct ntfs_inode *ni = ntfs_i(inode);
  204. struct inode *new_inode = d_inode(new_dentry);
  205. struct NTFS_DE *de, *new_de;
  206. bool is_same;
  207. /*
  208. * de - memory of PATH_MAX bytes:
  209. * [0-1024) - original name (dentry->d_name)
  210. * [1024-2048) - paired to original name, usually DOS variant of dentry->d_name
  211. * [2048-3072) - new name (new_dentry->d_name)
  212. */
  213. static_assert(SIZEOF_ATTRIBUTE_FILENAME_MAX + SIZEOF_RESIDENT < 1024);
  214. static_assert(SIZEOF_ATTRIBUTE_FILENAME_MAX + sizeof(struct NTFS_DE) <
  215. 1024);
  216. static_assert(PATH_MAX >= 4 * 1024);
  217. if (unlikely(ntfs3_forced_shutdown(sb)))
  218. return -EIO;
  219. if (flags & ~RENAME_NOREPLACE)
  220. return -EINVAL;
  221. is_same = dentry->d_name.len == new_dentry->d_name.len &&
  222. !memcmp(dentry->d_name.name, new_dentry->d_name.name,
  223. dentry->d_name.len);
  224. if (is_same && dir == new_dir) {
  225. /* Nothing to do. */
  226. return 0;
  227. }
  228. if (ntfs_is_meta_file(sbi, inode->i_ino)) {
  229. /* Should we print an error? */
  230. return -EINVAL;
  231. }
  232. if (new_inode) {
  233. /* Target name exists. Unlink it. */
  234. dget(new_dentry);
  235. ni_lock_dir(new_dir_ni);
  236. err = ntfs_unlink_inode(new_dir, new_dentry);
  237. ni_unlock(new_dir_ni);
  238. dput(new_dentry);
  239. if (err)
  240. return err;
  241. }
  242. /* Allocate PATH_MAX bytes. */
  243. de = __getname();
  244. if (!de)
  245. return -ENOMEM;
  246. /* Translate dentry->d_name into unicode form. */
  247. err = fill_name_de(sbi, de, &dentry->d_name, NULL);
  248. if (err < 0)
  249. goto out;
  250. if (is_same) {
  251. /* Reuse 'de'. */
  252. new_de = de;
  253. } else {
  254. /* Translate new_dentry->d_name into unicode form. */
  255. new_de = Add2Ptr(de, 2048);
  256. err = fill_name_de(sbi, new_de, &new_dentry->d_name, NULL);
  257. if (err < 0)
  258. goto out;
  259. }
  260. ni_lock_dir(dir_ni);
  261. ni_lock(ni);
  262. if (dir_ni != new_dir_ni)
  263. ni_lock_dir2(new_dir_ni);
  264. err = ni_rename(dir_ni, new_dir_ni, ni, de, new_de);
  265. if (!err) {
  266. simple_rename_timestamp(dir, dentry, new_dir, new_dentry);
  267. mark_inode_dirty(inode);
  268. mark_inode_dirty(dir);
  269. if (dir != new_dir)
  270. mark_inode_dirty(new_dir);
  271. if (IS_DIRSYNC(dir))
  272. ntfs_sync_inode(dir);
  273. if (IS_DIRSYNC(new_dir))
  274. ntfs_sync_inode(inode);
  275. }
  276. if (dir_ni != new_dir_ni)
  277. ni_unlock(new_dir_ni);
  278. ni_unlock(ni);
  279. ni_unlock(dir_ni);
  280. out:
  281. __putname(de);
  282. return err;
  283. }
  284. struct dentry *ntfs3_get_parent(struct dentry *child)
  285. {
  286. struct inode *inode = d_inode(child);
  287. struct ntfs_inode *ni = ntfs_i(inode);
  288. struct ATTR_LIST_ENTRY *le = NULL;
  289. struct ATTRIB *attr = NULL;
  290. struct ATTR_FILE_NAME *fname;
  291. while ((attr = ni_find_attr(ni, attr, &le, ATTR_NAME, NULL, 0, NULL,
  292. NULL))) {
  293. fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
  294. if (!fname)
  295. continue;
  296. return d_obtain_alias(
  297. ntfs_iget5(inode->i_sb, &fname->home, NULL));
  298. }
  299. return ERR_PTR(-ENOENT);
  300. }
  301. /*
  302. * dentry_operations::d_hash
  303. */
  304. static int ntfs_d_hash(const struct dentry *dentry, struct qstr *name)
  305. {
  306. struct ntfs_sb_info *sbi;
  307. const char *n = name->name;
  308. unsigned int len = name->len;
  309. unsigned long hash;
  310. struct cpu_str *uni;
  311. unsigned int c;
  312. int err;
  313. /* First try fast implementation. */
  314. hash = init_name_hash(dentry);
  315. for (;;) {
  316. if (!len--) {
  317. name->hash = end_name_hash(hash);
  318. return 0;
  319. }
  320. c = *n++;
  321. if (c >= 0x80)
  322. break;
  323. hash = partial_name_hash(toupper(c), hash);
  324. }
  325. /*
  326. * Try slow way with current upcase table
  327. */
  328. uni = kmem_cache_alloc(names_cachep, GFP_NOWAIT);
  329. if (!uni)
  330. return -ENOMEM;
  331. sbi = dentry->d_sb->s_fs_info;
  332. err = ntfs_nls_to_utf16(sbi, name->name, name->len, uni, NTFS_NAME_LEN,
  333. UTF16_HOST_ENDIAN);
  334. if (err < 0)
  335. goto out;
  336. if (!err) {
  337. err = -EINVAL;
  338. goto out;
  339. }
  340. hash = ntfs_names_hash(uni->name, uni->len, sbi->upcase,
  341. init_name_hash(dentry));
  342. name->hash = end_name_hash(hash);
  343. err = 0;
  344. out:
  345. kmem_cache_free(names_cachep, uni);
  346. return err;
  347. }
  348. /*
  349. * dentry_operations::d_compare
  350. */
  351. static int ntfs_d_compare(const struct dentry *dentry, unsigned int len1,
  352. const char *str, const struct qstr *name)
  353. {
  354. struct ntfs_sb_info *sbi;
  355. int ret;
  356. const char *n1 = str;
  357. const char *n2 = name->name;
  358. unsigned int len2 = name->len;
  359. unsigned int lm = min(len1, len2);
  360. unsigned char c1, c2;
  361. struct cpu_str *uni1;
  362. struct le_str *uni2;
  363. /* First try fast implementation. */
  364. for (;;) {
  365. if (!lm--)
  366. return len1 != len2;
  367. if ((c1 = *n1++) == (c2 = *n2++))
  368. continue;
  369. if (c1 >= 0x80 || c2 >= 0x80)
  370. break;
  371. if (toupper(c1) != toupper(c2))
  372. return 1;
  373. }
  374. /*
  375. * Try slow way with current upcase table
  376. */
  377. sbi = dentry->d_sb->s_fs_info;
  378. uni1 = __getname();
  379. if (!uni1)
  380. return -ENOMEM;
  381. ret = ntfs_nls_to_utf16(sbi, str, len1, uni1, NTFS_NAME_LEN,
  382. UTF16_HOST_ENDIAN);
  383. if (ret < 0)
  384. goto out;
  385. if (!ret) {
  386. ret = -EINVAL;
  387. goto out;
  388. }
  389. uni2 = Add2Ptr(uni1, 2048);
  390. ret = ntfs_nls_to_utf16(sbi, name->name, name->len,
  391. (struct cpu_str *)uni2, NTFS_NAME_LEN,
  392. UTF16_LITTLE_ENDIAN);
  393. if (ret < 0)
  394. goto out;
  395. if (!ret) {
  396. ret = -EINVAL;
  397. goto out;
  398. }
  399. ret = !ntfs_cmp_names_cpu(uni1, uni2, sbi->upcase, false) ? 0 : 1;
  400. out:
  401. __putname(uni1);
  402. return ret;
  403. }
  404. // clang-format off
  405. const struct inode_operations ntfs_dir_inode_operations = {
  406. .lookup = ntfs_lookup,
  407. .create = ntfs_create,
  408. .link = ntfs_link,
  409. .unlink = ntfs_unlink,
  410. .symlink = ntfs_symlink,
  411. .mkdir = ntfs_mkdir,
  412. .rmdir = ntfs_rmdir,
  413. .mknod = ntfs_mknod,
  414. .rename = ntfs_rename,
  415. .get_acl = ntfs_get_acl,
  416. .set_acl = ntfs_set_acl,
  417. .setattr = ntfs_setattr,
  418. .getattr = ntfs_getattr,
  419. .listxattr = ntfs_listxattr,
  420. .fiemap = ntfs_fiemap,
  421. .fileattr_get = ntfs_fileattr_get,
  422. .fileattr_set = ntfs_fileattr_set,
  423. };
  424. const struct inode_operations ntfs_special_inode_operations = {
  425. .setattr = ntfs_setattr,
  426. .getattr = ntfs_getattr,
  427. .listxattr = ntfs_listxattr,
  428. .get_acl = ntfs_get_acl,
  429. .set_acl = ntfs_set_acl,
  430. };
  431. const struct dentry_operations ntfs_dentry_ops = {
  432. .d_hash = ntfs_d_hash,
  433. .d_compare = ntfs_d_compare,
  434. };
  435. // clang-format on