namei.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 <linux/quotaops.h>
  34. #include "ext2.h"
  35. #include "xattr.h"
  36. #include "acl.h"
  37. #include "xip.h"
  38. static inline int ext2_add_nondir(struct dentry *dentry, struct inode *inode)
  39. {
  40. int err = ext2_add_link(dentry, inode);
  41. if (!err) {
  42. d_instantiate(dentry, inode);
  43. unlock_new_inode(inode);
  44. return 0;
  45. }
  46. inode_dec_link_count(inode);
  47. unlock_new_inode(inode);
  48. iput(inode);
  49. return err;
  50. }
  51. /*
  52. * Methods themselves.
  53. */
  54. static struct dentry *ext2_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
  55. {
  56. struct inode * inode;
  57. ino_t ino;
  58. if (dentry->d_name.len > EXT2_NAME_LEN)
  59. return ERR_PTR(-ENAMETOOLONG);
  60. ino = ext2_inode_by_name(dir, &dentry->d_name);
  61. inode = NULL;
  62. if (ino) {
  63. inode = ext2_iget(dir->i_sb, ino);
  64. if (inode == ERR_PTR(-ESTALE)) {
  65. ext2_error(dir->i_sb, __func__,
  66. "deleted inode referenced: %lu",
  67. (unsigned long) ino);
  68. return ERR_PTR(-EIO);
  69. }
  70. }
  71. return d_splice_alias(inode, dentry);
  72. }
  73. struct dentry *ext2_get_parent(struct dentry *child)
  74. {
  75. struct qstr dotdot = {.name = "..", .len = 2};
  76. unsigned long ino = ext2_inode_by_name(child->d_inode, &dotdot);
  77. if (!ino)
  78. return ERR_PTR(-ENOENT);
  79. return d_obtain_alias(ext2_iget(child->d_inode->i_sb, ino));
  80. }
  81. /*
  82. * By the time this is called, we already have created
  83. * the directory cache entry for the new file, but it
  84. * is so far negative - it has no inode.
  85. *
  86. * If the create succeeds, we fill in the inode information
  87. * with d_instantiate().
  88. */
  89. static int ext2_create (struct inode * dir, struct dentry * dentry, int mode, struct nameidata *nd)
  90. {
  91. struct inode *inode;
  92. dquot_initialize(dir);
  93. inode = ext2_new_inode(dir, mode, &dentry->d_name);
  94. if (IS_ERR(inode))
  95. return PTR_ERR(inode);
  96. inode->i_op = &ext2_file_inode_operations;
  97. if (ext2_use_xip(inode->i_sb)) {
  98. inode->i_mapping->a_ops = &ext2_aops_xip;
  99. inode->i_fop = &ext2_xip_file_operations;
  100. } else if (test_opt(inode->i_sb, NOBH)) {
  101. inode->i_mapping->a_ops = &ext2_nobh_aops;
  102. inode->i_fop = &ext2_file_operations;
  103. } else {
  104. inode->i_mapping->a_ops = &ext2_aops;
  105. inode->i_fop = &ext2_file_operations;
  106. }
  107. mark_inode_dirty(inode);
  108. return ext2_add_nondir(dentry, inode);
  109. }
  110. static int ext2_mknod (struct inode * dir, struct dentry *dentry, int mode, dev_t rdev)
  111. {
  112. struct inode * inode;
  113. int err;
  114. if (!new_valid_dev(rdev))
  115. return -EINVAL;
  116. dquot_initialize(dir);
  117. inode = ext2_new_inode (dir, mode, &dentry->d_name);
  118. err = PTR_ERR(inode);
  119. if (!IS_ERR(inode)) {
  120. init_special_inode(inode, inode->i_mode, rdev);
  121. #ifdef CONFIG_EXT2_FS_XATTR
  122. inode->i_op = &ext2_special_inode_operations;
  123. #endif
  124. mark_inode_dirty(inode);
  125. err = ext2_add_nondir(dentry, inode);
  126. }
  127. return err;
  128. }
  129. static int ext2_symlink (struct inode * dir, struct dentry * dentry,
  130. const char * symname)
  131. {
  132. struct super_block * sb = dir->i_sb;
  133. int err = -ENAMETOOLONG;
  134. unsigned l = strlen(symname)+1;
  135. struct inode * inode;
  136. if (l > sb->s_blocksize)
  137. goto out;
  138. dquot_initialize(dir);
  139. inode = ext2_new_inode (dir, S_IFLNK | S_IRWXUGO, &dentry->d_name);
  140. err = PTR_ERR(inode);
  141. if (IS_ERR(inode))
  142. goto out;
  143. if (l > sizeof (EXT2_I(inode)->i_data)) {
  144. /* slow symlink */
  145. inode->i_op = &ext2_symlink_inode_operations;
  146. if (test_opt(inode->i_sb, NOBH))
  147. inode->i_mapping->a_ops = &ext2_nobh_aops;
  148. else
  149. inode->i_mapping->a_ops = &ext2_aops;
  150. err = page_symlink(inode, symname, l);
  151. if (err)
  152. goto out_fail;
  153. } else {
  154. /* fast symlink */
  155. inode->i_op = &ext2_fast_symlink_inode_operations;
  156. memcpy((char*)(EXT2_I(inode)->i_data),symname,l);
  157. inode->i_size = l-1;
  158. }
  159. mark_inode_dirty(inode);
  160. err = ext2_add_nondir(dentry, inode);
  161. out:
  162. return err;
  163. out_fail:
  164. inode_dec_link_count(inode);
  165. unlock_new_inode(inode);
  166. iput (inode);
  167. goto out;
  168. }
  169. static int ext2_link (struct dentry * old_dentry, struct inode * dir,
  170. struct dentry *dentry)
  171. {
  172. struct inode *inode = old_dentry->d_inode;
  173. int err;
  174. if (inode->i_nlink >= EXT2_LINK_MAX)
  175. return -EMLINK;
  176. dquot_initialize(dir);
  177. inode->i_ctime = CURRENT_TIME_SEC;
  178. inode_inc_link_count(inode);
  179. ihold(inode);
  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. dquot_initialize(dir);
  196. inode_inc_link_count(dir);
  197. inode = ext2_new_inode(dir, S_IFDIR | mode, &dentry->d_name);
  198. err = PTR_ERR(inode);
  199. if (IS_ERR(inode))
  200. goto out_dir;
  201. inode->i_op = &ext2_dir_inode_operations;
  202. inode->i_fop = &ext2_dir_operations;
  203. if (test_opt(inode->i_sb, NOBH))
  204. inode->i_mapping->a_ops = &ext2_nobh_aops;
  205. else
  206. inode->i_mapping->a_ops = &ext2_aops;
  207. inode_inc_link_count(inode);
  208. err = ext2_make_empty(inode, dir);
  209. if (err)
  210. goto out_fail;
  211. err = ext2_add_link(dentry, inode);
  212. if (err)
  213. goto out_fail;
  214. d_instantiate(dentry, inode);
  215. unlock_new_inode(inode);
  216. out:
  217. return err;
  218. out_fail:
  219. inode_dec_link_count(inode);
  220. inode_dec_link_count(inode);
  221. unlock_new_inode(inode);
  222. iput(inode);
  223. out_dir:
  224. inode_dec_link_count(dir);
  225. goto out;
  226. }
  227. static int ext2_unlink(struct inode * dir, struct dentry *dentry)
  228. {
  229. struct inode * inode = dentry->d_inode;
  230. struct ext2_dir_entry_2 * de;
  231. struct page * page;
  232. int err = -ENOENT;
  233. dquot_initialize(dir);
  234. de = ext2_find_entry (dir, &dentry->d_name, &page);
  235. if (!de)
  236. goto out;
  237. err = ext2_delete_entry (de, page);
  238. if (err)
  239. goto out;
  240. inode->i_ctime = dir->i_ctime;
  241. inode_dec_link_count(inode);
  242. err = 0;
  243. out:
  244. return err;
  245. }
  246. static int ext2_rmdir (struct inode * dir, struct dentry *dentry)
  247. {
  248. struct inode * inode = dentry->d_inode;
  249. int err = -ENOTEMPTY;
  250. if (ext2_empty_dir(inode)) {
  251. err = ext2_unlink(dir, dentry);
  252. if (!err) {
  253. inode->i_size = 0;
  254. inode_dec_link_count(inode);
  255. inode_dec_link_count(dir);
  256. }
  257. }
  258. return err;
  259. }
  260. static int ext2_rename (struct inode * old_dir, struct dentry * old_dentry,
  261. struct inode * new_dir, struct dentry * new_dentry )
  262. {
  263. struct inode * old_inode = old_dentry->d_inode;
  264. struct inode * new_inode = new_dentry->d_inode;
  265. struct page * dir_page = NULL;
  266. struct ext2_dir_entry_2 * dir_de = NULL;
  267. struct page * old_page;
  268. struct ext2_dir_entry_2 * old_de;
  269. int err = -ENOENT;
  270. dquot_initialize(old_dir);
  271. dquot_initialize(new_dir);
  272. old_de = ext2_find_entry (old_dir, &old_dentry->d_name, &old_page);
  273. if (!old_de)
  274. goto out;
  275. if (S_ISDIR(old_inode->i_mode)) {
  276. err = -EIO;
  277. dir_de = ext2_dotdot(old_inode, &dir_page);
  278. if (!dir_de)
  279. goto out_old;
  280. }
  281. if (new_inode) {
  282. struct page *new_page;
  283. struct ext2_dir_entry_2 *new_de;
  284. err = -ENOTEMPTY;
  285. if (dir_de && !ext2_empty_dir (new_inode))
  286. goto out_dir;
  287. err = -ENOENT;
  288. new_de = ext2_find_entry (new_dir, &new_dentry->d_name, &new_page);
  289. if (!new_de)
  290. goto out_dir;
  291. ext2_set_link(new_dir, new_de, new_page, old_inode, 1);
  292. new_inode->i_ctime = CURRENT_TIME_SEC;
  293. if (dir_de)
  294. drop_nlink(new_inode);
  295. inode_dec_link_count(new_inode);
  296. } else {
  297. if (dir_de) {
  298. err = -EMLINK;
  299. if (new_dir->i_nlink >= EXT2_LINK_MAX)
  300. goto out_dir;
  301. }
  302. err = ext2_add_link(new_dentry, old_inode);
  303. if (err)
  304. goto out_dir;
  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. */
  312. old_inode->i_ctime = CURRENT_TIME_SEC;
  313. mark_inode_dirty(old_inode);
  314. ext2_delete_entry (old_de, old_page);
  315. if (dir_de) {
  316. if (old_dir != new_dir)
  317. ext2_set_link(old_inode, dir_de, dir_page, new_dir, 0);
  318. else {
  319. kunmap(dir_page);
  320. page_cache_release(dir_page);
  321. }
  322. inode_dec_link_count(old_dir);
  323. }
  324. return 0;
  325. out_dir:
  326. if (dir_de) {
  327. kunmap(dir_page);
  328. page_cache_release(dir_page);
  329. }
  330. out_old:
  331. kunmap(old_page);
  332. page_cache_release(old_page);
  333. out:
  334. return err;
  335. }
  336. const struct inode_operations ext2_dir_inode_operations = {
  337. .create = ext2_create,
  338. .lookup = ext2_lookup,
  339. .link = ext2_link,
  340. .unlink = ext2_unlink,
  341. .symlink = ext2_symlink,
  342. .mkdir = ext2_mkdir,
  343. .rmdir = ext2_rmdir,
  344. .mknod = ext2_mknod,
  345. .rename = ext2_rename,
  346. #ifdef CONFIG_EXT2_FS_XATTR
  347. .setxattr = generic_setxattr,
  348. .getxattr = generic_getxattr,
  349. .listxattr = ext2_listxattr,
  350. .removexattr = generic_removexattr,
  351. #endif
  352. .setattr = ext2_setattr,
  353. .get_acl = ext2_get_acl,
  354. };
  355. const struct inode_operations ext2_special_inode_operations = {
  356. #ifdef CONFIG_EXT2_FS_XATTR
  357. .setxattr = generic_setxattr,
  358. .getxattr = generic_getxattr,
  359. .listxattr = ext2_listxattr,
  360. .removexattr = generic_removexattr,
  361. #endif
  362. .setattr = ext2_setattr,
  363. .get_acl = ext2_get_acl,
  364. };