namei.c 9.8 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(child->d_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. inode_nohighmem(inode);
  165. if (test_opt(inode->i_sb, NOBH))
  166. inode->i_mapping->a_ops = &ext2_nobh_aops;
  167. else
  168. inode->i_mapping->a_ops = &ext2_aops;
  169. err = page_symlink(inode, symname, l);
  170. if (err)
  171. goto out_fail;
  172. } else {
  173. /* fast symlink */
  174. inode->i_op = &ext2_fast_symlink_inode_operations;
  175. inode->i_link = (char*)EXT2_I(inode)->i_data;
  176. memcpy(inode->i_link, 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. inode_dec_link_count(inode);
  185. unlock_new_inode(inode);
  186. iput (inode);
  187. goto out;
  188. }
  189. static int ext2_link (struct dentry * old_dentry, struct inode * dir,
  190. struct dentry *dentry)
  191. {
  192. struct inode *inode = d_inode(old_dentry);
  193. int err;
  194. err = dquot_initialize(dir);
  195. if (err)
  196. return err;
  197. inode->i_ctime = current_time(inode);
  198. inode_inc_link_count(inode);
  199. ihold(inode);
  200. err = ext2_add_link(dentry, inode);
  201. if (!err) {
  202. d_instantiate(dentry, inode);
  203. return 0;
  204. }
  205. inode_dec_link_count(inode);
  206. iput(inode);
  207. return err;
  208. }
  209. static int ext2_mkdir(struct inode * dir, struct dentry * dentry, umode_t mode)
  210. {
  211. struct inode * inode;
  212. int err;
  213. err = dquot_initialize(dir);
  214. if (err)
  215. return err;
  216. inode_inc_link_count(dir);
  217. inode = ext2_new_inode(dir, S_IFDIR | mode, &dentry->d_name);
  218. err = PTR_ERR(inode);
  219. if (IS_ERR(inode))
  220. goto out_dir;
  221. inode->i_op = &ext2_dir_inode_operations;
  222. inode->i_fop = &ext2_dir_operations;
  223. if (test_opt(inode->i_sb, NOBH))
  224. inode->i_mapping->a_ops = &ext2_nobh_aops;
  225. else
  226. inode->i_mapping->a_ops = &ext2_aops;
  227. inode_inc_link_count(inode);
  228. err = ext2_make_empty(inode, dir);
  229. if (err)
  230. goto out_fail;
  231. err = ext2_add_link(dentry, inode);
  232. if (err)
  233. goto out_fail;
  234. d_instantiate_new(dentry, inode);
  235. out:
  236. return err;
  237. out_fail:
  238. inode_dec_link_count(inode);
  239. inode_dec_link_count(inode);
  240. unlock_new_inode(inode);
  241. iput(inode);
  242. out_dir:
  243. inode_dec_link_count(dir);
  244. goto out;
  245. }
  246. static int ext2_unlink(struct inode * dir, struct dentry *dentry)
  247. {
  248. struct inode * inode = d_inode(dentry);
  249. struct ext2_dir_entry_2 * de;
  250. struct page * page;
  251. int err;
  252. err = dquot_initialize(dir);
  253. if (err)
  254. goto out;
  255. de = ext2_find_entry (dir, &dentry->d_name, &page);
  256. if (!de) {
  257. err = -ENOENT;
  258. goto out;
  259. }
  260. err = ext2_delete_entry (de, page);
  261. if (err)
  262. goto out;
  263. inode->i_ctime = dir->i_ctime;
  264. inode_dec_link_count(inode);
  265. err = 0;
  266. out:
  267. return err;
  268. }
  269. static int ext2_rmdir (struct inode * dir, struct dentry *dentry)
  270. {
  271. struct inode * inode = d_inode(dentry);
  272. int err = -ENOTEMPTY;
  273. if (ext2_empty_dir(inode)) {
  274. err = ext2_unlink(dir, dentry);
  275. if (!err) {
  276. inode->i_size = 0;
  277. inode_dec_link_count(inode);
  278. inode_dec_link_count(dir);
  279. }
  280. }
  281. return err;
  282. }
  283. static int ext2_rename (struct inode * old_dir, struct dentry * old_dentry,
  284. struct inode * new_dir, struct dentry * new_dentry,
  285. unsigned int flags)
  286. {
  287. struct inode * old_inode = d_inode(old_dentry);
  288. struct inode * new_inode = d_inode(new_dentry);
  289. struct page * dir_page = NULL;
  290. struct ext2_dir_entry_2 * dir_de = NULL;
  291. struct page * old_page;
  292. struct ext2_dir_entry_2 * old_de;
  293. int err;
  294. if (flags & ~RENAME_NOREPLACE)
  295. return -EINVAL;
  296. err = dquot_initialize(old_dir);
  297. if (err)
  298. goto out;
  299. err = dquot_initialize(new_dir);
  300. if (err)
  301. goto out;
  302. old_de = ext2_find_entry (old_dir, &old_dentry->d_name, &old_page);
  303. if (!old_de) {
  304. err = -ENOENT;
  305. goto out;
  306. }
  307. if (S_ISDIR(old_inode->i_mode)) {
  308. err = -EIO;
  309. dir_de = ext2_dotdot(old_inode, &dir_page);
  310. if (!dir_de)
  311. goto out_old;
  312. }
  313. if (new_inode) {
  314. struct page *new_page;
  315. struct ext2_dir_entry_2 *new_de;
  316. err = -ENOTEMPTY;
  317. if (dir_de && !ext2_empty_dir (new_inode))
  318. goto out_dir;
  319. err = -ENOENT;
  320. new_de = ext2_find_entry (new_dir, &new_dentry->d_name, &new_page);
  321. if (!new_de)
  322. goto out_dir;
  323. ext2_set_link(new_dir, new_de, new_page, old_inode, 1);
  324. new_inode->i_ctime = current_time(new_inode);
  325. if (dir_de)
  326. drop_nlink(new_inode);
  327. inode_dec_link_count(new_inode);
  328. } else {
  329. err = ext2_add_link(new_dentry, old_inode);
  330. if (err)
  331. goto out_dir;
  332. if (dir_de)
  333. inode_inc_link_count(new_dir);
  334. }
  335. /*
  336. * Like most other Unix systems, set the ctime for inodes on a
  337. * rename.
  338. */
  339. old_inode->i_ctime = current_time(old_inode);
  340. mark_inode_dirty(old_inode);
  341. ext2_delete_entry (old_de, old_page);
  342. if (dir_de) {
  343. if (old_dir != new_dir)
  344. ext2_set_link(old_inode, dir_de, dir_page, new_dir, 0);
  345. else {
  346. kunmap(dir_page);
  347. put_page(dir_page);
  348. }
  349. inode_dec_link_count(old_dir);
  350. }
  351. return 0;
  352. out_dir:
  353. if (dir_de) {
  354. kunmap(dir_page);
  355. put_page(dir_page);
  356. }
  357. out_old:
  358. kunmap(old_page);
  359. put_page(old_page);
  360. out:
  361. return err;
  362. }
  363. const struct inode_operations ext2_dir_inode_operations = {
  364. .create = ext2_create,
  365. .lookup = ext2_lookup,
  366. .link = ext2_link,
  367. .unlink = ext2_unlink,
  368. .symlink = ext2_symlink,
  369. .mkdir = ext2_mkdir,
  370. .rmdir = ext2_rmdir,
  371. .mknod = ext2_mknod,
  372. .rename = ext2_rename,
  373. #ifdef CONFIG_EXT2_FS_XATTR
  374. .listxattr = ext2_listxattr,
  375. #endif
  376. .setattr = ext2_setattr,
  377. .get_acl = ext2_get_acl,
  378. .set_acl = ext2_set_acl,
  379. .tmpfile = ext2_tmpfile,
  380. };
  381. const struct inode_operations ext2_special_inode_operations = {
  382. #ifdef CONFIG_EXT2_FS_XATTR
  383. .listxattr = ext2_listxattr,
  384. #endif
  385. .setattr = ext2_setattr,
  386. .get_acl = ext2_get_acl,
  387. .set_acl = ext2_set_acl,
  388. };