original development tree for Linux kernel GTP module; now long in mainline.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

448 lines
10 KiB

  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_SB(sb)->s_flags & SF_INTL ? affs_intl_toupper : affs_toupper;
  46. }
  47. /*
  48. * Note: the dentry argument is the parent dentry.
  49. */
  50. static inline int
  51. __affs_hash_dentry(struct qstr *qstr, toupper_t toupper)
  52. {
  53. const u8 *name = qstr->name;
  54. unsigned long hash;
  55. int i;
  56. i = affs_check_name(qstr->name, qstr->len);
  57. if (i)
  58. return i;
  59. hash = init_name_hash();
  60. i = min(qstr->len, 30u);
  61. for (; i > 0; name++, i--)
  62. hash = partial_name_hash(toupper(*name), hash);
  63. qstr->hash = end_name_hash(hash);
  64. return 0;
  65. }
  66. static int
  67. affs_hash_dentry(const struct dentry *dentry, struct qstr *qstr)
  68. {
  69. return __affs_hash_dentry(qstr, affs_toupper);
  70. }
  71. static int
  72. affs_intl_hash_dentry(const struct dentry *dentry, struct qstr *qstr)
  73. {
  74. return __affs_hash_dentry(qstr, affs_intl_toupper);
  75. }
  76. static inline int __affs_compare_dentry(unsigned int len,
  77. const char *str, const struct qstr *name, toupper_t toupper)
  78. {
  79. const u8 *aname = str;
  80. const u8 *bname = name->name;
  81. /*
  82. * 'str' is the name of an already existing dentry, so the name
  83. * must be valid. 'name' must be validated first.
  84. */
  85. if (affs_check_name(name->name, name->len))
  86. return 1;
  87. /*
  88. * If the names are longer than the allowed 30 chars,
  89. * the excess is ignored, so their length may differ.
  90. */
  91. if (len >= 30) {
  92. if (name->len < 30)
  93. return 1;
  94. len = 30;
  95. } else if (len != name->len)
  96. return 1;
  97. for (; len > 0; len--)
  98. if (toupper(*aname++) != toupper(*bname++))
  99. return 1;
  100. return 0;
  101. }
  102. static int
  103. affs_compare_dentry(const struct dentry *parent, const struct dentry *dentry,
  104. unsigned int len, const char *str, const struct qstr *name)
  105. {
  106. return __affs_compare_dentry(len, str, name, affs_toupper);
  107. }
  108. static int
  109. affs_intl_compare_dentry(const struct dentry *parent, const struct dentry *dentry,
  110. unsigned int len, const char *str, const struct qstr *name)
  111. {
  112. return __affs_compare_dentry(len, str, name, affs_intl_toupper);
  113. }
  114. /*
  115. * NOTE! unlike strncmp, affs_match returns 1 for success, 0 for failure.
  116. */
  117. static inline int
  118. affs_match(struct dentry *dentry, const u8 *name2, toupper_t toupper)
  119. {
  120. const u8 *name = dentry->d_name.name;
  121. int len = dentry->d_name.len;
  122. if (len >= 30) {
  123. if (*name2 < 30)
  124. return 0;
  125. len = 30;
  126. } else if (len != *name2)
  127. return 0;
  128. for (name2++; len > 0; len--)
  129. if (toupper(*name++) != toupper(*name2++))
  130. return 0;
  131. return 1;
  132. }
  133. int
  134. affs_hash_name(struct super_block *sb, const u8 *name, unsigned int len)
  135. {
  136. toupper_t toupper = affs_get_toupper(sb);
  137. int hash;
  138. hash = len = min(len, 30u);
  139. for (; len > 0; len--)
  140. hash = (hash * 13 + toupper(*name++)) & 0x7ff;
  141. return hash % AFFS_SB(sb)->s_hashsize;
  142. }
  143. static struct buffer_head *
  144. affs_find_entry(struct inode *dir, struct dentry *dentry)
  145. {
  146. struct super_block *sb = dir->i_sb;
  147. struct buffer_head *bh;
  148. toupper_t toupper = affs_get_toupper(sb);
  149. u32 key;
  150. pr_debug("AFFS: find_entry(\"%.*s\")\n", (int)dentry->d_name.len, dentry->d_name.name);
  151. bh = affs_bread(sb, dir->i_ino);
  152. if (!bh)
  153. return ERR_PTR(-EIO);
  154. key = be32_to_cpu(AFFS_HEAD(bh)->table[affs_hash_name(sb, dentry->d_name.name, dentry->d_name.len)]);
  155. for (;;) {
  156. affs_brelse(bh);
  157. if (key == 0)
  158. return NULL;
  159. bh = affs_bread(sb, key);
  160. if (!bh)
  161. return ERR_PTR(-EIO);
  162. if (affs_match(dentry, AFFS_TAIL(sb, bh)->name, toupper))
  163. return bh;
  164. key = be32_to_cpu(AFFS_TAIL(sb, bh)->hash_chain);
  165. }
  166. }
  167. struct dentry *
  168. affs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
  169. {
  170. struct super_block *sb = dir->i_sb;
  171. struct buffer_head *bh;
  172. struct inode *inode = NULL;
  173. pr_debug("AFFS: lookup(\"%.*s\")\n",(int)dentry->d_name.len,dentry->d_name.name);
  174. affs_lock_dir(dir);
  175. bh = affs_find_entry(dir, dentry);
  176. affs_unlock_dir(dir);
  177. if (IS_ERR(bh))
  178. return ERR_CAST(bh);
  179. if (bh) {
  180. u32 ino = bh->b_blocknr;
  181. /* store the real header ino in d_fsdata for faster lookups */
  182. dentry->d_fsdata = (void *)(long)ino;
  183. switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) {
  184. //link to dirs disabled
  185. //case ST_LINKDIR:
  186. case ST_LINKFILE:
  187. ino = be32_to_cpu(AFFS_TAIL(sb, bh)->original);
  188. }
  189. affs_brelse(bh);
  190. inode = affs_iget(sb, ino);
  191. if (IS_ERR(inode))
  192. return ERR_CAST(inode);
  193. }
  194. d_add(dentry, inode);
  195. return NULL;
  196. }
  197. int
  198. affs_unlink(struct inode *dir, struct dentry *dentry)
  199. {
  200. pr_debug("AFFS: unlink(dir=%d, %lu \"%.*s\")\n", (u32)dir->i_ino,
  201. dentry->d_inode->i_ino,
  202. (int)dentry->d_name.len, dentry->d_name.name);
  203. return affs_remove_header(dentry);
  204. }
  205. int
  206. affs_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool excl)
  207. {
  208. struct super_block *sb = dir->i_sb;
  209. struct inode *inode;
  210. int error;
  211. pr_debug("AFFS: create(%lu,\"%.*s\",0%ho)\n",dir->i_ino,(int)dentry->d_name.len,
  212. dentry->d_name.name,mode);
  213. inode = affs_new_inode(dir);
  214. if (!inode)
  215. return -ENOSPC;
  216. inode->i_mode = mode;
  217. mode_to_prot(inode);
  218. mark_inode_dirty(inode);
  219. inode->i_op = &affs_file_inode_operations;
  220. inode->i_fop = &affs_file_operations;
  221. inode->i_mapping->a_ops = (AFFS_SB(sb)->s_flags & SF_OFS) ? &affs_aops_ofs : &affs_aops;
  222. error = affs_add_entry(dir, inode, dentry, ST_FILE);
  223. if (error) {
  224. clear_nlink(inode);
  225. iput(inode);
  226. return error;
  227. }
  228. return 0;
  229. }
  230. int
  231. affs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  232. {
  233. struct inode *inode;
  234. int error;
  235. pr_debug("AFFS: mkdir(%lu,\"%.*s\",0%ho)\n",dir->i_ino,
  236. (int)dentry->d_name.len,dentry->d_name.name,mode);
  237. inode = affs_new_inode(dir);
  238. if (!inode)
  239. return -ENOSPC;
  240. inode->i_mode = S_IFDIR | mode;
  241. mode_to_prot(inode);
  242. inode->i_op = &affs_dir_inode_operations;
  243. inode->i_fop = &affs_dir_operations;
  244. error = affs_add_entry(dir, inode, dentry, ST_USERDIR);
  245. if (error) {
  246. clear_nlink(inode);
  247. mark_inode_dirty(inode);
  248. iput(inode);
  249. return error;
  250. }
  251. return 0;
  252. }
  253. int
  254. affs_rmdir(struct inode *dir, struct dentry *dentry)
  255. {
  256. pr_debug("AFFS: rmdir(dir=%u, %lu \"%.*s\")\n", (u32)dir->i_ino,
  257. dentry->d_inode->i_ino,
  258. (int)dentry->d_name.len, dentry->d_name.name);
  259. return affs_remove_header(dentry);
  260. }
  261. int
  262. affs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  263. {
  264. struct super_block *sb = dir->i_sb;
  265. struct buffer_head *bh;
  266. struct inode *inode;
  267. char *p;
  268. int i, maxlen, error;
  269. char c, lc;
  270. pr_debug("AFFS: symlink(%lu,\"%.*s\" -> \"%s\")\n",dir->i_ino,
  271. (int)dentry->d_name.len,dentry->d_name.name,symname);
  272. maxlen = AFFS_SB(sb)->s_hashsize * sizeof(u32) - 1;
  273. inode = affs_new_inode(dir);
  274. if (!inode)
  275. return -ENOSPC;
  276. inode->i_op = &affs_symlink_inode_operations;
  277. inode->i_data.a_ops = &affs_symlink_aops;
  278. inode->i_mode = S_IFLNK | 0777;
  279. mode_to_prot(inode);
  280. error = -EIO;
  281. bh = affs_bread(sb, inode->i_ino);
  282. if (!bh)
  283. goto err;
  284. i = 0;
  285. p = (char *)AFFS_HEAD(bh)->table;
  286. lc = '/';
  287. if (*symname == '/') {
  288. struct affs_sb_info *sbi = AFFS_SB(sb);
  289. while (*symname == '/')
  290. symname++;
  291. spin_lock(&sbi->symlink_lock);
  292. while (sbi->s_volume[i]) /* Cannot overflow */
  293. *p++ = sbi->s_volume[i++];
  294. spin_unlock(&sbi->symlink_lock);
  295. }
  296. while (i < maxlen && (c = *symname++)) {
  297. if (c == '.' && lc == '/' && *symname == '.' && symname[1] == '/') {
  298. *p++ = '/';
  299. i++;
  300. symname += 2;
  301. lc = '/';
  302. } else if (c == '.' && lc == '/' && *symname == '/') {
  303. symname++;
  304. lc = '/';
  305. } else {
  306. *p++ = c;
  307. lc = c;
  308. i++;
  309. }
  310. if (lc == '/')
  311. while (*symname == '/')
  312. symname++;
  313. }
  314. *p = 0;
  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 = old_dentry->d_inode;
  332. pr_debug("AFFS: link(%u, %u, \"%.*s\")\n", (u32)inode->i_ino, (u32)dir->i_ino,
  333. (int)dentry->d_name.len,dentry->d_name.name);
  334. return affs_add_entry(dir, inode, dentry, ST_LINKFILE);
  335. }
  336. 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. pr_debug("AFFS: rename(old=%u,\"%*s\" to new=%u,\"%*s\")\n",
  344. (u32)old_dir->i_ino, (int)old_dentry->d_name.len, old_dentry->d_name.name,
  345. (u32)new_dir->i_ino, (int)new_dentry->d_name.len, new_dentry->d_name.name);
  346. retval = affs_check_name(new_dentry->d_name.name,new_dentry->d_name.len);
  347. if (retval)
  348. return retval;
  349. /* Unlink destination if it already exists */
  350. if (new_dentry->d_inode) {
  351. retval = affs_remove_header(new_dentry);
  352. if (retval)
  353. return retval;
  354. }
  355. bh = affs_bread(sb, old_dentry->d_inode->i_ino);
  356. if (!bh)
  357. return -EIO;
  358. /* Remove header from its parent directory. */
  359. affs_lock_dir(old_dir);
  360. retval = affs_remove_hash(old_dir, bh);
  361. affs_unlock_dir(old_dir);
  362. if (retval)
  363. goto done;
  364. /* And insert it into the new directory with the new name. */
  365. affs_copy_name(AFFS_TAIL(sb, bh)->name, new_dentry);
  366. affs_fix_checksum(sb, bh);
  367. affs_lock_dir(new_dir);
  368. retval = affs_insert_hash(new_dir, bh);
  369. affs_unlock_dir(new_dir);
  370. /* TODO: move it back to old_dir, if error? */
  371. done:
  372. mark_buffer_dirty_inode(bh, retval ? old_dir : new_dir);
  373. affs_brelse(bh);
  374. return retval;
  375. }