namei.c 11 KB

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