namei.c 9.3 KB

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