namei.c 9.4 KB

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