2 * Copyright (c) International Business Machines Corp., 2000-2002
3 * Portions Copyright (c) Christoph Hellwig, 2001-2002
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.
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.
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
21 //#include <linux/locks.h>
22 #include "jfs_incore.h"
23 #include "jfs_filsys.h"
26 #include "jfs_extent.h"
27 #include "jfs_unicode.h"
28 #include "jfs_debug.h"
31 extern struct inode_operations jfs_dir_inode_operations;
32 extern struct inode_operations jfs_file_inode_operations;
33 extern struct inode_operations jfs_symlink_inode_operations;
34 extern struct file_operations jfs_dir_operations;
35 extern struct file_operations jfs_file_operations;
36 struct address_space_operations jfs_aops;
37 extern int freeZeroLink(struct inode *);
39 void jfs_clear_inode(struct inode *inode)
41 struct jfs_inode_info *ji = JFS_IP(inode);
43 if (is_bad_inode(inode))
45 * We free the fs-dependent structure before making the
50 jFYI(1, ("jfs_clear_inode called ip = 0x%p\n", inode));
52 if (ji->active_ag != -1) {
53 struct bmap *bmap = JFS_SBI(inode->i_sb)->bmap;
54 atomic_dec(&bmap->db_active[ji->active_ag]);
57 ASSERT(list_empty(&ji->anon_inode_list));
60 jERROR(1, ("jfs_clear_inode: inode %p has anonymous tlocks\n",
62 jERROR(1, ("i_state = 0x%lx, cflag = 0x%lx\n",
63 inode->i_state, ji->cflag));
66 free_jfs_inode(inode);
69 void jfs_read_inode(struct inode *inode)
73 rc = alloc_jfs_inode(inode);
75 jFYI(1, ("In jfs_read_inode, alloc_jfs_inode failed"));
78 jFYI(1, ("In jfs_read_inode, inode = 0x%p\n", inode));
83 if (S_ISREG(inode->i_mode)) {
84 inode->i_op = &jfs_file_inode_operations;
85 inode->i_fop = &jfs_file_operations;
86 inode->i_mapping->a_ops = &jfs_aops;
87 } else if (S_ISDIR(inode->i_mode)) {
88 inode->i_op = &jfs_dir_inode_operations;
89 inode->i_fop = &jfs_dir_operations;
90 inode->i_mapping->a_ops = &jfs_aops;
91 inode->i_mapping->gfp_mask = GFP_NOFS;
92 } else if (S_ISLNK(inode->i_mode)) {
93 if (inode->i_size >= IDATASIZE) {
94 inode->i_op = &page_symlink_inode_operations;
95 inode->i_mapping->a_ops = &jfs_aops;
97 inode->i_op = &jfs_symlink_inode_operations;
99 inode->i_op = &jfs_file_inode_operations;
100 init_special_inode(inode, inode->i_mode,
101 kdev_t_to_nr(inode->i_rdev));
107 free_jfs_inode(inode);
109 make_bad_inode(inode);
112 /* This define is from fs/open.c */
113 #define special_file(m) (S_ISCHR(m)||S_ISBLK(m)||S_ISFIFO(m)||S_ISSOCK(m))
116 * Workhorse of both fsync & write_inode
118 int jfs_commit_inode(struct inode *inode, int wait)
122 static int noisy = 5;
124 jFYI(1, ("In jfs_commit_inode, inode = 0x%p\n", inode));
127 * Don't commit if inode has been committed since last being
128 * marked dirty, or if it has been deleted.
130 if (test_cflag(COMMIT_Nolink, inode) ||
131 !test_cflag(COMMIT_Dirty, inode))
134 if (isReadOnly(inode)) {
135 /* kernel allows writes to devices on read-only
136 * partitions and may think inode is dirty
138 if (!special_file(inode->i_mode) && noisy) {
139 jERROR(1, ("jfs_commit_inode(0x%p) called on "
140 "read-only volume\n", inode));
141 jERROR(1, ("Is remount racy?\n"));
147 tid = txBegin(inode->i_sb, COMMIT_INODE);
148 down(&JFS_IP(inode)->commit_sem);
149 rc = txCommit(tid, 1, &inode, wait ? COMMIT_SYNC : 0);
151 up(&JFS_IP(inode)->commit_sem);
155 void jfs_write_inode(struct inode *inode, int wait)
158 * If COMMIT_DIRTY is not set, the inode isn't really dirty.
159 * It has been committed since the last change, but was still
160 * on the dirty inode list
162 if (test_cflag(COMMIT_Nolink, inode) ||
163 !test_cflag(COMMIT_Dirty, inode))
166 if (jfs_commit_inode(inode, wait)) {
167 jERROR(1, ("jfs_write_inode: jfs_commit_inode failed!\n"));
171 void jfs_delete_inode(struct inode *inode)
173 jFYI(1, ("In jfs_delete_inode, inode = 0x%p\n", inode));
175 if (test_cflag(COMMIT_Freewmap, inode))
183 void jfs_dirty_inode(struct inode *inode)
185 static int noisy = 5;
187 if (isReadOnly(inode)) {
188 if (!special_file(inode->i_mode) && noisy) {
189 /* kernel allows writes to devices on read-only
190 * partitions and may try to mark inode dirty
192 jERROR(1, ("jfs_dirty_inode called on "
193 "read-only volume\n"));
194 jERROR(1, ("Is remount racy?\n"));
200 set_cflag(COMMIT_Dirty, inode);
203 static int jfs_get_block(struct inode *ip, long lblock,
204 struct buffer_head *bh_result, int create)
206 s64 lblock64 = lblock;
207 int no_size_check = 0;
216 * If this is a special inode (imap, dmap) or directory,
217 * the lock should already be taken
219 take_locks = ((JFS_IP(ip)->fileset != AGGREGATE_I) &&
220 !S_ISDIR(ip->i_mode));
222 * Take appropriate lock on inode
232 * A directory's "data" is the inode index table, but i_size is the
233 * size of the d-tree, so don't check the offset against i_size
235 if (S_ISDIR(ip->i_mode))
238 if ((no_size_check ||
239 ((lblock64 << ip->i_sb->s_blocksize_bits) < ip->i_size)) &&
240 (xtLookup(ip, lblock64, 1, &xflag, &xaddr, &xlen, no_size_check)
242 if (xflag & XAD_NOTRECORDED) {
245 * Allocated but not recorded, read treats
250 XADoffset(&xad, lblock64);
251 XADlength(&xad, xlen);
252 XADaddress(&xad, xaddr);
255 * As long as block size = 4K, this isn't a problem.
256 * We should mark the whole page not ABNR, but how
257 * will we know to mark the other blocks BH_New?
261 rc = extRecord(ip, &xad);
264 bh_result->b_state |= (1UL << BH_New);
267 bh_result->b_dev = ip->i_dev;
268 bh_result->b_blocknr = xaddr;
269 bh_result->b_state |= (1UL << BH_Mapped);
276 * Allocate a new block
279 if ((rc = extHint(ip, lblock64 << ip->i_sb->s_blocksize_bits, &xad)))
281 rc = extAlloc(ip, 1, lblock64, &xad, FALSE);
285 bh_result->b_dev = ip->i_dev;
286 bh_result->b_blocknr = addressXAD(&xad);
287 bh_result->b_state |= ((1UL << BH_Mapped) | (1UL << BH_New));
291 * We need to do whatever it takes to keep all but the last buffers
292 * in 4K pages - see jfs_write.c
299 * Release lock on inode
310 static int jfs_writepage(struct page *page)
312 return block_write_full_page(page, jfs_get_block);
315 static int jfs_readpage(struct file *file, struct page *page)
317 return block_read_full_page(page, jfs_get_block);
320 static int jfs_prepare_write(struct file *file,
321 struct page *page, unsigned from, unsigned to)
323 return block_prepare_write(page, from, to, jfs_get_block);
326 static int jfs_bmap(struct address_space *mapping, long block)
328 return generic_block_bmap(mapping, block, jfs_get_block);
331 static int jfs_direct_IO(int rw, struct inode *inode, struct kiobuf *iobuf,
332 unsigned long blocknr, int blocksize)
334 return generic_direct_IO(rw, inode, iobuf, blocknr,
335 blocksize, jfs_get_block);
338 struct address_space_operations jfs_aops = {
339 .readpage = jfs_readpage,
340 .writepage = jfs_writepage,
341 .sync_page = block_sync_page,
342 .prepare_write = jfs_prepare_write,
343 .commit_write = generic_commit_write,
345 .direct_IO = jfs_direct_IO,
349 * Guts of jfs_truncate. Called with locks already held. Can be called
350 * with directory for truncating directory index table.
352 void jfs_truncate_nolock(struct inode *ip, loff_t length)
359 if (test_cflag(COMMIT_Nolink, ip)) {
360 xtTruncate(0, ip, length, COMMIT_WMAP);
365 tid = txBegin(ip->i_sb, 0);
368 * The commit_sem cannot be taken before txBegin.
369 * txBegin may block and there is a chance the inode
370 * could be marked dirty and need to be committed
371 * before txBegin unblocks
373 down(&JFS_IP(ip)->commit_sem);
375 newsize = xtTruncate(tid, ip, length,
376 COMMIT_TRUNCATE | COMMIT_PWMAP);
379 up(&JFS_IP(ip)->commit_sem);
383 ip->i_mtime = ip->i_ctime = CURRENT_TIME;
384 mark_inode_dirty(ip);
386 txCommit(tid, 1, &ip, 0);
388 up(&JFS_IP(ip)->commit_sem);
389 } while (newsize > length); /* Truncate isn't always atomic */
392 void jfs_truncate(struct inode *ip)
394 jFYI(1, ("jfs_truncate: size = 0x%lx\n", (ulong) ip->i_size));
396 block_truncate_page(ip->i_mapping, ip->i_size, jfs_get_block);
399 jfs_truncate_nolock(ip, ip->i_size);