namei.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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, bool notruncate)
  52. {
  53. const u8 *name = qstr->name;
  54. unsigned long hash;
  55. int i;
  56. i = affs_check_name(qstr->name, qstr->len, notruncate);
  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. affs_nofilenametruncate(dentry));
  71. }
  72. static int
  73. affs_intl_hash_dentry(const struct dentry *dentry, struct qstr *qstr)
  74. {
  75. return __affs_hash_dentry(qstr, affs_intl_toupper,
  76. affs_nofilenametruncate(dentry));
  77. }
  78. static inline int __affs_compare_dentry(unsigned int len,
  79. const char *str, const struct qstr *name, toupper_t toupper,
  80. bool notruncate)
  81. {
  82. const u8 *aname = str;
  83. const u8 *bname = name->name;
  84. /*
  85. * 'str' is the name of an already existing dentry, so the name
  86. * must be valid. 'name' must be validated first.
  87. */
  88. if (affs_check_name(name->name, name->len, notruncate))
  89. return 1;
  90. /*
  91. * If the names are longer than the allowed 30 chars,
  92. * the excess is ignored, so their length may differ.
  93. */
  94. if (len >= 30) {
  95. if (name->len < 30)
  96. return 1;
  97. len = 30;
  98. } else if (len != name->len)
  99. return 1;
  100. for (; len > 0; len--)
  101. if (toupper(*aname++) != toupper(*bname++))
  102. return 1;
  103. return 0;
  104. }
  105. static int
  106. affs_compare_dentry(const struct dentry *parent, const struct dentry *dentry,
  107. unsigned int len, const char *str, const struct qstr *name)
  108. {
  109. return __affs_compare_dentry(len, str, name, affs_toupper,
  110. affs_nofilenametruncate(parent));
  111. }
  112. static int
  113. affs_intl_compare_dentry(const struct dentry *parent, const struct dentry *dentry,
  114. unsigned int len, const char *str, const struct qstr *name)
  115. {
  116. return __affs_compare_dentry(len, str, name, affs_intl_toupper,
  117. affs_nofilenametruncate(parent));
  118. }
  119. /*
  120. * NOTE! unlike strncmp, affs_match returns 1 for success, 0 for failure.
  121. */
  122. static inline int
  123. affs_match(struct dentry *dentry, const u8 *name2, toupper_t toupper)
  124. {
  125. const u8 *name = dentry->d_name.name;
  126. int len = dentry->d_name.len;
  127. if (len >= 30) {
  128. if (*name2 < 30)
  129. return 0;
  130. len = 30;
  131. } else if (len != *name2)
  132. return 0;
  133. for (name2++; len > 0; len--)
  134. if (toupper(*name++) != toupper(*name2++))
  135. return 0;
  136. return 1;
  137. }
  138. int
  139. affs_hash_name(struct super_block *sb, const u8 *name, unsigned int len)
  140. {
  141. toupper_t toupper = affs_get_toupper(sb);
  142. int hash;
  143. hash = len = min(len, 30u);
  144. for (; len > 0; len--)
  145. hash = (hash * 13 + toupper(*name++)) & 0x7ff;
  146. return hash % AFFS_SB(sb)->s_hashsize;
  147. }
  148. static struct buffer_head *
  149. affs_find_entry(struct inode *dir, struct dentry *dentry)
  150. {
  151. struct super_block *sb = dir->i_sb;
  152. struct buffer_head *bh;
  153. toupper_t toupper = affs_get_toupper(sb);
  154. u32 key;
  155. pr_debug("%s(\"%.*s\")\n",
  156. __func__, (int)dentry->d_name.len, dentry->d_name.name);
  157. bh = affs_bread(sb, dir->i_ino);
  158. if (!bh)
  159. return ERR_PTR(-EIO);
  160. key = be32_to_cpu(AFFS_HEAD(bh)->table[affs_hash_name(sb, dentry->d_name.name, dentry->d_name.len)]);
  161. for (;;) {
  162. affs_brelse(bh);
  163. if (key == 0)
  164. return NULL;
  165. bh = affs_bread(sb, key);
  166. if (!bh)
  167. return ERR_PTR(-EIO);
  168. if (affs_match(dentry, AFFS_TAIL(sb, bh)->name, toupper))
  169. return bh;
  170. key = be32_to_cpu(AFFS_TAIL(sb, bh)->hash_chain);
  171. }
  172. }
  173. struct dentry *
  174. affs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
  175. {
  176. struct super_block *sb = dir->i_sb;
  177. struct buffer_head *bh;
  178. struct inode *inode = NULL;
  179. pr_debug("%s(\"%.*s\")\n",
  180. __func__, (int)dentry->d_name.len, dentry->d_name.name);
  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=%d, %lu \"%.*s\")\n",
  208. __func__, (u32)dir->i_ino, dentry->d_inode->i_ino,
  209. (int)dentry->d_name.len, dentry->d_name.name);
  210. return affs_remove_header(dentry);
  211. }
  212. int
  213. affs_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool excl)
  214. {
  215. struct super_block *sb = dir->i_sb;
  216. struct inode *inode;
  217. int error;
  218. pr_debug("%s(%lu,\"%.*s\",0%ho)\n",
  219. __func__, dir->i_ino, (int)dentry->d_name.len,
  220. dentry->d_name.name,mode);
  221. inode = affs_new_inode(dir);
  222. if (!inode)
  223. return -ENOSPC;
  224. inode->i_mode = mode;
  225. mode_to_prot(inode);
  226. mark_inode_dirty(inode);
  227. inode->i_op = &affs_file_inode_operations;
  228. inode->i_fop = &affs_file_operations;
  229. inode->i_mapping->a_ops = (AFFS_SB(sb)->s_flags & SF_OFS) ? &affs_aops_ofs : &affs_aops;
  230. error = affs_add_entry(dir, inode, dentry, ST_FILE);
  231. if (error) {
  232. clear_nlink(inode);
  233. iput(inode);
  234. return error;
  235. }
  236. return 0;
  237. }
  238. int
  239. affs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  240. {
  241. struct inode *inode;
  242. int error;
  243. pr_debug("%s(%lu,\"%.*s\",0%ho)\n",
  244. __func__, dir->i_ino, (int)dentry->d_name.len,
  245. dentry->d_name.name, mode);
  246. inode = affs_new_inode(dir);
  247. if (!inode)
  248. return -ENOSPC;
  249. inode->i_mode = S_IFDIR | mode;
  250. mode_to_prot(inode);
  251. inode->i_op = &affs_dir_inode_operations;
  252. inode->i_fop = &affs_dir_operations;
  253. error = affs_add_entry(dir, inode, dentry, ST_USERDIR);
  254. if (error) {
  255. clear_nlink(inode);
  256. mark_inode_dirty(inode);
  257. iput(inode);
  258. return error;
  259. }
  260. return 0;
  261. }
  262. int
  263. affs_rmdir(struct inode *dir, struct dentry *dentry)
  264. {
  265. pr_debug("%s(dir=%u, %lu \"%.*s\")\n",
  266. __func__, (u32)dir->i_ino, dentry->d_inode->i_ino,
  267. (int)dentry->d_name.len, dentry->d_name.name);
  268. return affs_remove_header(dentry);
  269. }
  270. int
  271. affs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  272. {
  273. struct super_block *sb = dir->i_sb;
  274. struct buffer_head *bh;
  275. struct inode *inode;
  276. char *p;
  277. int i, maxlen, error;
  278. char c, lc;
  279. pr_debug("%s(%lu,\"%.*s\" -> \"%s\")\n",
  280. __func__, dir->i_ino, (int)dentry->d_name.len,
  281. 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. 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 = old_dentry->d_inode;
  342. pr_debug("%s(%u, %u, \"%.*s\")\n",
  343. __func__, (u32)inode->i_ino, (u32)dir->i_ino,
  344. (int)dentry->d_name.len,dentry->d_name.name);
  345. return affs_add_entry(dir, inode, dentry, ST_LINKFILE);
  346. }
  347. int
  348. affs_rename(struct inode *old_dir, struct dentry *old_dentry,
  349. struct inode *new_dir, struct dentry *new_dentry)
  350. {
  351. struct super_block *sb = old_dir->i_sb;
  352. struct buffer_head *bh = NULL;
  353. int retval;
  354. pr_debug("%s(old=%u,\"%*s\" to new=%u,\"%*s\")\n",
  355. __func__, (u32)old_dir->i_ino, (int)old_dentry->d_name.len,
  356. old_dentry->d_name.name, (u32)new_dir->i_ino,
  357. (int)new_dentry->d_name.len, new_dentry->d_name.name);
  358. retval = affs_check_name(new_dentry->d_name.name,
  359. new_dentry->d_name.len,
  360. affs_nofilenametruncate(old_dentry));
  361. if (retval)
  362. return retval;
  363. /* Unlink destination if it already exists */
  364. if (new_dentry->d_inode) {
  365. retval = affs_remove_header(new_dentry);
  366. if (retval)
  367. return retval;
  368. }
  369. bh = affs_bread(sb, old_dentry->d_inode->i_ino);
  370. if (!bh)
  371. return -EIO;
  372. /* Remove header from its parent directory. */
  373. affs_lock_dir(old_dir);
  374. retval = affs_remove_hash(old_dir, bh);
  375. affs_unlock_dir(old_dir);
  376. if (retval)
  377. goto done;
  378. /* And insert it into the new directory with the new name. */
  379. affs_copy_name(AFFS_TAIL(sb, bh)->name, new_dentry);
  380. affs_fix_checksum(sb, bh);
  381. affs_lock_dir(new_dir);
  382. retval = affs_insert_hash(new_dir, bh);
  383. affs_unlock_dir(new_dir);
  384. /* TODO: move it back to old_dir, if error? */
  385. done:
  386. mark_buffer_dirty_inode(bh, retval ? old_dir : new_dir);
  387. affs_brelse(bh);
  388. return retval;
  389. }