inode.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) International Business Machines Corp., 2000-2004
  4. * Portions Copyright (C) Christoph Hellwig, 2001-2002
  5. */
  6. #include <linux/fs.h>
  7. #include <linux/mpage.h>
  8. #include <linux/buffer_head.h>
  9. #include <linux/pagemap.h>
  10. #include <linux/quotaops.h>
  11. #include <linux/uio.h>
  12. #include <linux/writeback.h>
  13. #include "jfs_incore.h"
  14. #include "jfs_inode.h"
  15. #include "jfs_filsys.h"
  16. #include "jfs_imap.h"
  17. #include "jfs_extent.h"
  18. #include "jfs_unicode.h"
  19. #include "jfs_debug.h"
  20. #include "jfs_dmap.h"
  21. struct inode *jfs_iget(struct super_block *sb, unsigned long ino)
  22. {
  23. struct inode *inode;
  24. int ret;
  25. inode = iget_locked(sb, ino);
  26. if (!inode)
  27. return ERR_PTR(-ENOMEM);
  28. if (!(inode_state_read_once(inode) & I_NEW))
  29. return inode;
  30. ret = diRead(inode);
  31. if (ret < 0) {
  32. iget_failed(inode);
  33. return ERR_PTR(ret);
  34. }
  35. if (S_ISREG(inode->i_mode)) {
  36. inode->i_op = &jfs_file_inode_operations;
  37. inode->i_fop = &jfs_file_operations;
  38. inode->i_mapping->a_ops = &jfs_aops;
  39. } else if (S_ISDIR(inode->i_mode)) {
  40. inode->i_op = &jfs_dir_inode_operations;
  41. inode->i_fop = &jfs_dir_operations;
  42. } else if (S_ISLNK(inode->i_mode)) {
  43. if (inode->i_size >= IDATASIZE) {
  44. inode->i_op = &page_symlink_inode_operations;
  45. inode_nohighmem(inode);
  46. inode->i_mapping->a_ops = &jfs_aops;
  47. } else {
  48. inode->i_op = &jfs_fast_symlink_inode_operations;
  49. inode->i_link = JFS_IP(inode)->i_inline;
  50. /*
  51. * The inline data should be null-terminated, but
  52. * don't let on-disk corruption crash the kernel
  53. */
  54. inode->i_link[inode->i_size] = '\0';
  55. }
  56. } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
  57. S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  58. inode->i_op = &jfs_file_inode_operations;
  59. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  60. } else {
  61. printk(KERN_DEBUG "JFS: Invalid file type 0%04o for inode %lu.\n",
  62. inode->i_mode, inode->i_ino);
  63. iget_failed(inode);
  64. return ERR_PTR(-EIO);
  65. }
  66. unlock_new_inode(inode);
  67. return inode;
  68. }
  69. /*
  70. * Workhorse of both fsync & write_inode
  71. */
  72. int jfs_commit_inode(struct inode *inode, int wait)
  73. {
  74. int rc = 0;
  75. tid_t tid;
  76. static int noisy = 5;
  77. jfs_info("In jfs_commit_inode, inode = 0x%p", inode);
  78. /*
  79. * Don't commit if inode has been committed since last being
  80. * marked dirty, or if it has been deleted.
  81. */
  82. if (inode->i_nlink == 0 || !test_cflag(COMMIT_Dirty, inode))
  83. return 0;
  84. if (isReadOnly(inode)) {
  85. /* kernel allows writes to devices on read-only
  86. * partitions and may think inode is dirty
  87. */
  88. if (!special_file(inode->i_mode) && noisy) {
  89. jfs_err("jfs_commit_inode(0x%p) called on read-only volume",
  90. inode);
  91. jfs_err("Is remount racy?");
  92. noisy--;
  93. }
  94. return 0;
  95. }
  96. tid = txBegin(inode->i_sb, COMMIT_INODE);
  97. mutex_lock(&JFS_IP(inode)->commit_mutex);
  98. /*
  99. * Retest inode state after taking commit_mutex
  100. */
  101. if (inode->i_nlink && test_cflag(COMMIT_Dirty, inode))
  102. rc = txCommit(tid, 1, &inode, wait ? COMMIT_SYNC : 0);
  103. txEnd(tid);
  104. mutex_unlock(&JFS_IP(inode)->commit_mutex);
  105. return rc;
  106. }
  107. int jfs_write_inode(struct inode *inode, struct writeback_control *wbc)
  108. {
  109. int wait = wbc->sync_mode == WB_SYNC_ALL;
  110. if (inode->i_nlink == 0)
  111. return 0;
  112. /*
  113. * If COMMIT_DIRTY is not set, the inode isn't really dirty.
  114. * It has been committed since the last change, but was still
  115. * on the dirty inode list.
  116. */
  117. if (!test_cflag(COMMIT_Dirty, inode)) {
  118. /* Make sure committed changes hit the disk */
  119. jfs_flush_journal(JFS_SBI(inode->i_sb)->log, wait);
  120. return 0;
  121. }
  122. if (jfs_commit_inode(inode, wait)) {
  123. jfs_err("jfs_write_inode: jfs_commit_inode failed!");
  124. return -EIO;
  125. } else
  126. return 0;
  127. }
  128. void jfs_evict_inode(struct inode *inode)
  129. {
  130. struct jfs_inode_info *ji = JFS_IP(inode);
  131. jfs_info("In jfs_evict_inode, inode = 0x%p", inode);
  132. if (!inode->i_nlink && !is_bad_inode(inode)) {
  133. dquot_initialize(inode);
  134. truncate_inode_pages_final(&inode->i_data);
  135. if (JFS_IP(inode)->fileset == FILESYSTEM_I) {
  136. struct inode *ipimap = JFS_SBI(inode->i_sb)->ipimap;
  137. if (test_cflag(COMMIT_Freewmap, inode))
  138. jfs_free_zero_link(inode);
  139. if (ipimap && JFS_IP(ipimap)->i_imap)
  140. diFree(inode);
  141. /*
  142. * Free the inode from the quota allocation.
  143. */
  144. dquot_free_inode(inode);
  145. }
  146. } else {
  147. truncate_inode_pages_final(&inode->i_data);
  148. }
  149. clear_inode(inode);
  150. dquot_drop(inode);
  151. BUG_ON(!list_empty(&ji->anon_inode_list));
  152. spin_lock_irq(&ji->ag_lock);
  153. if (ji->active_ag != -1) {
  154. struct bmap *bmap = JFS_SBI(inode->i_sb)->bmap;
  155. atomic_dec(&bmap->db_active[ji->active_ag]);
  156. ji->active_ag = -1;
  157. }
  158. spin_unlock_irq(&ji->ag_lock);
  159. }
  160. void jfs_dirty_inode(struct inode *inode, int flags)
  161. {
  162. static int noisy = 5;
  163. if (isReadOnly(inode)) {
  164. if (!special_file(inode->i_mode) && noisy) {
  165. /* kernel allows writes to devices on read-only
  166. * partitions and may try to mark inode dirty
  167. */
  168. jfs_err("jfs_dirty_inode called on read-only volume");
  169. jfs_err("Is remount racy?");
  170. noisy--;
  171. }
  172. return;
  173. }
  174. set_cflag(COMMIT_Dirty, inode);
  175. }
  176. int jfs_get_block(struct inode *ip, sector_t lblock,
  177. struct buffer_head *bh_result, int create)
  178. {
  179. s64 lblock64 = lblock;
  180. int rc = 0;
  181. xad_t xad;
  182. s64 xaddr;
  183. int xflag;
  184. s32 xlen = bh_result->b_size >> ip->i_blkbits;
  185. /*
  186. * Take appropriate lock on inode
  187. */
  188. if (create)
  189. IWRITE_LOCK(ip, RDWRLOCK_NORMAL);
  190. else
  191. IREAD_LOCK(ip, RDWRLOCK_NORMAL);
  192. if (((lblock64 << ip->i_sb->s_blocksize_bits) < ip->i_size) &&
  193. (!xtLookup(ip, lblock64, xlen, &xflag, &xaddr, &xlen, 0)) &&
  194. xaddr) {
  195. if (xflag & XAD_NOTRECORDED) {
  196. if (!create)
  197. /*
  198. * Allocated but not recorded, read treats
  199. * this as a hole
  200. */
  201. goto unlock;
  202. XADoffset(&xad, lblock64);
  203. XADlength(&xad, xlen);
  204. XADaddress(&xad, xaddr);
  205. rc = extRecord(ip, &xad);
  206. if (rc)
  207. goto unlock;
  208. set_buffer_new(bh_result);
  209. }
  210. map_bh(bh_result, ip->i_sb, xaddr);
  211. bh_result->b_size = xlen << ip->i_blkbits;
  212. goto unlock;
  213. }
  214. if (!create)
  215. goto unlock;
  216. /*
  217. * Allocate a new block
  218. */
  219. if ((rc = extHint(ip, lblock64 << ip->i_sb->s_blocksize_bits, &xad)))
  220. goto unlock;
  221. rc = extAlloc(ip, xlen, lblock64, &xad, false);
  222. if (rc)
  223. goto unlock;
  224. set_buffer_new(bh_result);
  225. map_bh(bh_result, ip->i_sb, addressXAD(&xad));
  226. bh_result->b_size = lengthXAD(&xad) << ip->i_blkbits;
  227. unlock:
  228. /*
  229. * Release lock on inode
  230. */
  231. if (create)
  232. IWRITE_UNLOCK(ip);
  233. else
  234. IREAD_UNLOCK(ip);
  235. return rc;
  236. }
  237. static int jfs_writepages(struct address_space *mapping,
  238. struct writeback_control *wbc)
  239. {
  240. return mpage_writepages(mapping, wbc, jfs_get_block);
  241. }
  242. static int jfs_read_folio(struct file *file, struct folio *folio)
  243. {
  244. return mpage_read_folio(folio, jfs_get_block);
  245. }
  246. static void jfs_readahead(struct readahead_control *rac)
  247. {
  248. mpage_readahead(rac, jfs_get_block);
  249. }
  250. static void jfs_write_failed(struct address_space *mapping, loff_t to)
  251. {
  252. struct inode *inode = mapping->host;
  253. if (to > inode->i_size) {
  254. truncate_pagecache(inode, inode->i_size);
  255. jfs_truncate(inode);
  256. }
  257. }
  258. static int jfs_write_begin(const struct kiocb *iocb,
  259. struct address_space *mapping,
  260. loff_t pos, unsigned len,
  261. struct folio **foliop, void **fsdata)
  262. {
  263. int ret;
  264. ret = block_write_begin(mapping, pos, len, foliop, jfs_get_block);
  265. if (unlikely(ret))
  266. jfs_write_failed(mapping, pos + len);
  267. return ret;
  268. }
  269. static int jfs_write_end(const struct kiocb *iocb,
  270. struct address_space *mapping,
  271. loff_t pos, unsigned len, unsigned copied,
  272. struct folio *folio, void *fsdata)
  273. {
  274. int ret;
  275. ret = generic_write_end(iocb, mapping, pos, len, copied, folio, fsdata);
  276. if (ret < len)
  277. jfs_write_failed(mapping, pos + len);
  278. return ret;
  279. }
  280. static sector_t jfs_bmap(struct address_space *mapping, sector_t block)
  281. {
  282. return generic_block_bmap(mapping, block, jfs_get_block);
  283. }
  284. static ssize_t jfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
  285. {
  286. struct file *file = iocb->ki_filp;
  287. struct address_space *mapping = file->f_mapping;
  288. struct inode *inode = file->f_mapping->host;
  289. size_t count = iov_iter_count(iter);
  290. ssize_t ret;
  291. ret = blockdev_direct_IO(iocb, inode, iter, jfs_get_block);
  292. /*
  293. * In case of error extending write may have instantiated a few
  294. * blocks outside i_size. Trim these off again.
  295. */
  296. if (unlikely(iov_iter_rw(iter) == WRITE && ret < 0)) {
  297. loff_t isize = i_size_read(inode);
  298. loff_t end = iocb->ki_pos + count;
  299. if (end > isize)
  300. jfs_write_failed(mapping, end);
  301. }
  302. return ret;
  303. }
  304. const struct address_space_operations jfs_aops = {
  305. .dirty_folio = block_dirty_folio,
  306. .invalidate_folio = block_invalidate_folio,
  307. .read_folio = jfs_read_folio,
  308. .readahead = jfs_readahead,
  309. .writepages = jfs_writepages,
  310. .write_begin = jfs_write_begin,
  311. .write_end = jfs_write_end,
  312. .bmap = jfs_bmap,
  313. .direct_IO = jfs_direct_IO,
  314. .migrate_folio = buffer_migrate_folio,
  315. };
  316. /*
  317. * Guts of jfs_truncate. Called with locks already held. Can be called
  318. * with directory for truncating directory index table.
  319. */
  320. void jfs_truncate_nolock(struct inode *ip, loff_t length)
  321. {
  322. loff_t newsize;
  323. tid_t tid;
  324. ASSERT(length >= 0);
  325. if (test_cflag(COMMIT_Nolink, ip) || isReadOnly(ip)) {
  326. xtTruncate(0, ip, length, COMMIT_WMAP);
  327. return;
  328. }
  329. do {
  330. tid = txBegin(ip->i_sb, 0);
  331. /*
  332. * The commit_mutex cannot be taken before txBegin.
  333. * txBegin may block and there is a chance the inode
  334. * could be marked dirty and need to be committed
  335. * before txBegin unblocks
  336. */
  337. mutex_lock(&JFS_IP(ip)->commit_mutex);
  338. newsize = xtTruncate(tid, ip, length,
  339. COMMIT_TRUNCATE | COMMIT_PWMAP);
  340. if (newsize < 0) {
  341. txEnd(tid);
  342. mutex_unlock(&JFS_IP(ip)->commit_mutex);
  343. break;
  344. }
  345. inode_set_mtime_to_ts(ip, inode_set_ctime_current(ip));
  346. mark_inode_dirty(ip);
  347. txCommit(tid, 1, &ip, 0);
  348. txEnd(tid);
  349. mutex_unlock(&JFS_IP(ip)->commit_mutex);
  350. } while (newsize > length); /* Truncate isn't always atomic */
  351. }
  352. void jfs_truncate(struct inode *ip)
  353. {
  354. jfs_info("jfs_truncate: size = 0x%lx", (ulong) ip->i_size);
  355. block_truncate_page(ip->i_mapping, ip->i_size, jfs_get_block);
  356. IWRITE_LOCK(ip, RDWRLOCK_NORMAL);
  357. jfs_truncate_nolock(ip, ip->i_size);
  358. IWRITE_UNLOCK(ip);
  359. }