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_dmap.h"
25 #include "jfs_imap.h"
26 #include "jfs_extent.h"
27 #include "jfs_unicode.h"
28 #include "jfs_debug.h"
29
30
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 *);
38
jfs_clear_inode(struct inode * inode)39 void jfs_clear_inode(struct inode *inode)
40 {
41 struct jfs_inode_info *ji = JFS_IP(inode);
42
43 if (is_bad_inode(inode))
44 /*
45 * We free the fs-dependent structure before making the
46 * inode bad
47 */
48 return;
49
50 jfs_info("jfs_clear_inode called ip = 0x%p", inode);
51
52 spin_lock_irq(&ji->ag_lock);
53 if (ji->active_ag != -1) {
54 struct bmap *bmap = JFS_SBI(inode->i_sb)->bmap;
55 atomic_dec(&bmap->db_active[ji->active_ag]);
56 ji->active_ag = -1;
57 }
58 spin_unlock_irq(&ji->ag_lock);
59
60 ASSERT(list_empty(&ji->anon_inode_list));
61
62 if (ji->atlhead) {
63 jfs_err("jfs_clear_inode: inode %p has anonymous tlocks",
64 inode);
65 jfs_err("i_state = 0x%lx, cflag = 0x%lx", inode->i_state,
66 ji->cflag);
67 }
68
69 free_jfs_inode(inode);
70 }
71
jfs_read_inode(struct inode * inode)72 void jfs_read_inode(struct inode *inode)
73 {
74 int rc;
75
76 rc = alloc_jfs_inode(inode);
77 if (rc) {
78 jfs_warn("In jfs_read_inode, alloc_jfs_inode failed");
79 goto bad_inode;
80 }
81 jfs_info("In jfs_read_inode, inode = 0x%p", inode);
82
83 if (diRead(inode))
84 goto bad_inode_free;
85
86 if (S_ISREG(inode->i_mode)) {
87 inode->i_op = &jfs_file_inode_operations;
88 inode->i_fop = &jfs_file_operations;
89 inode->i_mapping->a_ops = &jfs_aops;
90 } else if (S_ISDIR(inode->i_mode)) {
91 inode->i_op = &jfs_dir_inode_operations;
92 inode->i_fop = &jfs_dir_operations;
93 } else if (S_ISLNK(inode->i_mode)) {
94 if (inode->i_size >= IDATASIZE) {
95 inode->i_op = &page_symlink_inode_operations;
96 inode->i_mapping->a_ops = &jfs_aops;
97 } else
98 inode->i_op = &jfs_symlink_inode_operations;
99 } else {
100 inode->i_op = &jfs_file_inode_operations;
101 init_special_inode(inode, inode->i_mode,
102 kdev_t_to_nr(inode->i_rdev));
103 }
104
105 return;
106
107 bad_inode_free:
108 free_jfs_inode(inode);
109 bad_inode:
110 make_bad_inode(inode);
111 }
112
113 /* This define is from fs/open.c */
114 #define special_file(m) (S_ISCHR(m)||S_ISBLK(m)||S_ISFIFO(m)||S_ISSOCK(m))
115
116 /*
117 * Workhorse of both fsync & write_inode
118 */
jfs_commit_inode(struct inode * inode,int wait)119 int jfs_commit_inode(struct inode *inode, int wait)
120 {
121 int rc = 0;
122 tid_t tid;
123 static int noisy = 5;
124
125 jfs_info("In jfs_commit_inode, inode = 0x%p", inode);
126
127 /*
128 * Don't commit if inode has been committed since last being
129 * marked dirty, or if it has been deleted.
130 */
131 if (inode->i_nlink == 0 || !test_cflag(COMMIT_Dirty, inode))
132 return 0;
133
134 if (isReadOnly(inode)) {
135 /* kernel allows writes to devices on read-only
136 * partitions and may think inode is dirty
137 */
138 if (!special_file(inode->i_mode) && noisy) {
139 jfs_err("jfs_commit_inode(0x%p) called on "
140 "read-only volume", inode);
141 jfs_err("Is remount racy?");
142 noisy--;
143 }
144 return 0;
145 }
146
147 tid = txBegin(inode->i_sb, COMMIT_INODE);
148 down(&JFS_IP(inode)->commit_sem);
149
150 /*
151 * Retest inode state after taking commit_sem
152 */
153 if (inode->i_nlink && test_cflag(COMMIT_Dirty, inode))
154 rc = txCommit(tid, 1, &inode, wait ? COMMIT_SYNC : 0);
155
156 txEnd(tid);
157 up(&JFS_IP(inode)->commit_sem);
158 return rc;
159 }
160
jfs_write_inode(struct inode * inode,int wait)161 void jfs_write_inode(struct inode *inode, int wait)
162 {
163 if (test_cflag(COMMIT_Nolink, inode))
164 return;
165 /*
166 * If COMMIT_DIRTY is not set, the inode isn't really dirty.
167 * It has been committed since the last change, but was still
168 * on the dirty inode list.
169 */
170 if (!test_cflag(COMMIT_Dirty, inode)) {
171 /* Make sure committed changes hit the disk */
172 jfs_flush_journal(JFS_SBI(inode->i_sb)->log, wait);
173 return;
174 }
175
176 if (jfs_commit_inode(inode, wait)) {
177 jfs_err("jfs_write_inode: jfs_commit_inode failed!");
178 }
179 }
180
jfs_delete_inode(struct inode * inode)181 void jfs_delete_inode(struct inode *inode)
182 {
183 jfs_info("In jfs_delete_inode, inode = 0x%p", inode);
184
185 if (test_cflag(COMMIT_Freewmap, inode))
186 freeZeroLink(inode);
187
188 diFree(inode);
189
190 clear_inode(inode);
191 }
192
jfs_dirty_inode(struct inode * inode)193 void jfs_dirty_inode(struct inode *inode)
194 {
195 static int noisy = 5;
196
197 if (isReadOnly(inode)) {
198 if (!special_file(inode->i_mode) && noisy) {
199 /* kernel allows writes to devices on read-only
200 * partitions and may try to mark inode dirty
201 */
202 jfs_err("jfs_dirty_inode called on read-only volume");
203 jfs_err("Is remount racy?");
204 noisy--;
205 }
206 return;
207 }
208
209 set_cflag(COMMIT_Dirty, inode);
210 }
211
jfs_get_block(struct inode * ip,long lblock,struct buffer_head * bh_result,int create)212 static int jfs_get_block(struct inode *ip, long lblock,
213 struct buffer_head *bh_result, int create)
214 {
215 s64 lblock64 = lblock;
216 int rc = 0;
217 int take_locks;
218 xad_t xad;
219 s64 xaddr;
220 int xflag;
221 s32 xlen;
222
223 /*
224 * If this is a special inode (imap, dmap)
225 * the lock should already be taken
226 */
227 take_locks = (JFS_IP(ip)->fileset != AGGREGATE_I);
228
229 /*
230 * Take appropriate lock on inode
231 */
232 if (take_locks) {
233 if (create)
234 IWRITE_LOCK(ip);
235 else
236 IREAD_LOCK(ip);
237 }
238
239 if (((lblock64 << ip->i_sb->s_blocksize_bits) < ip->i_size) &&
240 (xtLookup(ip, lblock64, 1, &xflag, &xaddr, &xlen, 0) == 0) &&
241 xlen) {
242 if (xflag & XAD_NOTRECORDED) {
243 if (!create)
244 /*
245 * Allocated but not recorded, read treats
246 * this as a hole
247 */
248 goto unlock;
249 #ifdef _JFS_4K
250 XADoffset(&xad, lblock64);
251 XADlength(&xad, xlen);
252 XADaddress(&xad, xaddr);
253 #else /* _JFS_4K */
254 /*
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?
258 */
259 BUG();
260 #endif /* _JFS_4K */
261 rc = extRecord(ip, &xad);
262 if (rc)
263 goto unlock;
264 bh_result->b_state |= (1UL << BH_New);
265 }
266
267 bh_result->b_dev = ip->i_dev;
268 bh_result->b_blocknr = xaddr;
269 bh_result->b_state |= (1UL << BH_Mapped);
270 goto unlock;
271 }
272 if (!create)
273 goto unlock;
274
275 /*
276 * Allocate a new block
277 */
278 #ifdef _JFS_4K
279 if ((rc = extHint(ip, lblock64 << ip->i_sb->s_blocksize_bits, &xad)))
280 goto unlock;
281 rc = extAlloc(ip, 1, lblock64, &xad, FALSE);
282 if (rc)
283 goto unlock;
284
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));
288
289 #else /* _JFS_4K */
290 /*
291 * We need to do whatever it takes to keep all but the last buffers
292 * in 4K pages - see jfs_write.c
293 */
294 BUG();
295 #endif /* _JFS_4K */
296
297 unlock:
298 /*
299 * Release lock on inode
300 */
301 if (take_locks) {
302 if (create)
303 IWRITE_UNLOCK(ip);
304 else
305 IREAD_UNLOCK(ip);
306 }
307 return rc;
308 }
309
jfs_writepage(struct page * page)310 static int jfs_writepage(struct page *page)
311 {
312 return block_write_full_page(page, jfs_get_block);
313 }
314
jfs_readpage(struct file * file,struct page * page)315 static int jfs_readpage(struct file *file, struct page *page)
316 {
317 return block_read_full_page(page, jfs_get_block);
318 }
319
jfs_prepare_write(struct file * file,struct page * page,unsigned from,unsigned to)320 static int jfs_prepare_write(struct file *file,
321 struct page *page, unsigned from, unsigned to)
322 {
323 return block_prepare_write(page, from, to, jfs_get_block);
324 }
325
jfs_bmap(struct address_space * mapping,long block)326 static int jfs_bmap(struct address_space *mapping, long block)
327 {
328 return generic_block_bmap(mapping, block, jfs_get_block);
329 }
330
jfs_direct_IO(int rw,struct inode * inode,struct kiobuf * iobuf,unsigned long blocknr,int blocksize)331 static int jfs_direct_IO(int rw, struct inode *inode, struct kiobuf *iobuf,
332 unsigned long blocknr, int blocksize)
333 {
334 return generic_direct_IO(rw, inode, iobuf, blocknr,
335 blocksize, jfs_get_block);
336 }
337
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,
344 .bmap = jfs_bmap,
345 .direct_IO = jfs_direct_IO,
346 };
347
348 /*
349 * Guts of jfs_truncate. Called with locks already held. Can be called
350 * with directory for truncating directory index table.
351 */
jfs_truncate_nolock(struct inode * ip,loff_t length)352 void jfs_truncate_nolock(struct inode *ip, loff_t length)
353 {
354 loff_t newsize;
355 tid_t tid;
356
357 ASSERT(length >= 0);
358
359 if (test_cflag(COMMIT_Nolink, ip)) {
360 xtTruncate(0, ip, length, COMMIT_WMAP);
361 return;
362 }
363
364 do {
365 tid = txBegin(ip->i_sb, 0);
366
367 /*
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
372 */
373 down(&JFS_IP(ip)->commit_sem);
374
375 newsize = xtTruncate(tid, ip, length,
376 COMMIT_TRUNCATE | COMMIT_PWMAP);
377 if (newsize < 0) {
378 txEnd(tid);
379 up(&JFS_IP(ip)->commit_sem);
380 break;
381 }
382
383 ip->i_mtime = ip->i_ctime = CURRENT_TIME;
384 mark_inode_dirty(ip);
385
386 txCommit(tid, 1, &ip, 0);
387 txEnd(tid);
388 up(&JFS_IP(ip)->commit_sem);
389 } while (newsize > length); /* Truncate isn't always atomic */
390 }
391
jfs_truncate(struct inode * ip)392 void jfs_truncate(struct inode *ip)
393 {
394 jfs_info("jfs_truncate: size = 0x%lx", (ulong) ip->i_size);
395
396 block_truncate_page(ip->i_mapping, ip->i_size, jfs_get_block);
397
398 IWRITE_LOCK(ip);
399 jfs_truncate_nolock(ip, ip->i_size);
400 IWRITE_UNLOCK(ip);
401 }
402