namei.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. * linux/fs/ext2/namei.c
  3. *
  4. * Rewrite to pagecache. Almost all code had been changed, so blame me
  5. * if the things go wrong. Please, send bug reports to
  6. * viro@parcelfarce.linux.theplanet.co.uk
  7. *
  8. * Stuff here is basically a glue between the VFS and generic UNIXish
  9. * filesystem that keeps everything in pagecache. All knowledge of the
  10. * directory layout is in fs/ext2/dir.c - it turned out to be easily separatable
  11. * and it's easier to debug that way. In principle we might want to
  12. * generalize that a bit and turn it into a library. Or not.
  13. *
  14. * The only non-static object here is ext2_dir_inode_operations.
  15. *
  16. * TODO: get rid of kmap() use, add readahead.
  17. *
  18. * Copyright (C) 1992, 1993, 1994, 1995
  19. * Remy Card (card@masi.ibp.fr)
  20. * Laboratoire MASI - Institut Blaise Pascal
  21. * Universite Pierre et Marie Curie (Paris VI)
  22. *
  23. * from
  24. *
  25. * linux/fs/minix/namei.c
  26. *
  27. * Copyright (C) 1991, 1992 Linus Torvalds
  28. *
  29. * Big-endian to little-endian byte-swapping/bitmaps by
  30. * David S. Miller (davem@caip.rutgers.edu), 1995
  31. */
  32. #include <linux/pagemap.h>
  33. #include "ext2.h"
  34. #include "xattr.h"
  35. #include "acl.h"
  36. #include "xip.h"
  37. static inline int ext2_add_nondir(struct dentry *dentry, struct inode *inode)
  38. {
  39. int err = ext2_add_link(dentry, inode);
  40. if (!err) {
  41. d_instantiate(dentry, inode);
  42. unlock_new_inode(inode);
  43. return 0;
  44. }
  45. inode_dec_link_count(inode);
  46. unlock_new_inode(inode);
  47. iput(inode);
  48. return err;
  49. }
  50. /*
  51. * Methods themselves.
  52. */
  53. static struct dentry *ext2_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
  54. {
  55. struct inode * inode;
  56. ino_t ino;
  57. if (dentry->d_name.len > EXT2_NAME_LEN)
  58. return ERR_PTR(-ENAMETOOLONG);
  59. ino = ext2_inode_by_name(dir, &dentry->d_name);
  60. inode = NULL;
  61. if (ino) {
  62. inode = ext2_iget(dir->i_sb, ino);
  63. if (unlikely(IS_ERR(inode))) {
  64. if (PTR_ERR(inode) == -ESTALE) {
  65. ext2_error(dir->i_sb, __func__,
  66. "deleted inode referenced: %lu",
  67. (unsigned long) ino);
  68. return ERR_PTR(-EIO);
  69. } else {
  70. return ERR_CAST(inode);
  71. }
  72. }
  73. }
  74. return d_splice_alias(inode, dentry);
  75. }
  76. struct dentry *ext2_get_parent(struct dentry *child)
  77. {
  78. struct qstr dotdot = {.name = "..", .len = 2};
  79. unsigned long ino = ext2_inode_by_name(child->d_inode, &dotdot);
  80. if (!ino)
  81. return ERR_PTR(-ENOENT);
  82. return d_obtain_alias(ext2_iget(child->d_inode->i_sb, ino));
  83. }
  84. /*
  85. * By the time this is called, we already have created
  86. * the directory cache entry for the new file, but it
  87. * is so far negative - it has no inode.
  88. *
  89. * If the create succeeds, we fill in the inode information
  90. * with d_instantiate().
  91. */
  92. static int ext2_create (struct inode * dir, struct dentry * dentry, int mode, struct nameidata *nd)
  93. {
  94. struct inode * inode = ext2_new_inode (dir, mode);
  95. int err = PTR_ERR(inode);
  96. if (!IS_ERR(inode)) {
  97. inode->i_op = &ext2_file_inode_operations;
  98. if (ext2_use_xip(inode->i_sb)) {
  99. inode->i_mapping->a_ops = &ext2_aops_xip;
  100. inode->i_fop = &ext2_xip_file_operations;
  101. } else if (test_opt(inode->i_sb, NOBH)) {
  102. inode->i_mapping->a_ops = &ext2_nobh_aops;
  103. inode->i_fop = &ext2_file_operations;
  104. } else {
  105. inode->i_mapping->a_ops = &ext2_aops;
  106. inode->i_fop = &ext2_file_operations;
  107. }
  108. mark_inode_dirty(inode);
  109. err = ext2_add_nondir(dentry, inode);
  110. }
  111. return err;
  112. }
  113. static int ext2_mknod (struct inode * dir, struct dentry *dentry, int mode, dev_t rdev)
  114. {
  115. struct inode * inode;
  116. int err;
  117. if (!new_valid_dev(rdev))
  118. return -EINVAL;
  119. inode = ext2_new_inode (dir, mode);
  120. err = PTR_ERR(inode);
  121. if (!IS_ERR(inode)) {
  122. init_special_inode(inode, inode->i_mode, rdev);
  123. #ifdef CONFIG_EXT2_FS_XATTR
  124. inode->i_op = &ext2_special_inode_operations;
  125. #endif
  126. mark_inode_dirty(inode);
  127. err = ext2_add_nondir(dentry, inode);
  128. }
  129. return err;
  130. }
  131. static int ext2_symlink (struct inode * dir, struct dentry * dentry,
  132. const char * symname)
  133. {
  134. struct super_block * sb = dir->i_sb;
  135. int err = -ENAMETOOLONG;
  136. unsigned l = strlen(symname)+1;
  137. struct inode * inode;
  138. if (l > sb->s_blocksize)
  139. goto out;
  140. inode = ext2_new_inode (dir, S_IFLNK | S_IRWXUGO);
  141. err = PTR_ERR(inode);
  142. if (IS_ERR(inode))
  143. goto out;
  144. if (l > sizeof (EXT2_I(inode)->i_data)) {
  145. /* slow symlink */
  146. inode->i_op = &ext2_symlink_inode_operations;
  147. if (test_opt(inode->i_sb, NOBH))
  148. inode->i_mapping->a_ops = &ext2_nobh_aops;
  149. else
  150. inode->i_mapping->a_ops = &ext2_aops;
  151. err = page_symlink(inode, symname, l);
  152. if (err)
  153. goto out_fail;
  154. } else {
  155. /* fast symlink */
  156. inode->i_op = &ext2_fast_symlink_inode_operations;
  157. memcpy((char*)(EXT2_I(inode)->i_data),symname,l);
  158. inode->i_size = l-1;
  159. }
  160. mark_inode_dirty(inode);
  161. err = ext2_add_nondir(dentry, inode);
  162. out:
  163. return err;
  164. out_fail:
  165. inode_dec_link_count(inode);
  166. unlock_new_inode(inode);
  167. iput (inode);
  168. goto out;
  169. }
  170. static int ext2_link (struct dentry * old_dentry, struct inode * dir,
  171. struct dentry *dentry)
  172. {
  173. struct inode *inode = old_dentry->d_inode;
  174. int err;
  175. if (inode->i_nlink >= EXT2_LINK_MAX)
  176. return -EMLINK;
  177. inode->i_ctime = CURRENT_TIME_SEC;
  178. inode_inc_link_count(inode);
  179. atomic_inc(&inode->i_count);
  180. err = ext2_add_link(dentry, inode);
  181. if (!err) {
  182. d_instantiate(dentry, inode);
  183. return 0;
  184. }
  185. inode_dec_link_count(inode);
  186. iput(inode);
  187. return err;
  188. }
  189. static int ext2_mkdir(struct inode * dir, struct dentry * dentry, int mode)
  190. {
  191. struct inode * inode;
  192. int err = -EMLINK;
  193. if (dir->i_nlink >= EXT2_LINK_MAX)
  194. goto out;
  195. inode_inc_link_count(dir);
  196. inode = ext2_new_inode (dir, S_IFDIR | mode);
  197. err = PTR_ERR(inode);
  198. if (IS_ERR(inode))
  199. goto out_dir;
  200. inode->i_op = &ext2_dir_inode_operations;
  201. inode->i_fop = &ext2_dir_operations;
  202. if (test_opt(inode->i_sb, NOBH))
  203. inode->i_mapping->a_ops = &ext2_nobh_aops;
  204. else
  205. inode->i_mapping->a_ops = &ext2_aops;
  206. inode_inc_link_count(inode);
  207. err = ext2_make_empty(inode, dir);
  208. if (err)
  209. goto out_fail;
  210. err = ext2_add_link(dentry, inode);
  211. if (err)
  212. goto out_fail;
  213. d_instantiate(dentry, inode);
  214. unlock_new_inode(inode);
  215. out:
  216. return err;
  217. out_fail:
  218. inode_dec_link_count(inode);
  219. inode_dec_link_count(inode);
  220. unlock_new_inode(inode);
  221. iput(inode);
  222. out_dir:
  223. inode_dec_link_count(dir);
  224. goto out;
  225. }
  226. static int ext2_unlink(struct inode * dir, struct dentry *dentry)
  227. {
  228. struct inode * inode = dentry->d_inode;
  229. struct ext2_dir_entry_2 * de;
  230. struct page * page;
  231. int err = -ENOENT;
  232. de = ext2_find_entry (dir, &dentry->d_name, &page);
  233. if (!de)
  234. goto out;
  235. err = ext2_delete_entry (de, page);
  236. if (err)
  237. goto out;
  238. inode->i_ctime = dir->i_ctime;
  239. inode_dec_link_count(inode);
  240. err = 0;
  241. out:
  242. return err;
  243. }
  244. static int ext2_rmdir (struct inode * dir, struct dentry *dentry)
  245. {
  246. struct inode * inode = dentry->d_inode;
  247. int err = -ENOTEMPTY;
  248. if (ext2_empty_dir(inode)) {
  249. err = ext2_unlink(dir, dentry);
  250. if (!err) {
  251. inode->i_size = 0;
  252. inode_dec_link_count(inode);
  253. inode_dec_link_count(dir);
  254. }
  255. }
  256. return err;
  257. }
  258. static int ext2_rename (struct inode * old_dir, struct dentry * old_dentry,
  259. struct inode * new_dir, struct dentry * new_dentry )
  260. {
  261. struct inode * old_inode = old_dentry->d_inode;
  262. struct inode * new_inode = new_dentry->d_inode;
  263. struct page * dir_page = NULL;
  264. struct ext2_dir_entry_2 * dir_de = NULL;
  265. struct page * old_page;
  266. struct ext2_dir_entry_2 * old_de;
  267. int err = -ENOENT;
  268. old_de = ext2_find_entry (old_dir, &old_dentry->d_name, &old_page);
  269. if (!old_de)
  270. goto out;
  271. if (S_ISDIR(old_inode->i_mode)) {
  272. err = -EIO;
  273. dir_de = ext2_dotdot(old_inode, &dir_page);
  274. if (!dir_de)
  275. goto out_old;
  276. }
  277. if (new_inode) {
  278. struct page *new_page;
  279. struct ext2_dir_entry_2 *new_de;
  280. err = -ENOTEMPTY;
  281. if (dir_de && !ext2_empty_dir (new_inode))
  282. goto out_dir;
  283. err = -ENOENT;
  284. new_de = ext2_find_entry (new_dir, &new_dentry->d_name, &new_page);
  285. if (!new_de)
  286. goto out_dir;
  287. inode_inc_link_count(old_inode);
  288. ext2_set_link(new_dir, new_de, new_page, old_inode, 1);
  289. new_inode->i_ctime = CURRENT_TIME_SEC;
  290. if (dir_de)
  291. drop_nlink(new_inode);
  292. inode_dec_link_count(new_inode);
  293. } else {
  294. if (dir_de) {
  295. err = -EMLINK;
  296. if (new_dir->i_nlink >= EXT2_LINK_MAX)
  297. goto out_dir;
  298. }
  299. inode_inc_link_count(old_inode);
  300. err = ext2_add_link(new_dentry, old_inode);
  301. if (err) {
  302. inode_dec_link_count(old_inode);
  303. goto out_dir;
  304. }
  305. if (dir_de)
  306. inode_inc_link_count(new_dir);
  307. }
  308. /*
  309. * Like most other Unix systems, set the ctime for inodes on a
  310. * rename.
  311. * inode_dec_link_count() will mark the inode dirty.
  312. */
  313. old_inode->i_ctime = CURRENT_TIME_SEC;
  314. ext2_delete_entry (old_de, old_page);
  315. inode_dec_link_count(old_inode);
  316. if (dir_de) {
  317. if (old_dir != new_dir)
  318. ext2_set_link(old_inode, dir_de, dir_page, new_dir, 0);
  319. else {
  320. kunmap(dir_page);
  321. page_cache_release(dir_page);
  322. }
  323. inode_dec_link_count(old_dir);
  324. }
  325. return 0;
  326. out_dir:
  327. if (dir_de) {
  328. kunmap(dir_page);
  329. page_cache_release(dir_page);
  330. }
  331. out_old:
  332. kunmap(old_page);
  333. page_cache_release(old_page);
  334. out:
  335. return err;
  336. }
  337. const struct inode_operations ext2_dir_inode_operations = {
  338. .create = ext2_create,
  339. .lookup = ext2_lookup,
  340. .link = ext2_link,
  341. .unlink = ext2_unlink,
  342. .symlink = ext2_symlink,
  343. .mkdir = ext2_mkdir,
  344. .rmdir = ext2_rmdir,
  345. .mknod = ext2_mknod,
  346. .rename = ext2_rename,
  347. #ifdef CONFIG_EXT2_FS_XATTR
  348. .setxattr = generic_setxattr,
  349. .getxattr = generic_getxattr,
  350. .listxattr = ext2_listxattr,
  351. .removexattr = generic_removexattr,
  352. #endif
  353. .setattr = ext2_setattr,
  354. .check_acl = ext2_check_acl,
  355. };
  356. const struct inode_operations ext2_special_inode_operations = {
  357. #ifdef CONFIG_EXT2_FS_XATTR
  358. .setxattr = generic_setxattr,
  359. .getxattr = generic_getxattr,
  360. .listxattr = ext2_listxattr,
  361. .removexattr = generic_removexattr,
  362. #endif
  363. .setattr = ext2_setattr,
  364. .check_acl = ext2_check_acl,
  365. };