inode.c 9.3 KB

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