inode.c 8.7 KB

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