namei.c 9.7 KB

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