inode.c 9.0 KB

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