namei.c 10 KB

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