inode.c 9.6 KB

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