namei.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. if (IS_ERR(bh)) {
  184. affs_unlock_dir(dir);
  185. return ERR_CAST(bh);
  186. }
  187. if (bh) {
  188. u32 ino = bh->b_blocknr;
  189. /* store the real header ino in d_fsdata for faster lookups */
  190. dentry->d_fsdata = (void *)(long)ino;
  191. switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) {
  192. //link to dirs disabled
  193. //case ST_LINKDIR:
  194. case ST_LINKFILE:
  195. ino = be32_to_cpu(AFFS_TAIL(sb, bh)->original);
  196. }
  197. affs_brelse(bh);
  198. inode = affs_iget(sb, ino);
  199. if (IS_ERR(inode)) {
  200. affs_unlock_dir(dir);
  201. return ERR_CAST(inode);
  202. }
  203. }
  204. d_add(dentry, inode);
  205. affs_unlock_dir(dir);
  206. return NULL;
  207. }
  208. int
  209. affs_unlink(struct inode *dir, struct dentry *dentry)
  210. {
  211. pr_debug("%s(dir=%lu, %lu \"%pd\")\n", __func__, dir->i_ino,
  212. d_inode(dentry)->i_ino, dentry);
  213. return affs_remove_header(dentry);
  214. }
  215. int
  216. affs_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool excl)
  217. {
  218. struct super_block *sb = dir->i_sb;
  219. struct inode *inode;
  220. int error;
  221. pr_debug("%s(%lu,\"%pd\",0%ho)\n",
  222. __func__, dir->i_ino, dentry, 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_test_opt(AFFS_SB(sb)->s_flags, SF_OFS) ?
  232. &affs_aops_ofs : &affs_aops;
  233. error = affs_add_entry(dir, inode, dentry, ST_FILE);
  234. if (error) {
  235. clear_nlink(inode);
  236. iput(inode);
  237. return error;
  238. }
  239. return 0;
  240. }
  241. int
  242. affs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  243. {
  244. struct inode *inode;
  245. int error;
  246. pr_debug("%s(%lu,\"%pd\",0%ho)\n",
  247. __func__, dir->i_ino, dentry, mode);
  248. inode = affs_new_inode(dir);
  249. if (!inode)
  250. return -ENOSPC;
  251. inode->i_mode = S_IFDIR | mode;
  252. mode_to_prot(inode);
  253. inode->i_op = &affs_dir_inode_operations;
  254. inode->i_fop = &affs_dir_operations;
  255. error = affs_add_entry(dir, inode, dentry, ST_USERDIR);
  256. if (error) {
  257. clear_nlink(inode);
  258. mark_inode_dirty(inode);
  259. iput(inode);
  260. return error;
  261. }
  262. return 0;
  263. }
  264. int
  265. affs_rmdir(struct inode *dir, struct dentry *dentry)
  266. {
  267. pr_debug("%s(dir=%lu, %lu \"%pd\")\n", __func__, dir->i_ino,
  268. d_inode(dentry)->i_ino, dentry);
  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("%s(%lu,\"%pd\" -> \"%s\")\n",
  281. __func__, dir->i_ino, dentry, 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. clear_nlink(inode);
  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 = d_inode(old_dentry);
  342. pr_debug("%s(%lu, %lu, \"%pd\")\n", __func__, inode->i_ino, dir->i_ino,
  343. dentry);
  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("%s(old=%lu,\"%pd\" to new=%lu,\"%pd\")\n", __func__,
  354. old_dir->i_ino, old_dentry, new_dir->i_ino, new_dentry);
  355. retval = affs_check_name(new_dentry->d_name.name,
  356. new_dentry->d_name.len,
  357. affs_nofilenametruncate(old_dentry));
  358. if (retval)
  359. return retval;
  360. /* Unlink destination if it already exists */
  361. if (d_really_is_positive(new_dentry)) {
  362. retval = affs_remove_header(new_dentry);
  363. if (retval)
  364. return retval;
  365. }
  366. bh = affs_bread(sb, d_inode(old_dentry)->i_ino);
  367. if (!bh)
  368. return -EIO;
  369. /* Remove header from its parent directory. */
  370. affs_lock_dir(old_dir);
  371. retval = affs_remove_hash(old_dir, bh);
  372. affs_unlock_dir(old_dir);
  373. if (retval)
  374. goto done;
  375. /* And insert it into the new directory with the new name. */
  376. affs_copy_name(AFFS_TAIL(sb, bh)->name, new_dentry);
  377. affs_fix_checksum(sb, bh);
  378. affs_lock_dir(new_dir);
  379. retval = affs_insert_hash(new_dir, bh);
  380. affs_unlock_dir(new_dir);
  381. /* TODO: move it back to old_dir, if error? */
  382. done:
  383. mark_buffer_dirty_inode(bh, retval ? old_dir : new_dir);
  384. affs_brelse(bh);
  385. return retval;
  386. }