namei.c 9.9 KB

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