namei.c 9.1 KB

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