inode.c 9.4 KB

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