namei.c 9.0 KB

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