namei.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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. #include <linux/exportfs.h>
  12. typedef int (*toupper_t)(int);
  13. /* Simple toupper() for DOS\1 */
  14. static int
  15. affs_toupper(int ch)
  16. {
  17. return ch >= 'a' && ch <= 'z' ? ch -= ('a' - 'A') : ch;
  18. }
  19. /* International toupper() for DOS\3 ("international") */
  20. static int
  21. affs_intl_toupper(int ch)
  22. {
  23. return (ch >= 'a' && ch <= 'z') || (ch >= 0xE0
  24. && ch <= 0xFE && ch != 0xF7) ?
  25. ch - ('a' - 'A') : ch;
  26. }
  27. static inline toupper_t
  28. affs_get_toupper(struct super_block *sb)
  29. {
  30. return affs_test_opt(AFFS_SB(sb)->s_flags, SF_INTL) ?
  31. affs_intl_toupper : affs_toupper;
  32. }
  33. /*
  34. * Note: the dentry argument is the parent dentry.
  35. */
  36. static inline int
  37. __affs_hash_dentry(const struct dentry *dentry, struct qstr *qstr, toupper_t toupper, bool notruncate)
  38. {
  39. const u8 *name = qstr->name;
  40. unsigned long hash;
  41. int retval;
  42. u32 len;
  43. retval = affs_check_name(qstr->name, qstr->len, notruncate);
  44. if (retval)
  45. return retval;
  46. hash = init_name_hash(dentry);
  47. len = min(qstr->len, AFFSNAMEMAX);
  48. for (; len > 0; name++, len--)
  49. hash = partial_name_hash(toupper(*name), hash);
  50. qstr->hash = end_name_hash(hash);
  51. return 0;
  52. }
  53. static int
  54. affs_hash_dentry(const struct dentry *dentry, struct qstr *qstr)
  55. {
  56. return __affs_hash_dentry(dentry, qstr, affs_toupper,
  57. affs_nofilenametruncate(dentry));
  58. }
  59. static int
  60. affs_intl_hash_dentry(const struct dentry *dentry, struct qstr *qstr)
  61. {
  62. return __affs_hash_dentry(dentry, qstr, affs_intl_toupper,
  63. affs_nofilenametruncate(dentry));
  64. }
  65. static inline int __affs_compare_dentry(unsigned int len,
  66. const char *str, const struct qstr *name, toupper_t toupper,
  67. bool notruncate)
  68. {
  69. const u8 *aname = str;
  70. const u8 *bname = name->name;
  71. /*
  72. * 'str' is the name of an already existing dentry, so the name
  73. * must be valid. 'name' must be validated first.
  74. */
  75. if (affs_check_name(name->name, name->len, notruncate))
  76. return 1;
  77. /*
  78. * If the names are longer than the allowed 30 chars,
  79. * the excess is ignored, so their length may differ.
  80. */
  81. if (len >= AFFSNAMEMAX) {
  82. if (name->len < AFFSNAMEMAX)
  83. return 1;
  84. len = AFFSNAMEMAX;
  85. } else if (len != name->len)
  86. return 1;
  87. for (; len > 0; len--)
  88. if (toupper(*aname++) != toupper(*bname++))
  89. return 1;
  90. return 0;
  91. }
  92. static int
  93. affs_compare_dentry(const struct dentry *dentry,
  94. unsigned int len, const char *str, const struct qstr *name)
  95. {
  96. return __affs_compare_dentry(len, str, name, affs_toupper,
  97. affs_nofilenametruncate(dentry));
  98. }
  99. static int
  100. affs_intl_compare_dentry(const struct dentry *dentry,
  101. unsigned int len, const char *str, const struct qstr *name)
  102. {
  103. return __affs_compare_dentry(len, str, name, affs_intl_toupper,
  104. affs_nofilenametruncate(dentry));
  105. }
  106. /*
  107. * NOTE! unlike strncmp, affs_match returns 1 for success, 0 for failure.
  108. */
  109. static inline int
  110. affs_match(struct dentry *dentry, const u8 *name2, toupper_t toupper)
  111. {
  112. const u8 *name = dentry->d_name.name;
  113. int len = dentry->d_name.len;
  114. if (len >= AFFSNAMEMAX) {
  115. if (*name2 < AFFSNAMEMAX)
  116. return 0;
  117. len = AFFSNAMEMAX;
  118. } else if (len != *name2)
  119. return 0;
  120. for (name2++; len > 0; len--)
  121. if (toupper(*name++) != toupper(*name2++))
  122. return 0;
  123. return 1;
  124. }
  125. int
  126. affs_hash_name(struct super_block *sb, const u8 *name, unsigned int len)
  127. {
  128. toupper_t toupper = affs_get_toupper(sb);
  129. u32 hash;
  130. hash = len = min(len, AFFSNAMEMAX);
  131. for (; len > 0; len--)
  132. hash = (hash * 13 + toupper(*name++)) & 0x7ff;
  133. return hash % AFFS_SB(sb)->s_hashsize;
  134. }
  135. static struct buffer_head *
  136. affs_find_entry(struct inode *dir, struct dentry *dentry)
  137. {
  138. struct super_block *sb = dir->i_sb;
  139. struct buffer_head *bh;
  140. toupper_t toupper = affs_get_toupper(sb);
  141. u32 key;
  142. pr_debug("%s(\"%pd\")\n", __func__, dentry);
  143. bh = affs_bread(sb, dir->i_ino);
  144. if (!bh)
  145. return ERR_PTR(-EIO);
  146. key = be32_to_cpu(AFFS_HEAD(bh)->table[affs_hash_name(sb, dentry->d_name.name, dentry->d_name.len)]);
  147. for (;;) {
  148. affs_brelse(bh);
  149. if (key == 0)
  150. return NULL;
  151. bh = affs_bread(sb, key);
  152. if (!bh)
  153. return ERR_PTR(-EIO);
  154. if (affs_match(dentry, AFFS_TAIL(sb, bh)->name, toupper))
  155. return bh;
  156. key = be32_to_cpu(AFFS_TAIL(sb, bh)->hash_chain);
  157. }
  158. }
  159. struct dentry *
  160. affs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
  161. {
  162. struct super_block *sb = dir->i_sb;
  163. struct buffer_head *bh;
  164. struct inode *inode = NULL;
  165. pr_debug("%s(\"%pd\")\n", __func__, dentry);
  166. affs_lock_dir(dir);
  167. bh = affs_find_entry(dir, dentry);
  168. affs_unlock_dir(dir);
  169. if (IS_ERR(bh))
  170. return ERR_CAST(bh);
  171. if (bh) {
  172. u32 ino = bh->b_blocknr;
  173. /* store the real header ino in d_fsdata for faster lookups */
  174. dentry->d_fsdata = (void *)(long)ino;
  175. switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) {
  176. //link to dirs disabled
  177. //case ST_LINKDIR:
  178. case ST_LINKFILE:
  179. ino = be32_to_cpu(AFFS_TAIL(sb, bh)->original);
  180. }
  181. affs_brelse(bh);
  182. inode = affs_iget(sb, ino);
  183. if (IS_ERR(inode))
  184. return ERR_CAST(inode);
  185. }
  186. d_add(dentry, inode);
  187. return NULL;
  188. }
  189. int
  190. affs_unlink(struct inode *dir, struct dentry *dentry)
  191. {
  192. pr_debug("%s(dir=%lu, %lu \"%pd\")\n", __func__, dir->i_ino,
  193. d_inode(dentry)->i_ino, dentry);
  194. return affs_remove_header(dentry);
  195. }
  196. int
  197. affs_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool excl)
  198. {
  199. struct super_block *sb = dir->i_sb;
  200. struct inode *inode;
  201. int error;
  202. pr_debug("%s(%lu,\"%pd\",0%ho)\n",
  203. __func__, dir->i_ino, dentry, mode);
  204. inode = affs_new_inode(dir);
  205. if (!inode)
  206. return -ENOSPC;
  207. inode->i_mode = mode;
  208. affs_mode_to_prot(inode);
  209. mark_inode_dirty(inode);
  210. inode->i_op = &affs_file_inode_operations;
  211. inode->i_fop = &affs_file_operations;
  212. inode->i_mapping->a_ops = affs_test_opt(AFFS_SB(sb)->s_flags, SF_OFS) ?
  213. &affs_aops_ofs : &affs_aops;
  214. error = affs_add_entry(dir, inode, dentry, ST_FILE);
  215. if (error) {
  216. clear_nlink(inode);
  217. iput(inode);
  218. return error;
  219. }
  220. return 0;
  221. }
  222. int
  223. affs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  224. {
  225. struct inode *inode;
  226. int error;
  227. pr_debug("%s(%lu,\"%pd\",0%ho)\n",
  228. __func__, dir->i_ino, dentry, mode);
  229. inode = affs_new_inode(dir);
  230. if (!inode)
  231. return -ENOSPC;
  232. inode->i_mode = S_IFDIR | mode;
  233. affs_mode_to_prot(inode);
  234. inode->i_op = &affs_dir_inode_operations;
  235. inode->i_fop = &affs_dir_operations;
  236. error = affs_add_entry(dir, inode, dentry, ST_USERDIR);
  237. if (error) {
  238. clear_nlink(inode);
  239. mark_inode_dirty(inode);
  240. iput(inode);
  241. return error;
  242. }
  243. return 0;
  244. }
  245. int
  246. affs_rmdir(struct inode *dir, struct dentry *dentry)
  247. {
  248. pr_debug("%s(dir=%lu, %lu \"%pd\")\n", __func__, dir->i_ino,
  249. d_inode(dentry)->i_ino, dentry);
  250. return affs_remove_header(dentry);
  251. }
  252. int
  253. affs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  254. {
  255. struct super_block *sb = dir->i_sb;
  256. struct buffer_head *bh;
  257. struct inode *inode;
  258. char *p;
  259. int i, maxlen, error;
  260. char c, lc;
  261. pr_debug("%s(%lu,\"%pd\" -> \"%s\")\n",
  262. __func__, dir->i_ino, dentry, symname);
  263. maxlen = AFFS_SB(sb)->s_hashsize * sizeof(u32) - 1;
  264. inode = affs_new_inode(dir);
  265. if (!inode)
  266. return -ENOSPC;
  267. inode->i_op = &affs_symlink_inode_operations;
  268. inode_nohighmem(inode);
  269. inode->i_data.a_ops = &affs_symlink_aops;
  270. inode->i_mode = S_IFLNK | 0777;
  271. affs_mode_to_prot(inode);
  272. error = -EIO;
  273. bh = affs_bread(sb, inode->i_ino);
  274. if (!bh)
  275. goto err;
  276. i = 0;
  277. p = (char *)AFFS_HEAD(bh)->table;
  278. lc = '/';
  279. if (*symname == '/') {
  280. struct affs_sb_info *sbi = AFFS_SB(sb);
  281. while (*symname == '/')
  282. symname++;
  283. spin_lock(&sbi->symlink_lock);
  284. while (sbi->s_volume[i]) /* Cannot overflow */
  285. *p++ = sbi->s_volume[i++];
  286. spin_unlock(&sbi->symlink_lock);
  287. }
  288. while (i < maxlen && (c = *symname++)) {
  289. if (c == '.' && lc == '/' && *symname == '.' && symname[1] == '/') {
  290. *p++ = '/';
  291. i++;
  292. symname += 2;
  293. lc = '/';
  294. } else if (c == '.' && lc == '/' && *symname == '/') {
  295. symname++;
  296. lc = '/';
  297. } else {
  298. *p++ = c;
  299. lc = c;
  300. i++;
  301. }
  302. if (lc == '/')
  303. while (*symname == '/')
  304. symname++;
  305. }
  306. *p = 0;
  307. inode->i_size = i + 1;
  308. mark_buffer_dirty_inode(bh, inode);
  309. affs_brelse(bh);
  310. mark_inode_dirty(inode);
  311. error = affs_add_entry(dir, inode, dentry, ST_SOFTLINK);
  312. if (error)
  313. goto err;
  314. return 0;
  315. err:
  316. clear_nlink(inode);
  317. mark_inode_dirty(inode);
  318. iput(inode);
  319. return error;
  320. }
  321. int
  322. affs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  323. {
  324. struct inode *inode = d_inode(old_dentry);
  325. pr_debug("%s(%lu, %lu, \"%pd\")\n", __func__, inode->i_ino, dir->i_ino,
  326. dentry);
  327. return affs_add_entry(dir, inode, dentry, ST_LINKFILE);
  328. }
  329. static int
  330. affs_rename(struct inode *old_dir, struct dentry *old_dentry,
  331. struct inode *new_dir, struct dentry *new_dentry)
  332. {
  333. struct super_block *sb = old_dir->i_sb;
  334. struct buffer_head *bh = NULL;
  335. int retval;
  336. retval = affs_check_name(new_dentry->d_name.name,
  337. new_dentry->d_name.len,
  338. affs_nofilenametruncate(old_dentry));
  339. if (retval)
  340. return retval;
  341. /* Unlink destination if it already exists */
  342. if (d_really_is_positive(new_dentry)) {
  343. retval = affs_remove_header(new_dentry);
  344. if (retval)
  345. return retval;
  346. }
  347. bh = affs_bread(sb, d_inode(old_dentry)->i_ino);
  348. if (!bh)
  349. return -EIO;
  350. /* Remove header from its parent directory. */
  351. affs_lock_dir(old_dir);
  352. retval = affs_remove_hash(old_dir, bh);
  353. affs_unlock_dir(old_dir);
  354. if (retval)
  355. goto done;
  356. /* And insert it into the new directory with the new name. */
  357. affs_copy_name(AFFS_TAIL(sb, bh)->name, new_dentry);
  358. affs_fix_checksum(sb, bh);
  359. affs_lock_dir(new_dir);
  360. retval = affs_insert_hash(new_dir, bh);
  361. affs_unlock_dir(new_dir);
  362. /* TODO: move it back to old_dir, if error? */
  363. done:
  364. mark_buffer_dirty_inode(bh, retval ? old_dir : new_dir);
  365. affs_brelse(bh);
  366. return retval;
  367. }
  368. static int
  369. affs_xrename(struct inode *old_dir, struct dentry *old_dentry,
  370. struct inode *new_dir, struct dentry *new_dentry)
  371. {
  372. struct super_block *sb = old_dir->i_sb;
  373. struct buffer_head *bh_old = NULL;
  374. struct buffer_head *bh_new = NULL;
  375. int retval;
  376. bh_old = affs_bread(sb, d_inode(old_dentry)->i_ino);
  377. if (!bh_old)
  378. return -EIO;
  379. bh_new = affs_bread(sb, d_inode(new_dentry)->i_ino);
  380. if (!bh_new)
  381. return -EIO;
  382. /* Remove old header from its parent directory. */
  383. affs_lock_dir(old_dir);
  384. retval = affs_remove_hash(old_dir, bh_old);
  385. affs_unlock_dir(old_dir);
  386. if (retval)
  387. goto done;
  388. /* Remove new header from its parent directory. */
  389. affs_lock_dir(new_dir);
  390. retval = affs_remove_hash(new_dir, bh_new);
  391. affs_unlock_dir(new_dir);
  392. if (retval)
  393. goto done;
  394. /* Insert old into the new directory with the new name. */
  395. affs_copy_name(AFFS_TAIL(sb, bh_old)->name, new_dentry);
  396. affs_fix_checksum(sb, bh_old);
  397. affs_lock_dir(new_dir);
  398. retval = affs_insert_hash(new_dir, bh_old);
  399. affs_unlock_dir(new_dir);
  400. /* Insert new into the old directory with the old name. */
  401. affs_copy_name(AFFS_TAIL(sb, bh_new)->name, old_dentry);
  402. affs_fix_checksum(sb, bh_new);
  403. affs_lock_dir(old_dir);
  404. retval = affs_insert_hash(old_dir, bh_new);
  405. affs_unlock_dir(old_dir);
  406. done:
  407. mark_buffer_dirty_inode(bh_old, new_dir);
  408. mark_buffer_dirty_inode(bh_new, old_dir);
  409. affs_brelse(bh_old);
  410. affs_brelse(bh_new);
  411. return retval;
  412. }
  413. int affs_rename2(struct inode *old_dir, struct dentry *old_dentry,
  414. struct inode *new_dir, struct dentry *new_dentry,
  415. unsigned int flags)
  416. {
  417. if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
  418. return -EINVAL;
  419. pr_debug("%s(old=%lu,\"%pd\" to new=%lu,\"%pd\")\n", __func__,
  420. old_dir->i_ino, old_dentry, new_dir->i_ino, new_dentry);
  421. if (flags & RENAME_EXCHANGE)
  422. return affs_xrename(old_dir, old_dentry, new_dir, new_dentry);
  423. return affs_rename(old_dir, old_dentry, new_dir, new_dentry);
  424. }
  425. static struct dentry *affs_get_parent(struct dentry *child)
  426. {
  427. struct inode *parent;
  428. struct buffer_head *bh;
  429. bh = affs_bread(child->d_sb, d_inode(child)->i_ino);
  430. if (!bh)
  431. return ERR_PTR(-EIO);
  432. parent = affs_iget(child->d_sb,
  433. be32_to_cpu(AFFS_TAIL(child->d_sb, bh)->parent));
  434. brelse(bh);
  435. if (IS_ERR(parent))
  436. return ERR_CAST(parent);
  437. return d_obtain_alias(parent);
  438. }
  439. static struct inode *affs_nfs_get_inode(struct super_block *sb, u64 ino,
  440. u32 generation)
  441. {
  442. struct inode *inode;
  443. if (!affs_validblock(sb, ino))
  444. return ERR_PTR(-ESTALE);
  445. inode = affs_iget(sb, ino);
  446. if (IS_ERR(inode))
  447. return ERR_CAST(inode);
  448. return inode;
  449. }
  450. static struct dentry *affs_fh_to_dentry(struct super_block *sb, struct fid *fid,
  451. int fh_len, int fh_type)
  452. {
  453. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  454. affs_nfs_get_inode);
  455. }
  456. static struct dentry *affs_fh_to_parent(struct super_block *sb, struct fid *fid,
  457. int fh_len, int fh_type)
  458. {
  459. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  460. affs_nfs_get_inode);
  461. }
  462. const struct export_operations affs_export_ops = {
  463. .fh_to_dentry = affs_fh_to_dentry,
  464. .fh_to_parent = affs_fh_to_parent,
  465. .get_parent = affs_get_parent,
  466. };
  467. const struct dentry_operations affs_dentry_operations = {
  468. .d_hash = affs_hash_dentry,
  469. .d_compare = affs_compare_dentry,
  470. };
  471. const struct dentry_operations affs_intl_dentry_operations = {
  472. .d_hash = affs_intl_hash_dentry,
  473. .d_compare = affs_intl_compare_dentry,
  474. };