namei.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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 = QSTR_INIT("..", 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, umode_t 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, umode_t 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. dquot_initialize(dir);
  175. inode->i_ctime = CURRENT_TIME_SEC;
  176. inode_inc_link_count(inode);
  177. ihold(inode);
  178. err = ext2_add_link(dentry, inode);
  179. if (!err) {
  180. d_instantiate(dentry, inode);
  181. return 0;
  182. }
  183. inode_dec_link_count(inode);
  184. iput(inode);
  185. return err;
  186. }
  187. static int ext2_mkdir(struct inode * dir, struct dentry * dentry, umode_t mode)
  188. {
  189. struct inode * inode;
  190. int err;
  191. dquot_initialize(dir);
  192. inode_inc_link_count(dir);
  193. inode = ext2_new_inode(dir, S_IFDIR | mode, &dentry->d_name);
  194. err = PTR_ERR(inode);
  195. if (IS_ERR(inode))
  196. goto out_dir;
  197. inode->i_op = &ext2_dir_inode_operations;
  198. inode->i_fop = &ext2_dir_operations;
  199. if (test_opt(inode->i_sb, NOBH))
  200. inode->i_mapping->a_ops = &ext2_nobh_aops;
  201. else
  202. inode->i_mapping->a_ops = &ext2_aops;
  203. inode_inc_link_count(inode);
  204. err = ext2_make_empty(inode, dir);
  205. if (err)
  206. goto out_fail;
  207. err = ext2_add_link(dentry, inode);
  208. if (err)
  209. goto out_fail;
  210. d_instantiate(dentry, inode);
  211. unlock_new_inode(inode);
  212. out:
  213. return err;
  214. out_fail:
  215. inode_dec_link_count(inode);
  216. inode_dec_link_count(inode);
  217. unlock_new_inode(inode);
  218. iput(inode);
  219. out_dir:
  220. inode_dec_link_count(dir);
  221. goto out;
  222. }
  223. static int ext2_unlink(struct inode * dir, struct dentry *dentry)
  224. {
  225. struct inode * inode = dentry->d_inode;
  226. struct ext2_dir_entry_2 * de;
  227. struct page * page;
  228. int err = -ENOENT;
  229. dquot_initialize(dir);
  230. de = ext2_find_entry (dir, &dentry->d_name, &page);
  231. if (!de)
  232. goto out;
  233. err = ext2_delete_entry (de, page);
  234. if (err)
  235. goto out;
  236. inode->i_ctime = dir->i_ctime;
  237. inode_dec_link_count(inode);
  238. err = 0;
  239. out:
  240. return err;
  241. }
  242. static int ext2_rmdir (struct inode * dir, struct dentry *dentry)
  243. {
  244. struct inode * inode = dentry->d_inode;
  245. int err = -ENOTEMPTY;
  246. if (ext2_empty_dir(inode)) {
  247. err = ext2_unlink(dir, dentry);
  248. if (!err) {
  249. inode->i_size = 0;
  250. inode_dec_link_count(inode);
  251. inode_dec_link_count(dir);
  252. }
  253. }
  254. return err;
  255. }
  256. static int ext2_rename (struct inode * old_dir, struct dentry * old_dentry,
  257. struct inode * new_dir, struct dentry * new_dentry )
  258. {
  259. struct inode * old_inode = old_dentry->d_inode;
  260. struct inode * new_inode = new_dentry->d_inode;
  261. struct page * dir_page = NULL;
  262. struct ext2_dir_entry_2 * dir_de = NULL;
  263. struct page * old_page;
  264. struct ext2_dir_entry_2 * old_de;
  265. int err = -ENOENT;
  266. dquot_initialize(old_dir);
  267. dquot_initialize(new_dir);
  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. ext2_set_link(new_dir, new_de, new_page, old_inode, 1);
  288. new_inode->i_ctime = CURRENT_TIME_SEC;
  289. if (dir_de)
  290. drop_nlink(new_inode);
  291. inode_dec_link_count(new_inode);
  292. } else {
  293. err = ext2_add_link(new_dentry, old_inode);
  294. if (err)
  295. goto out_dir;
  296. if (dir_de)
  297. inode_inc_link_count(new_dir);
  298. }
  299. /*
  300. * Like most other Unix systems, set the ctime for inodes on a
  301. * rename.
  302. */
  303. old_inode->i_ctime = CURRENT_TIME_SEC;
  304. mark_inode_dirty(old_inode);
  305. ext2_delete_entry (old_de, old_page);
  306. if (dir_de) {
  307. if (old_dir != new_dir)
  308. ext2_set_link(old_inode, dir_de, dir_page, new_dir, 0);
  309. else {
  310. kunmap(dir_page);
  311. page_cache_release(dir_page);
  312. }
  313. inode_dec_link_count(old_dir);
  314. }
  315. return 0;
  316. out_dir:
  317. if (dir_de) {
  318. kunmap(dir_page);
  319. page_cache_release(dir_page);
  320. }
  321. out_old:
  322. kunmap(old_page);
  323. page_cache_release(old_page);
  324. out:
  325. return err;
  326. }
  327. const struct inode_operations ext2_dir_inode_operations = {
  328. .create = ext2_create,
  329. .lookup = ext2_lookup,
  330. .link = ext2_link,
  331. .unlink = ext2_unlink,
  332. .symlink = ext2_symlink,
  333. .mkdir = ext2_mkdir,
  334. .rmdir = ext2_rmdir,
  335. .mknod = ext2_mknod,
  336. .rename = ext2_rename,
  337. #ifdef CONFIG_EXT2_FS_XATTR
  338. .setxattr = generic_setxattr,
  339. .getxattr = generic_getxattr,
  340. .listxattr = ext2_listxattr,
  341. .removexattr = generic_removexattr,
  342. #endif
  343. .setattr = ext2_setattr,
  344. .get_acl = ext2_get_acl,
  345. };
  346. const struct inode_operations ext2_special_inode_operations = {
  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. .get_acl = ext2_get_acl,
  355. };