namei.c 9.3 KB

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