inode.c 10 KB

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