PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/affs/namei.c

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