namei.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/affs/namei.c
  4. *
  5. * (c) 1996 Hans-Joachim Widmaier - Rewritten
  6. *
  7. * (C) 1993 Ray Burr - Modified for Amiga FFS filesystem.
  8. *
  9. * (C) 1991 Linus Torvalds - minix filesystem
  10. */
  11. #include "affs.h"
  12. #include <linux/exportfs.h>
  13. typedef int (*toupper_t)(int);
  14. /* Simple toupper() for DOS\1 */
  15. static int
  16. affs_toupper(int ch)
  17. {
  18. return ch >= 'a' && ch <= 'z' ? ch -= ('a' - 'A') : ch;
  19. }
  20. /* International toupper() for DOS\3 ("international") */
  21. static int
  22. affs_intl_toupper(int ch)
  23. {
  24. return (ch >= 'a' && ch <= 'z') || (ch >= 0xE0
  25. && ch <= 0xFE && ch != 0xF7) ?
  26. ch - ('a' - 'A') : ch;
  27. }
  28. static inline toupper_t
  29. affs_get_toupper(struct super_block *sb)
  30. {
  31. return affs_test_opt(AFFS_SB(sb)->s_flags, SF_INTL) ?
  32. affs_intl_toupper : affs_toupper;
  33. }
  34. /*
  35. * Note: the dentry argument is the parent dentry.
  36. */
  37. static inline int
  38. __affs_hash_dentry(const struct dentry *dentry, struct qstr *qstr, toupper_t toupper, bool notruncate)
  39. {
  40. const u8 *name = qstr->name;
  41. unsigned long hash;
  42. int retval;
  43. u32 len;
  44. retval = affs_check_name(qstr->name, qstr->len, notruncate);
  45. if (retval)
  46. return retval;
  47. hash = init_name_hash(dentry);
  48. len = min(qstr->len, AFFSNAMEMAX);
  49. for (; len > 0; name++, len--)
  50. hash = partial_name_hash(toupper(*name), hash);
  51. qstr->hash = end_name_hash(hash);
  52. return 0;
  53. }
  54. static int
  55. affs_hash_dentry(const struct dentry *dentry, struct qstr *qstr)
  56. {
  57. return __affs_hash_dentry(dentry, qstr, affs_toupper,
  58. affs_nofilenametruncate(dentry));
  59. }
  60. static int
  61. affs_intl_hash_dentry(const struct dentry *dentry, struct qstr *qstr)
  62. {
  63. return __affs_hash_dentry(dentry, qstr, affs_intl_toupper,
  64. affs_nofilenametruncate(dentry));
  65. }
  66. static inline int __affs_compare_dentry(unsigned int len,
  67. const char *str, const struct qstr *name, toupper_t toupper,
  68. bool notruncate)
  69. {
  70. const u8 *aname = str;
  71. const u8 *bname = name->name;
  72. /*
  73. * 'str' is the name of an already existing dentry, so the name
  74. * must be valid. 'name' must be validated first.
  75. */
  76. if (affs_check_name(name->name, name->len, notruncate))
  77. return 1;
  78. /*
  79. * If the names are longer than the allowed 30 chars,
  80. * the excess is ignored, so their length may differ.
  81. */
  82. if (len >= AFFSNAMEMAX) {
  83. if (name->len < AFFSNAMEMAX)
  84. return 1;
  85. len = AFFSNAMEMAX;
  86. } else if (len != name->len)
  87. return 1;
  88. for (; len > 0; len--)
  89. if (toupper(*aname++) != toupper(*bname++))
  90. return 1;
  91. return 0;
  92. }
  93. static int
  94. affs_compare_dentry(const struct dentry *dentry,
  95. unsigned int len, const char *str, const struct qstr *name)
  96. {
  97. return __affs_compare_dentry(len, str, name, affs_toupper,
  98. affs_nofilenametruncate(dentry));
  99. }
  100. static int
  101. affs_intl_compare_dentry(const struct dentry *dentry,
  102. unsigned int len, const char *str, const struct qstr *name)
  103. {
  104. return __affs_compare_dentry(len, str, name, affs_intl_toupper,
  105. affs_nofilenametruncate(dentry));
  106. }
  107. /*
  108. * NOTE! unlike strncmp, affs_match returns 1 for success, 0 for failure.
  109. */
  110. static inline int
  111. affs_match(struct dentry *dentry, const u8 *name2, toupper_t toupper)
  112. {
  113. const u8 *name = dentry->d_name.name;
  114. int len = dentry->d_name.len;
  115. if (len >= AFFSNAMEMAX) {
  116. if (*name2 < AFFSNAMEMAX)
  117. return 0;
  118. len = AFFSNAMEMAX;
  119. } else if (len != *name2)
  120. return 0;
  121. for (name2++; len > 0; len--)
  122. if (toupper(*name++) != toupper(*name2++))
  123. return 0;
  124. return 1;
  125. }
  126. int
  127. affs_hash_name(struct super_block *sb, const u8 *name, unsigned int len)
  128. {
  129. toupper_t toupper = affs_get_toupper(sb);
  130. u32 hash;
  131. hash = len = min(len, AFFSNAMEMAX);
  132. for (; len > 0; len--)
  133. hash = (hash * 13 + toupper(*name++)) & 0x7ff;
  134. return hash % AFFS_SB(sb)->s_hashsize;
  135. }
  136. static struct buffer_head *
  137. affs_find_entry(struct inode *dir, struct dentry *dentry)
  138. {
  139. struct super_block *sb = dir->i_sb;
  140. struct buffer_head *bh;
  141. toupper_t toupper = affs_get_toupper(sb);
  142. u32 key;
  143. pr_debug("%s(\"%pd\")\n", __func__, dentry);
  144. bh = affs_bread(sb, dir->i_ino);
  145. if (!bh)
  146. return ERR_PTR(-EIO);
  147. key = be32_to_cpu(AFFS_HEAD(bh)->table[affs_hash_name(sb, dentry->d_name.name, dentry->d_name.len)]);
  148. for (;;) {
  149. affs_brelse(bh);
  150. if (key == 0)
  151. return NULL;
  152. bh = affs_bread(sb, key);
  153. if (!bh)
  154. return ERR_PTR(-EIO);
  155. if (affs_match(dentry, AFFS_TAIL(sb, bh)->name, toupper))
  156. return bh;
  157. key = be32_to_cpu(AFFS_TAIL(sb, bh)->hash_chain);
  158. }
  159. }
  160. struct dentry *
  161. affs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
  162. {
  163. struct super_block *sb = dir->i_sb;
  164. struct buffer_head *bh;
  165. struct inode *inode = NULL;
  166. struct dentry *res;
  167. pr_debug("%s(\"%pd\")\n", __func__, dentry);
  168. affs_lock_dir(dir);
  169. bh = affs_find_entry(dir, dentry);
  170. if (IS_ERR(bh)) {
  171. affs_unlock_dir(dir);
  172. return ERR_CAST(bh);
  173. }
  174. if (bh) {
  175. u32 ino = bh->b_blocknr;
  176. /* store the real header ino in d_fsdata for faster lookups */
  177. dentry->d_fsdata = (void *)(long)ino;
  178. switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) {
  179. //link to dirs disabled
  180. //case ST_LINKDIR:
  181. case ST_LINKFILE:
  182. ino = be32_to_cpu(AFFS_TAIL(sb, bh)->original);
  183. }
  184. affs_brelse(bh);
  185. inode = affs_iget(sb, ino);
  186. }
  187. res = d_splice_alias(inode, dentry);
  188. if (!IS_ERR_OR_NULL(res))
  189. res->d_fsdata = dentry->d_fsdata;
  190. affs_unlock_dir(dir);
  191. return res;
  192. }
  193. int
  194. affs_unlink(struct inode *dir, struct dentry *dentry)
  195. {
  196. pr_debug("%s(dir=%lu, %lu \"%pd\")\n", __func__, dir->i_ino,
  197. d_inode(dentry)->i_ino, dentry);
  198. return affs_remove_header(dentry);
  199. }
  200. int
  201. affs_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool excl)
  202. {
  203. struct super_block *sb = dir->i_sb;
  204. struct inode *inode;
  205. int error;
  206. pr_debug("%s(%lu,\"%pd\",0%ho)\n",
  207. __func__, dir->i_ino, dentry, mode);
  208. inode = affs_new_inode(dir);
  209. if (!inode)
  210. return -ENOSPC;
  211. inode->i_mode = mode;
  212. affs_mode_to_prot(inode);
  213. mark_inode_dirty(inode);
  214. inode->i_op = &affs_file_inode_operations;
  215. inode->i_fop = &affs_file_operations;
  216. inode->i_mapping->a_ops = affs_test_opt(AFFS_SB(sb)->s_flags, SF_OFS) ?
  217. &affs_aops_ofs : &affs_aops;
  218. error = affs_add_entry(dir, inode, dentry, ST_FILE);
  219. if (error) {
  220. clear_nlink(inode);
  221. iput(inode);
  222. return error;
  223. }
  224. return 0;
  225. }
  226. int
  227. affs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  228. {
  229. struct inode *inode;
  230. int error;
  231. pr_debug("%s(%lu,\"%pd\",0%ho)\n",
  232. __func__, dir->i_ino, dentry, mode);
  233. inode = affs_new_inode(dir);
  234. if (!inode)
  235. return -ENOSPC;
  236. inode->i_mode = S_IFDIR | mode;
  237. affs_mode_to_prot(inode);
  238. inode->i_op = &affs_dir_inode_operations;
  239. inode->i_fop = &affs_dir_operations;
  240. error = affs_add_entry(dir, inode, dentry, ST_USERDIR);
  241. if (error) {
  242. clear_nlink(inode);
  243. mark_inode_dirty(inode);
  244. iput(inode);
  245. return error;
  246. }
  247. return 0;
  248. }
  249. int
  250. affs_rmdir(struct inode *dir, struct dentry *dentry)
  251. {
  252. pr_debug("%s(dir=%lu, %lu \"%pd\")\n", __func__, dir->i_ino,
  253. d_inode(dentry)->i_ino, dentry);
  254. return affs_remove_header(dentry);
  255. }
  256. int
  257. affs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  258. {
  259. struct super_block *sb = dir->i_sb;
  260. struct buffer_head *bh;
  261. struct inode *inode;
  262. char *p;
  263. int i, maxlen, error;
  264. char c, lc;
  265. pr_debug("%s(%lu,\"%pd\" -> \"%s\")\n",
  266. __func__, dir->i_ino, dentry, symname);
  267. maxlen = AFFS_SB(sb)->s_hashsize * sizeof(u32) - 1;
  268. inode = affs_new_inode(dir);
  269. if (!inode)
  270. return -ENOSPC;
  271. inode->i_op = &affs_symlink_inode_operations;
  272. inode_nohighmem(inode);
  273. inode->i_data.a_ops = &affs_symlink_aops;
  274. inode->i_mode = S_IFLNK | 0777;
  275. affs_mode_to_prot(inode);
  276. error = -EIO;
  277. bh = affs_bread(sb, inode->i_ino);
  278. if (!bh)
  279. goto err;
  280. i = 0;
  281. p = (char *)AFFS_HEAD(bh)->table;
  282. lc = '/';
  283. if (*symname == '/') {
  284. struct affs_sb_info *sbi = AFFS_SB(sb);
  285. while (*symname == '/')
  286. symname++;
  287. spin_lock(&sbi->symlink_lock);
  288. while (sbi->s_volume[i]) /* Cannot overflow */
  289. *p++ = sbi->s_volume[i++];
  290. spin_unlock(&sbi->symlink_lock);
  291. }
  292. while (i < maxlen && (c = *symname++)) {
  293. if (c == '.' && lc == '/' && *symname == '.' && symname[1] == '/') {
  294. *p++ = '/';
  295. i++;
  296. symname += 2;
  297. lc = '/';
  298. } else if (c == '.' && lc == '/' && *symname == '/') {
  299. symname++;
  300. lc = '/';
  301. } else {
  302. *p++ = c;
  303. lc = c;
  304. i++;
  305. }
  306. if (lc == '/')
  307. while (*symname == '/')
  308. symname++;
  309. }
  310. *p = 0;
  311. inode->i_size = i + 1;
  312. mark_buffer_dirty_inode(bh, inode);
  313. affs_brelse(bh);
  314. mark_inode_dirty(inode);
  315. error = affs_add_entry(dir, inode, dentry, ST_SOFTLINK);
  316. if (error)
  317. goto err;
  318. return 0;
  319. err:
  320. clear_nlink(inode);
  321. mark_inode_dirty(inode);
  322. iput(inode);
  323. return error;
  324. }
  325. int
  326. affs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  327. {
  328. struct inode *inode = d_inode(old_dentry);
  329. pr_debug("%s(%lu, %lu, \"%pd\")\n", __func__, inode->i_ino, dir->i_ino,
  330. dentry);
  331. return affs_add_entry(dir, inode, dentry, ST_LINKFILE);
  332. }
  333. static int
  334. affs_rename(struct inode *old_dir, struct dentry *old_dentry,
  335. struct inode *new_dir, struct dentry *new_dentry)
  336. {
  337. struct super_block *sb = old_dir->i_sb;
  338. struct buffer_head *bh = NULL;
  339. int retval;
  340. retval = affs_check_name(new_dentry->d_name.name,
  341. new_dentry->d_name.len,
  342. affs_nofilenametruncate(old_dentry));
  343. if (retval)
  344. return retval;
  345. /* Unlink destination if it already exists */
  346. if (d_really_is_positive(new_dentry)) {
  347. retval = affs_remove_header(new_dentry);
  348. if (retval)
  349. return retval;
  350. }
  351. bh = affs_bread(sb, d_inode(old_dentry)->i_ino);
  352. if (!bh)
  353. return -EIO;
  354. /* Remove header from its parent directory. */
  355. affs_lock_dir(old_dir);
  356. retval = affs_remove_hash(old_dir, bh);
  357. affs_unlock_dir(old_dir);
  358. if (retval)
  359. goto done;
  360. /* And insert it into the new directory with the new name. */
  361. affs_copy_name(AFFS_TAIL(sb, bh)->name, new_dentry);
  362. affs_fix_checksum(sb, bh);
  363. affs_lock_dir(new_dir);
  364. retval = affs_insert_hash(new_dir, bh);
  365. affs_unlock_dir(new_dir);
  366. /* TODO: move it back to old_dir, if error? */
  367. done:
  368. mark_buffer_dirty_inode(bh, retval ? old_dir : new_dir);
  369. affs_brelse(bh);
  370. return retval;
  371. }
  372. static int
  373. affs_xrename(struct inode *old_dir, struct dentry *old_dentry,
  374. struct inode *new_dir, struct dentry *new_dentry)
  375. {
  376. struct super_block *sb = old_dir->i_sb;
  377. struct buffer_head *bh_old = NULL;
  378. struct buffer_head *bh_new = NULL;
  379. int retval;
  380. bh_old = affs_bread(sb, d_inode(old_dentry)->i_ino);
  381. if (!bh_old)
  382. return -EIO;
  383. bh_new = affs_bread(sb, d_inode(new_dentry)->i_ino);
  384. if (!bh_new)
  385. return -EIO;
  386. /* Remove old header from its parent directory. */
  387. affs_lock_dir(old_dir);
  388. retval = affs_remove_hash(old_dir, bh_old);
  389. affs_unlock_dir(old_dir);
  390. if (retval)
  391. goto done;
  392. /* Remove new header from its parent directory. */
  393. affs_lock_dir(new_dir);
  394. retval = affs_remove_hash(new_dir, bh_new);
  395. affs_unlock_dir(new_dir);
  396. if (retval)
  397. goto done;
  398. /* Insert old into the new directory with the new name. */
  399. affs_copy_name(AFFS_TAIL(sb, bh_old)->name, new_dentry);
  400. affs_fix_checksum(sb, bh_old);
  401. affs_lock_dir(new_dir);
  402. retval = affs_insert_hash(new_dir, bh_old);
  403. affs_unlock_dir(new_dir);
  404. /* Insert new into the old directory with the old name. */
  405. affs_copy_name(AFFS_TAIL(sb, bh_new)->name, old_dentry);
  406. affs_fix_checksum(sb, bh_new);
  407. affs_lock_dir(old_dir);
  408. retval = affs_insert_hash(old_dir, bh_new);
  409. affs_unlock_dir(old_dir);
  410. done:
  411. mark_buffer_dirty_inode(bh_old, new_dir);
  412. mark_buffer_dirty_inode(bh_new, old_dir);
  413. affs_brelse(bh_old);
  414. affs_brelse(bh_new);
  415. return retval;
  416. }
  417. int affs_rename2(struct inode *old_dir, struct dentry *old_dentry,
  418. struct inode *new_dir, struct dentry *new_dentry,
  419. unsigned int flags)
  420. {
  421. if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
  422. return -EINVAL;
  423. pr_debug("%s(old=%lu,\"%pd\" to new=%lu,\"%pd\")\n", __func__,
  424. old_dir->i_ino, old_dentry, new_dir->i_ino, new_dentry);
  425. if (flags & RENAME_EXCHANGE)
  426. return affs_xrename(old_dir, old_dentry, new_dir, new_dentry);
  427. return affs_rename(old_dir, old_dentry, new_dir, new_dentry);
  428. }
  429. static struct dentry *affs_get_parent(struct dentry *child)
  430. {
  431. struct inode *parent;
  432. struct buffer_head *bh;
  433. bh = affs_bread(child->d_sb, d_inode(child)->i_ino);
  434. if (!bh)
  435. return ERR_PTR(-EIO);
  436. parent = affs_iget(child->d_sb,
  437. be32_to_cpu(AFFS_TAIL(child->d_sb, bh)->parent));
  438. brelse(bh);
  439. if (IS_ERR(parent))
  440. return ERR_CAST(parent);
  441. return d_obtain_alias(parent);
  442. }
  443. static struct inode *affs_nfs_get_inode(struct super_block *sb, u64 ino,
  444. u32 generation)
  445. {
  446. struct inode *inode;
  447. if (!affs_validblock(sb, ino))
  448. return ERR_PTR(-ESTALE);
  449. inode = affs_iget(sb, ino);
  450. if (IS_ERR(inode))
  451. return ERR_CAST(inode);
  452. return inode;
  453. }
  454. static struct dentry *affs_fh_to_dentry(struct super_block *sb, struct fid *fid,
  455. int fh_len, int fh_type)
  456. {
  457. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  458. affs_nfs_get_inode);
  459. }
  460. static struct dentry *affs_fh_to_parent(struct super_block *sb, struct fid *fid,
  461. int fh_len, int fh_type)
  462. {
  463. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  464. affs_nfs_get_inode);
  465. }
  466. const struct export_operations affs_export_ops = {
  467. .fh_to_dentry = affs_fh_to_dentry,
  468. .fh_to_parent = affs_fh_to_parent,
  469. .get_parent = affs_get_parent,
  470. };
  471. const struct dentry_operations affs_dentry_operations = {
  472. .d_hash = affs_hash_dentry,
  473. .d_compare = affs_compare_dentry,
  474. };
  475. const struct dentry_operations affs_intl_dentry_operations = {
  476. .d_hash = affs_intl_hash_dentry,
  477. .d_compare = affs_intl_compare_dentry,
  478. };