namei.c 9.7 KB

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