1/*
2 *   Copyright (c) International Business Machines Corp., 2000-2002
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
20#include <linux/fs.h>
21//#include <linux/locks.h>
22#include "jfs_incore.h"
23#include "jfs_filsys.h"
24#include "jfs_imap.h"
25#include "jfs_extent.h"
26#include "jfs_unicode.h"
27#include "jfs_debug.h"
28
29
30extern struct inode_operations jfs_dir_inode_operations;
31extern struct inode_operations jfs_file_inode_operations;
32extern struct inode_operations jfs_symlink_inode_operations;
33extern struct file_operations jfs_dir_operations;
34extern struct file_operations jfs_file_operations;
35struct address_space_operations jfs_aops;
36extern int freeZeroLink(struct inode *);
37
38void jfs_clear_inode(struct inode *inode)
39{
40	struct jfs_inode_info *ji = JFS_IP(inode);
41
42	jFYI(1, ("jfs_clear_inode called ip = 0x%p\n", inode));
43
44	if (ji->active_ag != -1) {
45		printk(KERN_ERR "jfs_clear_inode, active_ag = %d\n",
46		       ji->active_ag);
47		printk(KERN_ERR "i_ino = %ld, i_mode = %o\n",
48		       inode->i_ino, inode->i_mode);
49	}
50
51	ASSERT(list_empty(&ji->mp_list));
52	ASSERT(list_empty(&ji->anon_inode_list));
53
54	if (ji->atlhead) {
55		jERROR(1, ("jfs_clear_inode: inode %p has anonymous tlocks\n",
56					inode));
57		jERROR(1, ("i_state = 0x%lx, cflag = 0x%lx\n",
58					inode->i_state, ji->cflag));
59	}
60
61	free_jfs_inode(inode);
62}
63
64void jfs_read_inode(struct inode *inode)
65{
66	int rc;
67
68	rc = alloc_jfs_inode(inode);
69	if (rc) {
70		jFYI(1, ("In jfs_read_inode, alloc_jfs_inode failed"));
71		goto bad_inode;
72	}
73	jFYI(1, ("In jfs_read_inode, inode = 0x%p\n", inode));
74
75	if (diRead(inode))
76		goto bad_inode_free;
77
78	if (S_ISREG(inode->i_mode)) {
79		inode->i_op = &jfs_file_inode_operations;
80		inode->i_fop = &jfs_file_operations;
81		inode->i_mapping->a_ops = &jfs_aops;
82	} else if (S_ISDIR(inode->i_mode)) {
83		inode->i_op = &jfs_dir_inode_operations;
84		inode->i_fop = &jfs_dir_operations;
85		inode->i_mapping->a_ops = &jfs_aops;
86		inode->i_mapping->gfp_mask = GFP_NOFS;
87	} else if (S_ISLNK(inode->i_mode)) {
88		if (inode->i_size > IDATASIZE) {
89			inode->i_op = &page_symlink_inode_operations;
90			inode->i_mapping->a_ops = &jfs_aops;
91		} else
92			inode->i_op = &jfs_symlink_inode_operations;
93	} else {
94		inode->i_op = &jfs_file_inode_operations;
95		init_special_inode(inode, inode->i_mode,
96				   kdev_t_to_nr(inode->i_rdev));
97	}
98
99	return;
100
101      bad_inode_free:
102	free_jfs_inode(inode);
103      bad_inode:
104	make_bad_inode(inode);
105}
106
107/* This define is from fs/open.c */
108#define special_file(m) (S_ISCHR(m)||S_ISBLK(m)||S_ISFIFO(m)||S_ISSOCK(m))
109
110/*
111 * Workhorse of both fsync & write_inode
112 */
113int jfs_commit_inode(struct inode *inode, int wait)
114{
115	int rc = 0;
116	tid_t tid;
117	static int noisy = 5;
118
119	jFYI(1, ("In jfs_commit_inode, inode = 0x%p\n", inode));
120
121	/*
122	 * Don't commit if inode has been committed since last being
123	 * marked dirty, or if it has been deleted.
124	 */
125	if (test_cflag(COMMIT_Nolink, inode) ||
126	    !test_cflag(COMMIT_Dirty, inode))
127		return 0;
128
129	if (isReadOnly(inode)) {
130		/* kernel allows writes to devices on read-only
131		 * partitions and may think inode is dirty
132		 */
133		if (!special_file(inode->i_mode) && noisy) {
134			jERROR(1, ("jfs_commit_inode(0x%p) called on "
135				   "read-only volume\n", inode));
136			jERROR(1, ("Is remount racy?\n"));
137			noisy--;
138		}
139		return 0;
140	}
141
142	tid = txBegin(inode->i_sb, COMMIT_INODE);
143	down(&JFS_IP(inode)->commit_sem);
144	rc = txCommit(tid, 1, &inode, wait ? COMMIT_SYNC : 0);
145	txEnd(tid);
146	up(&JFS_IP(inode)->commit_sem);
147	return -rc;
148}
149
150void jfs_write_inode(struct inode *inode, int wait)
151{
152	/*
153	 * If COMMIT_DIRTY is not set, the inode isn't really dirty.
154	 * It has been committed since the last change, but was still
155	 * on the dirty inode list
156	 */
157	if (test_cflag(COMMIT_Nolink, inode) ||
158	    !test_cflag(COMMIT_Dirty, inode))
159		return;
160
161	if (jfs_commit_inode(inode, wait)) {
162		jERROR(1, ("jfs_write_inode: jfs_commit_inode failed!\n"));
163	}
164}
165
166void jfs_delete_inode(struct inode *inode)
167{
168	jFYI(1, ("In jfs_delete_inode, inode = 0x%p\n", inode));
169
170	if (test_cflag(COMMIT_Freewmap, inode))
171		freeZeroLink(inode);
172
173	diFree(inode);
174
175	clear_inode(inode);
176}
177
178void jfs_dirty_inode(struct inode *inode)
179{
180	static int noisy = 5;
181
182	if (isReadOnly(inode)) {
183		if (!special_file(inode->i_mode) && noisy) {
184			/* kernel allows writes to devices on read-only
185			 * partitions and may try to mark inode dirty
186			 */
187			jERROR(1, ("jfs_dirty_inode called on "
188				   "read-only volume\n"));
189			jERROR(1, ("Is remount racy?\n"));
190			noisy--;
191		}
192		return;
193	}
194
195	set_cflag(COMMIT_Dirty, inode);
196}
197
198static int jfs_get_block(struct inode *ip, long lblock,
199			 struct buffer_head *bh_result, int create)
200{
201	s64 lblock64 = lblock;
202	int no_size_check = 0;
203	int rc = 0;
204	int take_locks;
205	xad_t xad;
206	s64 xaddr;
207	int xflag;
208	s32 xlen;
209
210	/*
211	 * If this is a special inode (imap, dmap) or directory,
212	 * the lock should already be taken
213	 */
214	take_locks = ((JFS_IP(ip)->fileset != AGGREGATE_I) &&
215		      !S_ISDIR(ip->i_mode));
216	/*
217	 * Take appropriate lock on inode
218	 */
219	if (take_locks) {
220		if (create)
221			IWRITE_LOCK(ip);
222		else
223			IREAD_LOCK(ip);
224	}
225
226	/*
227	 * A directory's "data" is the inode index table, but i_size is the
228	 * size of the d-tree, so don't check the offset against i_size
229	 */
230	if (S_ISDIR(ip->i_mode))
231		no_size_check = 1;
232
233	if ((no_size_check ||
234	     ((lblock64 << ip->i_sb->s_blocksize_bits) < ip->i_size)) &&
235	    (xtLookup(ip, lblock64, 1, &xflag, &xaddr, &xlen, no_size_check)
236	     == 0) && xlen) {
237		if (xflag & XAD_NOTRECORDED) {
238			if (!create)
239				/*
240				 * Allocated but not recorded, read treats
241				 * this as a hole
242				 */
243				goto unlock;
244#ifdef _JFS_4K
245			XADoffset(&xad, lblock64);
246			XADlength(&xad, xlen);
247			XADaddress(&xad, xaddr);
248#else				/* _JFS_4K */
249			/*
250			 * As long as block size = 4K, this isn't a problem.
251			 * We should mark the whole page not ABNR, but how
252			 * will we know to mark the other blocks BH_New?
253			 */
254			BUG();
255#endif				/* _JFS_4K */
256			rc = extRecord(ip, &xad);
257			if (rc)
258				goto unlock;
259			bh_result->b_state |= (1UL << BH_New);
260		}
261
262		bh_result->b_dev = ip->i_dev;
263		bh_result->b_blocknr = xaddr;
264		bh_result->b_state |= (1UL << BH_Mapped);
265		goto unlock;
266	}
267	if (!create)
268		goto unlock;
269
270	/*
271	 * Allocate a new block
272	 */
273#ifdef _JFS_4K
274	if ((rc = extHint(ip, lblock64 << ip->i_sb->s_blocksize_bits, &xad)))
275		goto unlock;
276	rc = extAlloc(ip, 1, lblock64, &xad, FALSE);
277	if (rc)
278		goto unlock;
279
280	bh_result->b_dev = ip->i_dev;
281	bh_result->b_blocknr = addressXAD(&xad);
282	bh_result->b_state |= ((1UL << BH_Mapped) | (1UL << BH_New));
283
284#else				/* _JFS_4K */
285	/*
286	 * We need to do whatever it takes to keep all but the last buffers
287	 * in 4K pages - see jfs_write.c
288	 */
289	BUG();
290#endif				/* _JFS_4K */
291
292      unlock:
293	/*
294	 * Release lock on inode
295	 */
296	if (take_locks) {
297		if (create)
298			IWRITE_UNLOCK(ip);
299		else
300			IREAD_UNLOCK(ip);
301	}
302	return -rc;
303}
304
305static int jfs_writepage(struct page *page)
306{
307	return block_write_full_page(page, jfs_get_block);
308}
309
310static int jfs_readpage(struct file *file, struct page *page)
311{
312	return block_read_full_page(page, jfs_get_block);
313}
314
315static int jfs_prepare_write(struct file *file,
316			     struct page *page, unsigned from, unsigned to)
317{
318	return block_prepare_write(page, from, to, jfs_get_block);
319}
320
321static int jfs_bmap(struct address_space *mapping, long block)
322{
323	return generic_block_bmap(mapping, block, jfs_get_block);
324}
325
326static int jfs_direct_IO(int rw, struct inode *inode, struct kiobuf *iobuf,
327			 unsigned long blocknr, int blocksize)
328{
329	return generic_direct_IO(rw, inode, iobuf, blocknr,
330				 blocksize, jfs_get_block);
331}
332
333struct address_space_operations jfs_aops = {
334	.readpage	= jfs_readpage,
335	.writepage	= jfs_writepage,
336	.sync_page	= block_sync_page,
337	.prepare_write	= jfs_prepare_write,
338	.commit_write	= generic_commit_write,
339	.bmap		= jfs_bmap,
340	.direct_IO	= jfs_direct_IO,
341};
342