namei.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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. pr_debug("%s(\"%pd\")\n", __func__, dentry);
  167. affs_lock_dir(dir);
  168. bh = affs_find_entry(dir, dentry);
  169. affs_unlock_dir(dir);
  170. if (IS_ERR(bh))
  171. return ERR_CAST(bh);
  172. if (bh) {
  173. u32 ino = bh->b_blocknr;
  174. /* store the real header ino in d_fsdata for faster lookups */
  175. dentry->d_fsdata = (void *)(long)ino;
  176. switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) {
  177. //link to dirs disabled
  178. //case ST_LINKDIR:
  179. case ST_LINKFILE:
  180. ino = be32_to_cpu(AFFS_TAIL(sb, bh)->original);
  181. }
  182. affs_brelse(bh);
  183. inode = affs_iget(sb, ino);
  184. if (IS_ERR(inode))
  185. return ERR_CAST(inode);
  186. }
  187. d_add(dentry, inode);
  188. return NULL;
  189. }
  190. int
  191. affs_unlink(struct inode *dir, struct dentry *dentry)
  192. {
  193. pr_debug("%s(dir=%lu, %lu \"%pd\")\n", __func__, dir->i_ino,
  194. d_inode(dentry)->i_ino, dentry);
  195. return affs_remove_header(dentry);
  196. }
  197. int
  198. affs_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool excl)
  199. {
  200. struct super_block *sb = dir->i_sb;
  201. struct inode *inode;
  202. int error;
  203. pr_debug("%s(%lu,\"%pd\",0%ho)\n",
  204. __func__, dir->i_ino, dentry, mode);
  205. inode = affs_new_inode(dir);
  206. if (!inode)
  207. return -ENOSPC;
  208. inode->i_mode = mode;
  209. affs_mode_to_prot(inode);
  210. mark_inode_dirty(inode);
  211. inode->i_op = &affs_file_inode_operations;
  212. inode->i_fop = &affs_file_operations;
  213. inode->i_mapping->a_ops = affs_test_opt(AFFS_SB(sb)->s_flags, SF_OFS) ?
  214. &affs_aops_ofs : &affs_aops;
  215. error = affs_add_entry(dir, inode, dentry, ST_FILE);
  216. if (error) {
  217. clear_nlink(inode);
  218. iput(inode);
  219. return error;
  220. }
  221. return 0;
  222. }
  223. int
  224. affs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  225. {
  226. struct inode *inode;
  227. int error;
  228. pr_debug("%s(%lu,\"%pd\",0%ho)\n",
  229. __func__, dir->i_ino, dentry, mode);
  230. inode = affs_new_inode(dir);
  231. if (!inode)
  232. return -ENOSPC;
  233. inode->i_mode = S_IFDIR | mode;
  234. affs_mode_to_prot(inode);
  235. inode->i_op = &affs_dir_inode_operations;
  236. inode->i_fop = &affs_dir_operations;
  237. error = affs_add_entry(dir, inode, dentry, ST_USERDIR);
  238. if (error) {
  239. clear_nlink(inode);
  240. mark_inode_dirty(inode);
  241. iput(inode);
  242. return error;
  243. }
  244. return 0;
  245. }
  246. int
  247. affs_rmdir(struct inode *dir, struct dentry *dentry)
  248. {
  249. pr_debug("%s(dir=%lu, %lu \"%pd\")\n", __func__, dir->i_ino,
  250. d_inode(dentry)->i_ino, dentry);
  251. return affs_remove_header(dentry);
  252. }
  253. int
  254. affs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  255. {
  256. struct super_block *sb = dir->i_sb;
  257. struct buffer_head *bh;
  258. struct inode *inode;
  259. char *p;
  260. int i, maxlen, error;
  261. char c, lc;
  262. pr_debug("%s(%lu,\"%pd\" -> \"%s\")\n",
  263. __func__, dir->i_ino, dentry, symname);
  264. maxlen = AFFS_SB(sb)->s_hashsize * sizeof(u32) - 1;
  265. inode = affs_new_inode(dir);
  266. if (!inode)
  267. return -ENOSPC;
  268. inode->i_op = &affs_symlink_inode_operations;
  269. inode_nohighmem(inode);
  270. inode->i_data.a_ops = &affs_symlink_aops;
  271. inode->i_mode = S_IFLNK | 0777;
  272. affs_mode_to_prot(inode);
  273. error = -EIO;
  274. bh = affs_bread(sb, inode->i_ino);
  275. if (!bh)
  276. goto err;
  277. i = 0;
  278. p = (char *)AFFS_HEAD(bh)->table;
  279. lc = '/';
  280. if (*symname == '/') {
  281. struct affs_sb_info *sbi = AFFS_SB(sb);
  282. while (*symname == '/')
  283. symname++;
  284. spin_lock(&sbi->symlink_lock);
  285. while (sbi->s_volume[i]) /* Cannot overflow */
  286. *p++ = sbi->s_volume[i++];
  287. spin_unlock(&sbi->symlink_lock);
  288. }
  289. while (i < maxlen && (c = *symname++)) {
  290. if (c == '.' && lc == '/' && *symname == '.' && symname[1] == '/') {
  291. *p++ = '/';
  292. i++;
  293. symname += 2;
  294. lc = '/';
  295. } else if (c == '.' && lc == '/' && *symname == '/') {
  296. symname++;
  297. lc = '/';
  298. } else {
  299. *p++ = c;
  300. lc = c;
  301. i++;
  302. }
  303. if (lc == '/')
  304. while (*symname == '/')
  305. symname++;
  306. }
  307. *p = 0;
  308. inode->i_size = i + 1;
  309. mark_buffer_dirty_inode(bh, inode);
  310. affs_brelse(bh);
  311. mark_inode_dirty(inode);
  312. error = affs_add_entry(dir, inode, dentry, ST_SOFTLINK);
  313. if (error)
  314. goto err;
  315. return 0;
  316. err:
  317. clear_nlink(inode);
  318. mark_inode_dirty(inode);
  319. iput(inode);
  320. return error;
  321. }
  322. int
  323. affs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  324. {
  325. struct inode *inode = d_inode(old_dentry);
  326. pr_debug("%s(%lu, %lu, \"%pd\")\n", __func__, inode->i_ino, dir->i_ino,
  327. dentry);
  328. return affs_add_entry(dir, inode, dentry, ST_LINKFILE);
  329. }
  330. static int
  331. affs_rename(struct inode *old_dir, struct dentry *old_dentry,
  332. struct inode *new_dir, struct dentry *new_dentry)
  333. {
  334. struct super_block *sb = old_dir->i_sb;
  335. struct buffer_head *bh = NULL;
  336. int retval;
  337. retval = affs_check_name(new_dentry->d_name.name,
  338. new_dentry->d_name.len,
  339. affs_nofilenametruncate(old_dentry));
  340. if (retval)
  341. return retval;
  342. /* Unlink destination if it already exists */
  343. if (d_really_is_positive(new_dentry)) {
  344. retval = affs_remove_header(new_dentry);
  345. if (retval)
  346. return retval;
  347. }
  348. bh = affs_bread(sb, d_inode(old_dentry)->i_ino);
  349. if (!bh)
  350. return -EIO;
  351. /* Remove header from its parent directory. */
  352. affs_lock_dir(old_dir);
  353. retval = affs_remove_hash(old_dir, bh);
  354. affs_unlock_dir(old_dir);
  355. if (retval)
  356. goto done;
  357. /* And insert it into the new directory with the new name. */
  358. affs_copy_name(AFFS_TAIL(sb, bh)->name, new_dentry);
  359. affs_fix_checksum(sb, bh);
  360. affs_lock_dir(new_dir);
  361. retval = affs_insert_hash(new_dir, bh);
  362. affs_unlock_dir(new_dir);
  363. /* TODO: move it back to old_dir, if error? */
  364. done:
  365. mark_buffer_dirty_inode(bh, retval ? old_dir : new_dir);
  366. affs_brelse(bh);
  367. return retval;
  368. }
  369. static int
  370. affs_xrename(struct inode *old_dir, struct dentry *old_dentry,
  371. struct inode *new_dir, struct dentry *new_dentry)
  372. {
  373. struct super_block *sb = old_dir->i_sb;
  374. struct buffer_head *bh_old = NULL;
  375. struct buffer_head *bh_new = NULL;
  376. int retval;
  377. bh_old = affs_bread(sb, d_inode(old_dentry)->i_ino);
  378. if (!bh_old)
  379. return -EIO;
  380. bh_new = affs_bread(sb, d_inode(new_dentry)->i_ino);
  381. if (!bh_new)
  382. return -EIO;
  383. /* Remove old header from its parent directory. */
  384. affs_lock_dir(old_dir);
  385. retval = affs_remove_hash(old_dir, bh_old);
  386. affs_unlock_dir(old_dir);
  387. if (retval)
  388. goto done;
  389. /* Remove new header from its parent directory. */
  390. affs_lock_dir(new_dir);
  391. retval = affs_remove_hash(new_dir, bh_new);
  392. affs_unlock_dir(new_dir);
  393. if (retval)
  394. goto done;
  395. /* Insert old into the new directory with the new name. */
  396. affs_copy_name(AFFS_TAIL(sb, bh_old)->name, new_dentry);
  397. affs_fix_checksum(sb, bh_old);
  398. affs_lock_dir(new_dir);
  399. retval = affs_insert_hash(new_dir, bh_old);
  400. affs_unlock_dir(new_dir);
  401. /* Insert new into the old directory with the old name. */
  402. affs_copy_name(AFFS_TAIL(sb, bh_new)->name, old_dentry);
  403. affs_fix_checksum(sb, bh_new);
  404. affs_lock_dir(old_dir);
  405. retval = affs_insert_hash(old_dir, bh_new);
  406. affs_unlock_dir(old_dir);
  407. done:
  408. mark_buffer_dirty_inode(bh_old, new_dir);
  409. mark_buffer_dirty_inode(bh_new, old_dir);
  410. affs_brelse(bh_old);
  411. affs_brelse(bh_new);
  412. return retval;
  413. }
  414. int affs_rename2(struct inode *old_dir, struct dentry *old_dentry,
  415. struct inode *new_dir, struct dentry *new_dentry,
  416. unsigned int flags)
  417. {
  418. if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
  419. return -EINVAL;
  420. pr_debug("%s(old=%lu,\"%pd\" to new=%lu,\"%pd\")\n", __func__,
  421. old_dir->i_ino, old_dentry, new_dir->i_ino, new_dentry);
  422. if (flags & RENAME_EXCHANGE)
  423. return affs_xrename(old_dir, old_dentry, new_dir, new_dentry);
  424. return affs_rename(old_dir, old_dentry, new_dir, new_dentry);
  425. }
  426. static struct dentry *affs_get_parent(struct dentry *child)
  427. {
  428. struct inode *parent;
  429. struct buffer_head *bh;
  430. bh = affs_bread(child->d_sb, d_inode(child)->i_ino);
  431. if (!bh)
  432. return ERR_PTR(-EIO);
  433. parent = affs_iget(child->d_sb,
  434. be32_to_cpu(AFFS_TAIL(child->d_sb, bh)->parent));
  435. brelse(bh);
  436. if (IS_ERR(parent))
  437. return ERR_CAST(parent);
  438. return d_obtain_alias(parent);
  439. }
  440. static struct inode *affs_nfs_get_inode(struct super_block *sb, u64 ino,
  441. u32 generation)
  442. {
  443. struct inode *inode;
  444. if (!affs_validblock(sb, ino))
  445. return ERR_PTR(-ESTALE);
  446. inode = affs_iget(sb, ino);
  447. if (IS_ERR(inode))
  448. return ERR_CAST(inode);
  449. return inode;
  450. }
  451. static struct dentry *affs_fh_to_dentry(struct super_block *sb, struct fid *fid,
  452. int fh_len, int fh_type)
  453. {
  454. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  455. affs_nfs_get_inode);
  456. }
  457. static struct dentry *affs_fh_to_parent(struct super_block *sb, struct fid *fid,
  458. int fh_len, int fh_type)
  459. {
  460. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  461. affs_nfs_get_inode);
  462. }
  463. const struct export_operations affs_export_ops = {
  464. .fh_to_dentry = affs_fh_to_dentry,
  465. .fh_to_parent = affs_fh_to_parent,
  466. .get_parent = affs_get_parent,
  467. };
  468. const struct dentry_operations affs_dentry_operations = {
  469. .d_hash = affs_hash_dentry,
  470. .d_compare = affs_compare_dentry,
  471. };
  472. const struct dentry_operations affs_intl_dentry_operations = {
  473. .d_hash = affs_intl_hash_dentry,
  474. .d_compare = affs_intl_compare_dentry,
  475. };