inode.c 9.9 KB

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