import of ftp.dlink.com/GPL/DSMG-600_reB/ppclinux.tar.gz
[linux-2.4.21-pre4.git] / fs / jfs / inode.c
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
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         jFYI(1, ("jfs_clear_inode called ip = 0x%p\n", inode));
51
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]);
55         }
56
57         ASSERT(list_empty(&ji->anon_inode_list));
58
59         if (ji->atlhead) {
60                 jERROR(1, ("jfs_clear_inode: inode %p has anonymous tlocks\n",
61                                         inode));
62                 jERROR(1, ("i_state = 0x%lx, cflag = 0x%lx\n",
63                                         inode->i_state, ji->cflag));
64         }
65
66         free_jfs_inode(inode);
67 }
68
69 void jfs_read_inode(struct inode *inode)
70 {
71         int rc;
72
73         rc = alloc_jfs_inode(inode);
74         if (rc) {
75                 jFYI(1, ("In jfs_read_inode, alloc_jfs_inode failed"));
76                 goto bad_inode;
77         }
78         jFYI(1, ("In jfs_read_inode, inode = 0x%p\n", inode));
79
80         if (diRead(inode))
81                 goto bad_inode_free;
82
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;
96                 } else
97                         inode->i_op = &jfs_symlink_inode_operations;
98         } else {
99                 inode->i_op = &jfs_file_inode_operations;
100                 init_special_inode(inode, inode->i_mode,
101                                    kdev_t_to_nr(inode->i_rdev));
102         }
103
104         return;
105
106       bad_inode_free:
107         free_jfs_inode(inode);
108       bad_inode:
109         make_bad_inode(inode);
110 }
111
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))
114
115 /*
116  * Workhorse of both fsync & write_inode
117  */
118 int jfs_commit_inode(struct inode *inode, int wait)
119 {
120         int rc = 0;
121         tid_t tid;
122         static int noisy = 5;
123
124         jFYI(1, ("In jfs_commit_inode, inode = 0x%p\n", inode));
125
126         /*
127          * Don't commit if inode has been committed since last being
128          * marked dirty, or if it has been deleted.
129          */
130         if (test_cflag(COMMIT_Nolink, inode) ||
131             !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                         jERROR(1, ("jfs_commit_inode(0x%p) called on "
140                                    "read-only volume\n", inode));
141                         jERROR(1, ("Is remount racy?\n"));
142                         noisy--;
143                 }
144                 return 0;
145         }
146
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);
150         txEnd(tid);
151         up(&JFS_IP(inode)->commit_sem);
152         return -rc;
153 }
154
155 void jfs_write_inode(struct inode *inode, int wait)
156 {
157         /*
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
161          */
162         if (test_cflag(COMMIT_Nolink, inode) ||
163             !test_cflag(COMMIT_Dirty, inode))
164                 return;
165
166         if (jfs_commit_inode(inode, wait)) {
167                 jERROR(1, ("jfs_write_inode: jfs_commit_inode failed!\n"));
168         }
169 }
170
171 void jfs_delete_inode(struct inode *inode)
172 {
173         jFYI(1, ("In jfs_delete_inode, inode = 0x%p\n", inode));
174
175         if (test_cflag(COMMIT_Freewmap, inode))
176                 freeZeroLink(inode);
177
178         diFree(inode);
179
180         clear_inode(inode);
181 }
182
183 void jfs_dirty_inode(struct inode *inode)
184 {
185         static int noisy = 5;
186
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
191                          */
192                         jERROR(1, ("jfs_dirty_inode called on "
193                                    "read-only volume\n"));
194                         jERROR(1, ("Is remount racy?\n"));
195                         noisy--;
196                 }
197                 return;
198         }
199
200         set_cflag(COMMIT_Dirty, inode);
201 }
202
203 static int jfs_get_block(struct inode *ip, long lblock,
204                          struct buffer_head *bh_result, int create)
205 {
206         s64 lblock64 = lblock;
207         int no_size_check = 0;
208         int rc = 0;
209         int take_locks;
210         xad_t xad;
211         s64 xaddr;
212         int xflag;
213         s32 xlen;
214
215         /*
216          * If this is a special inode (imap, dmap) or directory,
217          * the lock should already be taken
218          */
219         take_locks = ((JFS_IP(ip)->fileset != AGGREGATE_I) &&
220                       !S_ISDIR(ip->i_mode));
221         /*
222          * Take appropriate lock on inode
223          */
224         if (take_locks) {
225                 if (create)
226                         IWRITE_LOCK(ip);
227                 else
228                         IREAD_LOCK(ip);
229         }
230
231         /*
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
234          */
235         if (S_ISDIR(ip->i_mode))
236                 no_size_check = 1;
237
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)
241              == 0) && 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
310 static int jfs_writepage(struct page *page)
311 {
312         return block_write_full_page(page, jfs_get_block);
313 }
314
315 static int jfs_readpage(struct file *file, struct page *page)
316 {
317         return block_read_full_page(page, jfs_get_block);
318 }
319
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
326 static int jfs_bmap(struct address_space *mapping, long block)
327 {
328         return generic_block_bmap(mapping, block, jfs_get_block);
329 }
330
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  */
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
392 void jfs_truncate(struct inode *ip)
393 {
394         jFYI(1, ("jfs_truncate: size = 0x%lx\n", (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 }