PageRenderTime 62ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/affs/namei.c

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