namei.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/ext2/namei.c
  4. *
  5. * Rewrite to pagecache. Almost all code had been changed, so blame me
  6. * if the things go wrong. Please, send bug reports to
  7. * viro@parcelfarce.linux.theplanet.co.uk
  8. *
  9. * Stuff here is basically a glue between the VFS and generic UNIXish
  10. * filesystem that keeps everything in pagecache. All knowledge of the
  11. * directory layout is in fs/ext2/dir.c - it turned out to be easily separatable
  12. * and it's easier to debug that way. In principle we might want to
  13. * generalize that a bit and turn it into a library. Or not.
  14. *
  15. * The only non-static object here is ext2_dir_inode_operations.
  16. *
  17. * TODO: get rid of kmap() use, add readahead.
  18. *
  19. * Copyright (C) 1992, 1993, 1994, 1995
  20. * Remy Card (card@masi.ibp.fr)
  21. * Laboratoire MASI - Institut Blaise Pascal
  22. * Universite Pierre et Marie Curie (Paris VI)
  23. *
  24. * from
  25. *
  26. * linux/fs/minix/namei.c
  27. *
  28. * Copyright (C) 1991, 1992 Linus Torvalds
  29. *
  30. * Big-endian to little-endian byte-swapping/bitmaps by
  31. * David S. Miller (davem@caip.rutgers.edu), 1995
  32. */
  33. #include <linux/pagemap.h>
  34. #include <linux/quotaops.h>
  35. #include "ext2.h"
  36. #include "xattr.h"
  37. #include "acl.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_new(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(child->d_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. int err;
  92. err = dquot_initialize(dir);
  93. if (err)
  94. return err;
  95. inode = ext2_new_inode(dir, mode, &dentry->d_name);
  96. if (IS_ERR(inode))
  97. return PTR_ERR(inode);
  98. ext2_set_file_ops(inode);
  99. mark_inode_dirty(inode);
  100. return ext2_add_nondir(dentry, inode);
  101. }
  102. static int ext2_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
  103. {
  104. struct inode *inode = ext2_new_inode(dir, mode, NULL);
  105. if (IS_ERR(inode))
  106. return PTR_ERR(inode);
  107. ext2_set_file_ops(inode);
  108. mark_inode_dirty(inode);
  109. d_tmpfile(dentry, inode);
  110. unlock_new_inode(inode);
  111. return 0;
  112. }
  113. static int ext2_mknod (struct inode * dir, struct dentry *dentry, umode_t mode, dev_t rdev)
  114. {
  115. struct inode * inode;
  116. int err;
  117. err = dquot_initialize(dir);
  118. if (err)
  119. return err;
  120. inode = ext2_new_inode (dir, mode, &dentry->d_name);
  121. err = PTR_ERR(inode);
  122. if (!IS_ERR(inode)) {
  123. init_special_inode(inode, inode->i_mode, rdev);
  124. #ifdef CONFIG_EXT2_FS_XATTR
  125. inode->i_op = &ext2_special_inode_operations;
  126. #endif
  127. mark_inode_dirty(inode);
  128. err = ext2_add_nondir(dentry, inode);
  129. }
  130. return err;
  131. }
  132. static int ext2_symlink (struct inode * dir, struct dentry * dentry,
  133. const char * symname)
  134. {
  135. struct super_block * sb = dir->i_sb;
  136. int err = -ENAMETOOLONG;
  137. unsigned l = strlen(symname)+1;
  138. struct inode * inode;
  139. if (l > sb->s_blocksize)
  140. goto out;
  141. err = dquot_initialize(dir);
  142. if (err)
  143. goto out;
  144. inode = ext2_new_inode (dir, S_IFLNK | S_IRWXUGO, &dentry->d_name);
  145. err = PTR_ERR(inode);
  146. if (IS_ERR(inode))
  147. goto out;
  148. if (l > sizeof (EXT2_I(inode)->i_data)) {
  149. /* slow symlink */
  150. inode->i_op = &ext2_symlink_inode_operations;
  151. inode_nohighmem(inode);
  152. if (test_opt(inode->i_sb, NOBH))
  153. inode->i_mapping->a_ops = &ext2_nobh_aops;
  154. else
  155. inode->i_mapping->a_ops = &ext2_aops;
  156. err = page_symlink(inode, symname, l);
  157. if (err)
  158. goto out_fail;
  159. } else {
  160. /* fast symlink */
  161. inode->i_op = &ext2_fast_symlink_inode_operations;
  162. inode->i_link = (char*)EXT2_I(inode)->i_data;
  163. memcpy(inode->i_link, symname, l);
  164. inode->i_size = l-1;
  165. }
  166. mark_inode_dirty(inode);
  167. err = ext2_add_nondir(dentry, inode);
  168. out:
  169. return err;
  170. out_fail:
  171. inode_dec_link_count(inode);
  172. unlock_new_inode(inode);
  173. iput (inode);
  174. goto out;
  175. }
  176. static int ext2_link (struct dentry * old_dentry, struct inode * dir,
  177. struct dentry *dentry)
  178. {
  179. struct inode *inode = d_inode(old_dentry);
  180. int err;
  181. err = dquot_initialize(dir);
  182. if (err)
  183. return err;
  184. inode->i_ctime = current_time(inode);
  185. inode_inc_link_count(inode);
  186. ihold(inode);
  187. err = ext2_add_link(dentry, inode);
  188. if (!err) {
  189. d_instantiate(dentry, inode);
  190. return 0;
  191. }
  192. inode_dec_link_count(inode);
  193. iput(inode);
  194. return err;
  195. }
  196. static int ext2_mkdir(struct inode * dir, struct dentry * dentry, umode_t mode)
  197. {
  198. struct inode * inode;
  199. int err;
  200. err = dquot_initialize(dir);
  201. if (err)
  202. return err;
  203. inode_inc_link_count(dir);
  204. inode = ext2_new_inode(dir, S_IFDIR | mode, &dentry->d_name);
  205. err = PTR_ERR(inode);
  206. if (IS_ERR(inode))
  207. goto out_dir;
  208. inode->i_op = &ext2_dir_inode_operations;
  209. inode->i_fop = &ext2_dir_operations;
  210. if (test_opt(inode->i_sb, NOBH))
  211. inode->i_mapping->a_ops = &ext2_nobh_aops;
  212. else
  213. inode->i_mapping->a_ops = &ext2_aops;
  214. inode_inc_link_count(inode);
  215. err = ext2_make_empty(inode, dir);
  216. if (err)
  217. goto out_fail;
  218. err = ext2_add_link(dentry, inode);
  219. if (err)
  220. goto out_fail;
  221. d_instantiate_new(dentry, inode);
  222. out:
  223. return err;
  224. out_fail:
  225. inode_dec_link_count(inode);
  226. inode_dec_link_count(inode);
  227. unlock_new_inode(inode);
  228. iput(inode);
  229. out_dir:
  230. inode_dec_link_count(dir);
  231. goto out;
  232. }
  233. static int ext2_unlink(struct inode * dir, struct dentry *dentry)
  234. {
  235. struct inode * inode = d_inode(dentry);
  236. struct ext2_dir_entry_2 * de;
  237. struct page * page;
  238. int err;
  239. err = dquot_initialize(dir);
  240. if (err)
  241. goto out;
  242. de = ext2_find_entry (dir, &dentry->d_name, &page);
  243. if (!de) {
  244. err = -ENOENT;
  245. goto out;
  246. }
  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. unsigned int flags)
  273. {
  274. struct inode * old_inode = d_inode(old_dentry);
  275. struct inode * new_inode = d_inode(new_dentry);
  276. struct page * dir_page = NULL;
  277. struct ext2_dir_entry_2 * dir_de = NULL;
  278. struct page * old_page;
  279. struct ext2_dir_entry_2 * old_de;
  280. int err;
  281. if (flags & ~RENAME_NOREPLACE)
  282. return -EINVAL;
  283. err = dquot_initialize(old_dir);
  284. if (err)
  285. goto out;
  286. err = dquot_initialize(new_dir);
  287. if (err)
  288. goto out;
  289. old_de = ext2_find_entry (old_dir, &old_dentry->d_name, &old_page);
  290. if (!old_de) {
  291. err = -ENOENT;
  292. goto out;
  293. }
  294. if (S_ISDIR(old_inode->i_mode)) {
  295. err = -EIO;
  296. dir_de = ext2_dotdot(old_inode, &dir_page);
  297. if (!dir_de)
  298. goto out_old;
  299. }
  300. if (new_inode) {
  301. struct page *new_page;
  302. struct ext2_dir_entry_2 *new_de;
  303. err = -ENOTEMPTY;
  304. if (dir_de && !ext2_empty_dir (new_inode))
  305. goto out_dir;
  306. err = -ENOENT;
  307. new_de = ext2_find_entry (new_dir, &new_dentry->d_name, &new_page);
  308. if (!new_de)
  309. goto out_dir;
  310. ext2_set_link(new_dir, new_de, new_page, old_inode, 1);
  311. new_inode->i_ctime = current_time(new_inode);
  312. if (dir_de)
  313. drop_nlink(new_inode);
  314. inode_dec_link_count(new_inode);
  315. } else {
  316. err = ext2_add_link(new_dentry, old_inode);
  317. if (err)
  318. goto out_dir;
  319. if (dir_de)
  320. inode_inc_link_count(new_dir);
  321. }
  322. /*
  323. * Like most other Unix systems, set the ctime for inodes on a
  324. * rename.
  325. */
  326. old_inode->i_ctime = current_time(old_inode);
  327. mark_inode_dirty(old_inode);
  328. ext2_delete_entry (old_de, old_page);
  329. if (dir_de) {
  330. if (old_dir != new_dir)
  331. ext2_set_link(old_inode, dir_de, dir_page, new_dir, 0);
  332. else {
  333. kunmap(dir_page);
  334. put_page(dir_page);
  335. }
  336. inode_dec_link_count(old_dir);
  337. }
  338. return 0;
  339. out_dir:
  340. if (dir_de) {
  341. kunmap(dir_page);
  342. put_page(dir_page);
  343. }
  344. out_old:
  345. kunmap(old_page);
  346. put_page(old_page);
  347. out:
  348. return err;
  349. }
  350. const struct inode_operations ext2_dir_inode_operations = {
  351. .create = ext2_create,
  352. .lookup = ext2_lookup,
  353. .link = ext2_link,
  354. .unlink = ext2_unlink,
  355. .symlink = ext2_symlink,
  356. .mkdir = ext2_mkdir,
  357. .rmdir = ext2_rmdir,
  358. .mknod = ext2_mknod,
  359. .rename = ext2_rename,
  360. #ifdef CONFIG_EXT2_FS_XATTR
  361. .listxattr = ext2_listxattr,
  362. #endif
  363. .setattr = ext2_setattr,
  364. .get_acl = ext2_get_acl,
  365. .set_acl = ext2_set_acl,
  366. .tmpfile = ext2_tmpfile,
  367. };
  368. const struct inode_operations ext2_special_inode_operations = {
  369. #ifdef CONFIG_EXT2_FS_XATTR
  370. .listxattr = ext2_listxattr,
  371. #endif
  372. .setattr = ext2_setattr,
  373. .get_acl = ext2_get_acl,
  374. .set_acl = ext2_set_acl,
  375. };