namei.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. discard_new_inode(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. ext2_set_file_ops(inode);
  98. mark_inode_dirty(inode);
  99. return ext2_add_nondir(dentry, inode);
  100. }
  101. static int ext2_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
  102. {
  103. struct inode *inode = ext2_new_inode(dir, mode, NULL);
  104. if (IS_ERR(inode))
  105. return PTR_ERR(inode);
  106. ext2_set_file_ops(inode);
  107. mark_inode_dirty(inode);
  108. d_tmpfile(dentry, inode);
  109. unlock_new_inode(inode);
  110. return 0;
  111. }
  112. static int ext2_mknod (struct inode * dir, struct dentry *dentry, umode_t mode, dev_t rdev)
  113. {
  114. struct inode * inode;
  115. int err;
  116. err = dquot_initialize(dir);
  117. if (err)
  118. return err;
  119. inode = ext2_new_inode (dir, mode, &dentry->d_name);
  120. err = PTR_ERR(inode);
  121. if (!IS_ERR(inode)) {
  122. init_special_inode(inode, inode->i_mode, rdev);
  123. #ifdef CONFIG_EXT2_FS_XATTR
  124. inode->i_op = &ext2_special_inode_operations;
  125. #endif
  126. mark_inode_dirty(inode);
  127. err = ext2_add_nondir(dentry, inode);
  128. }
  129. return err;
  130. }
  131. static int ext2_symlink (struct inode * dir, struct dentry * dentry,
  132. const char * symname)
  133. {
  134. struct super_block * sb = dir->i_sb;
  135. int err = -ENAMETOOLONG;
  136. unsigned l = strlen(symname)+1;
  137. struct inode * inode;
  138. if (l > sb->s_blocksize)
  139. goto out;
  140. err = dquot_initialize(dir);
  141. if (err)
  142. goto out;
  143. inode = ext2_new_inode (dir, S_IFLNK | S_IRWXUGO, &dentry->d_name);
  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. inode_nohighmem(inode);
  151. if (test_opt(inode->i_sb, NOBH))
  152. inode->i_mapping->a_ops = &ext2_nobh_aops;
  153. else
  154. inode->i_mapping->a_ops = &ext2_aops;
  155. err = page_symlink(inode, symname, l);
  156. if (err)
  157. goto out_fail;
  158. } else {
  159. /* fast symlink */
  160. inode->i_op = &ext2_fast_symlink_inode_operations;
  161. inode->i_link = (char*)EXT2_I(inode)->i_data;
  162. memcpy(inode->i_link, symname, l);
  163. inode->i_size = l-1;
  164. }
  165. mark_inode_dirty(inode);
  166. err = ext2_add_nondir(dentry, inode);
  167. out:
  168. return err;
  169. out_fail:
  170. inode_dec_link_count(inode);
  171. discard_new_inode(inode);
  172. goto out;
  173. }
  174. static int ext2_link (struct dentry * old_dentry, struct inode * dir,
  175. struct dentry *dentry)
  176. {
  177. struct inode *inode = d_inode(old_dentry);
  178. int err;
  179. err = dquot_initialize(dir);
  180. if (err)
  181. return err;
  182. inode->i_ctime = current_time(inode);
  183. inode_inc_link_count(inode);
  184. ihold(inode);
  185. err = ext2_add_link(dentry, inode);
  186. if (!err) {
  187. d_instantiate(dentry, inode);
  188. return 0;
  189. }
  190. inode_dec_link_count(inode);
  191. iput(inode);
  192. return err;
  193. }
  194. static int ext2_mkdir(struct inode * dir, struct dentry * dentry, umode_t mode)
  195. {
  196. struct inode * inode;
  197. int err;
  198. err = dquot_initialize(dir);
  199. if (err)
  200. return err;
  201. inode_inc_link_count(dir);
  202. inode = ext2_new_inode(dir, S_IFDIR | mode, &dentry->d_name);
  203. err = PTR_ERR(inode);
  204. if (IS_ERR(inode))
  205. goto out_dir;
  206. inode->i_op = &ext2_dir_inode_operations;
  207. inode->i_fop = &ext2_dir_operations;
  208. if (test_opt(inode->i_sb, NOBH))
  209. inode->i_mapping->a_ops = &ext2_nobh_aops;
  210. else
  211. inode->i_mapping->a_ops = &ext2_aops;
  212. inode_inc_link_count(inode);
  213. err = ext2_make_empty(inode, dir);
  214. if (err)
  215. goto out_fail;
  216. err = ext2_add_link(dentry, inode);
  217. if (err)
  218. goto out_fail;
  219. d_instantiate_new(dentry, inode);
  220. out:
  221. return err;
  222. out_fail:
  223. inode_dec_link_count(inode);
  224. inode_dec_link_count(inode);
  225. discard_new_inode(inode);
  226. out_dir:
  227. inode_dec_link_count(dir);
  228. goto out;
  229. }
  230. static int ext2_unlink(struct inode * dir, struct dentry *dentry)
  231. {
  232. struct inode * inode = d_inode(dentry);
  233. struct ext2_dir_entry_2 * de;
  234. struct page * page;
  235. int err;
  236. err = dquot_initialize(dir);
  237. if (err)
  238. goto out;
  239. de = ext2_find_entry (dir, &dentry->d_name, &page);
  240. if (!de) {
  241. err = -ENOENT;
  242. goto out;
  243. }
  244. err = ext2_delete_entry (de, page);
  245. if (err)
  246. goto out;
  247. inode->i_ctime = dir->i_ctime;
  248. inode_dec_link_count(inode);
  249. err = 0;
  250. out:
  251. return err;
  252. }
  253. static int ext2_rmdir (struct inode * dir, struct dentry *dentry)
  254. {
  255. struct inode * inode = d_inode(dentry);
  256. int err = -ENOTEMPTY;
  257. if (ext2_empty_dir(inode)) {
  258. err = ext2_unlink(dir, dentry);
  259. if (!err) {
  260. inode->i_size = 0;
  261. inode_dec_link_count(inode);
  262. inode_dec_link_count(dir);
  263. }
  264. }
  265. return err;
  266. }
  267. static int ext2_rename (struct inode * old_dir, struct dentry * old_dentry,
  268. struct inode * new_dir, struct dentry * new_dentry,
  269. unsigned int flags)
  270. {
  271. struct inode * old_inode = d_inode(old_dentry);
  272. struct inode * new_inode = d_inode(new_dentry);
  273. struct page * dir_page = NULL;
  274. struct ext2_dir_entry_2 * dir_de = NULL;
  275. struct page * old_page;
  276. struct ext2_dir_entry_2 * old_de;
  277. int err;
  278. if (flags & ~RENAME_NOREPLACE)
  279. return -EINVAL;
  280. err = dquot_initialize(old_dir);
  281. if (err)
  282. goto out;
  283. err = dquot_initialize(new_dir);
  284. if (err)
  285. goto out;
  286. old_de = ext2_find_entry (old_dir, &old_dentry->d_name, &old_page);
  287. if (!old_de) {
  288. err = -ENOENT;
  289. goto out;
  290. }
  291. if (S_ISDIR(old_inode->i_mode)) {
  292. err = -EIO;
  293. dir_de = ext2_dotdot(old_inode, &dir_page);
  294. if (!dir_de)
  295. goto out_old;
  296. }
  297. if (new_inode) {
  298. struct page *new_page;
  299. struct ext2_dir_entry_2 *new_de;
  300. err = -ENOTEMPTY;
  301. if (dir_de && !ext2_empty_dir (new_inode))
  302. goto out_dir;
  303. err = -ENOENT;
  304. new_de = ext2_find_entry (new_dir, &new_dentry->d_name, &new_page);
  305. if (!new_de)
  306. goto out_dir;
  307. ext2_set_link(new_dir, new_de, new_page, old_inode, 1);
  308. new_inode->i_ctime = current_time(new_inode);
  309. if (dir_de)
  310. drop_nlink(new_inode);
  311. inode_dec_link_count(new_inode);
  312. } else {
  313. err = ext2_add_link(new_dentry, old_inode);
  314. if (err)
  315. goto out_dir;
  316. if (dir_de)
  317. inode_inc_link_count(new_dir);
  318. }
  319. /*
  320. * Like most other Unix systems, set the ctime for inodes on a
  321. * rename.
  322. */
  323. old_inode->i_ctime = current_time(old_inode);
  324. mark_inode_dirty(old_inode);
  325. ext2_delete_entry (old_de, old_page);
  326. if (dir_de) {
  327. if (old_dir != new_dir)
  328. ext2_set_link(old_inode, dir_de, dir_page, new_dir, 0);
  329. else {
  330. kunmap(dir_page);
  331. put_page(dir_page);
  332. }
  333. inode_dec_link_count(old_dir);
  334. }
  335. return 0;
  336. out_dir:
  337. if (dir_de) {
  338. kunmap(dir_page);
  339. put_page(dir_page);
  340. }
  341. out_old:
  342. kunmap(old_page);
  343. put_page(old_page);
  344. out:
  345. return err;
  346. }
  347. const struct inode_operations ext2_dir_inode_operations = {
  348. .create = ext2_create,
  349. .lookup = ext2_lookup,
  350. .link = ext2_link,
  351. .unlink = ext2_unlink,
  352. .symlink = ext2_symlink,
  353. .mkdir = ext2_mkdir,
  354. .rmdir = ext2_rmdir,
  355. .mknod = ext2_mknod,
  356. .rename = ext2_rename,
  357. #ifdef CONFIG_EXT2_FS_XATTR
  358. .listxattr = ext2_listxattr,
  359. #endif
  360. .setattr = ext2_setattr,
  361. .get_acl = ext2_get_acl,
  362. .set_acl = ext2_set_acl,
  363. .tmpfile = ext2_tmpfile,
  364. };
  365. const struct inode_operations ext2_special_inode_operations = {
  366. #ifdef CONFIG_EXT2_FS_XATTR
  367. .listxattr = ext2_listxattr,
  368. #endif
  369. .setattr = ext2_setattr,
  370. .get_acl = ext2_get_acl,
  371. .set_acl = ext2_set_acl,
  372. };