namei.c 9.2 KB

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