ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / adfs / inode.c
1 /*
2  *  linux/fs/adfs/inode.c
3  *
4  *  Copyright (C) 1997-1999 Russell King
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 #include <linux/errno.h>
11 #include <linux/fs.h>
12 #include <linux/adfs_fs.h>
13 #include <linux/time.h>
14 #include <linux/stat.h>
15 #include <linux/string.h>
16 #include <linux/mm.h>
17 #include <linux/smp_lock.h>
18 #include <linux/module.h>
19 #include <linux/buffer_head.h>
20
21 #include "adfs.h"
22
23 /*
24  * Lookup/Create a block at offset 'block' into 'inode'.  We currently do
25  * not support creation of new blocks, so we return -EIO for this case.
26  */
27 int
28 adfs_get_block(struct inode *inode, sector_t block, struct buffer_head *bh, int create)
29 {
30         if (block < 0)
31                 goto abort_negative;
32
33         if (!create) {
34                 if (block >= inode->i_blocks)
35                         goto abort_toobig;
36
37                 block = __adfs_block_map(inode->i_sb, inode->i_ino, block);
38                 if (block)
39                         map_bh(bh, inode->i_sb, block);
40                 return 0;
41         }
42         /* don't support allocation of blocks yet */
43         return -EIO;
44
45 abort_negative:
46         adfs_error(inode->i_sb, "block %d < 0", block);
47         return -EIO;
48
49 abort_toobig:
50         return 0;
51 }
52
53 static int adfs_writepage(struct page *page, struct writeback_control *wbc)
54 {
55         return block_write_full_page(page, adfs_get_block, wbc);
56 }
57
58 static int adfs_readpage(struct file *file, struct page *page)
59 {
60         return block_read_full_page(page, adfs_get_block);
61 }
62
63 static int adfs_prepare_write(struct file *file, struct page *page, unsigned int from, unsigned int to)
64 {
65         return cont_prepare_write(page, from, to, adfs_get_block,
66                 &ADFS_I(page->mapping->host)->mmu_private);
67 }
68
69 static sector_t _adfs_bmap(struct address_space *mapping, sector_t block)
70 {
71         return generic_block_bmap(mapping, block, adfs_get_block);
72 }
73
74 static struct address_space_operations adfs_aops = {
75         .readpage       = adfs_readpage,
76         .writepage      = adfs_writepage,
77         .sync_page      = block_sync_page,
78         .prepare_write  = adfs_prepare_write,
79         .commit_write   = generic_commit_write,
80         .bmap           = _adfs_bmap
81 };
82
83 static inline unsigned int
84 adfs_filetype(struct inode *inode)
85 {
86         unsigned int type;
87
88         if (ADFS_I(inode)->stamped)
89                 type = (ADFS_I(inode)->loadaddr >> 8) & 0xfff;
90         else
91                 type = (unsigned int) -1;
92
93         return type;
94 }
95
96 /*
97  * Convert ADFS attributes and filetype to Linux permission.
98  */
99 static umode_t
100 adfs_atts2mode(struct super_block *sb, struct inode *inode)
101 {
102         unsigned int filetype, attr = ADFS_I(inode)->attr;
103         umode_t mode, rmask;
104         struct adfs_sb_info *asb = ADFS_SB(sb);
105
106         if (attr & ADFS_NDA_DIRECTORY) {
107                 mode = S_IRUGO & asb->s_owner_mask;
108                 return S_IFDIR | S_IXUGO | mode;
109         }
110
111         filetype = adfs_filetype(inode);
112
113         switch (filetype) {
114         case 0xfc0:     /* LinkFS */
115                 return S_IFLNK|S_IRWXUGO;
116
117         case 0xfe6:     /* UnixExec */
118                 rmask = S_IRUGO | S_IXUGO;
119                 break;
120
121         default:
122                 rmask = S_IRUGO;
123         }
124
125         mode = S_IFREG;
126
127         if (attr & ADFS_NDA_OWNER_READ)
128                 mode |= rmask & asb->s_owner_mask;
129
130         if (attr & ADFS_NDA_OWNER_WRITE)
131                 mode |= S_IWUGO & asb->s_owner_mask;
132
133         if (attr & ADFS_NDA_PUBLIC_READ)
134                 mode |= rmask & asb->s_other_mask;
135
136         if (attr & ADFS_NDA_PUBLIC_WRITE)
137                 mode |= S_IWUGO & asb->s_other_mask;
138         return mode;
139 }
140
141 /*
142  * Convert Linux permission to ADFS attribute.  We try to do the reverse
143  * of atts2mode, but there is not a 1:1 translation.
144  */
145 static int
146 adfs_mode2atts(struct super_block *sb, struct inode *inode)
147 {
148         umode_t mode;
149         int attr;
150         struct adfs_sb_info *asb = ADFS_SB(sb);
151
152         /* FIXME: should we be able to alter a link? */
153         if (S_ISLNK(inode->i_mode))
154                 return ADFS_I(inode)->attr;
155
156         if (S_ISDIR(inode->i_mode))
157                 attr = ADFS_NDA_DIRECTORY;
158         else
159                 attr = 0;
160
161         mode = inode->i_mode & asb->s_owner_mask;
162         if (mode & S_IRUGO)
163                 attr |= ADFS_NDA_OWNER_READ;
164         if (mode & S_IWUGO)
165                 attr |= ADFS_NDA_OWNER_WRITE;
166
167         mode = inode->i_mode & asb->s_other_mask;
168         mode &= ~asb->s_owner_mask;
169         if (mode & S_IRUGO)
170                 attr |= ADFS_NDA_PUBLIC_READ;
171         if (mode & S_IWUGO)
172                 attr |= ADFS_NDA_PUBLIC_WRITE;
173
174         return attr;
175 }
176
177 /*
178  * Convert an ADFS time to Unix time.  ADFS has a 40-bit centi-second time
179  * referenced to 1 Jan 1900 (til 2248)
180  */
181 static void
182 adfs_adfs2unix_time(struct timespec *tv, struct inode *inode)
183 {
184         unsigned int high, low;
185
186         if (ADFS_I(inode)->stamped == 0)
187                 goto cur_time;
188
189         high = ADFS_I(inode)->loadaddr << 24;
190         low  = ADFS_I(inode)->execaddr;
191
192         high |= low >> 8;
193         low  &= 255;
194
195         /* Files dated pre  01 Jan 1970 00:00:00. */
196         if (high < 0x336e996a)
197                 goto too_early;
198
199         /* Files dated post 18 Jan 2038 03:14:05. */
200         if (high >= 0x656e9969)
201                 goto too_late;
202
203         /* discard 2208988800 (0x336e996a00) seconds of time */
204         high -= 0x336e996a;
205
206         /* convert 40-bit centi-seconds to 32-bit seconds */
207         tv->tv_sec = (((high % 100) << 8) + low) / 100 + (high / 100 << 8);
208         tv->tv_nsec = 0;
209         return;
210
211  cur_time:
212         *tv = CURRENT_TIME;
213         return;
214
215  too_early:
216         tv->tv_sec = tv->tv_nsec = 0;
217         return;
218
219  too_late:
220         tv->tv_sec = 0x7ffffffd;
221         tv->tv_nsec = 0;
222         return;
223 }
224
225 /*
226  * Convert an Unix time to ADFS time.  We only do this if the entry has a
227  * time/date stamp already.
228  */
229 static void
230 adfs_unix2adfs_time(struct inode *inode, unsigned int secs)
231 {
232         unsigned int high, low;
233
234         if (ADFS_I(inode)->stamped) {
235                 /* convert 32-bit seconds to 40-bit centi-seconds */
236                 low  = (secs & 255) * 100;
237                 high = (secs / 256) * 100 + (low >> 8) + 0x336e996a;
238
239                 ADFS_I(inode)->loadaddr = (high >> 24) |
240                                 (ADFS_I(inode)->loadaddr & ~0xff);
241                 ADFS_I(inode)->execaddr = (low & 255) | (high << 8);
242         }
243 }
244
245 /*
246  * Fill in the inode information from the object information.
247  *
248  * Note that this is an inode-less filesystem, so we can't use the inode
249  * number to reference the metadata on the media.  Instead, we use the
250  * inode number to hold the object ID, which in turn will tell us where
251  * the data is held.  We also save the parent object ID, and with these
252  * two, we can locate the metadata.
253  *
254  * This does mean that we rely on an objects parent remaining the same at
255  * all times - we cannot cope with a cross-directory rename (yet).
256  */
257 struct inode *
258 adfs_iget(struct super_block *sb, struct object_info *obj)
259 {
260         struct inode *inode;
261
262         inode = new_inode(sb);
263         if (!inode)
264                 goto out;
265
266         inode->i_uid     = ADFS_SB(sb)->s_uid;
267         inode->i_gid     = ADFS_SB(sb)->s_gid;
268         inode->i_ino     = obj->file_id;
269         inode->i_size    = obj->size;
270         inode->i_nlink   = 2;
271         inode->i_blksize = PAGE_SIZE;
272         inode->i_blocks  = (inode->i_size + sb->s_blocksize - 1) >>
273                             sb->s_blocksize_bits;
274
275         /*
276          * we need to save the parent directory ID so that
277          * write_inode can update the directory information
278          * for this file.  This will need special handling
279          * for cross-directory renames.
280          */
281         ADFS_I(inode)->parent_id = obj->parent_id;
282         ADFS_I(inode)->loadaddr  = obj->loadaddr;
283         ADFS_I(inode)->execaddr  = obj->execaddr;
284         ADFS_I(inode)->attr      = obj->attr;
285         ADFS_I(inode)->stamped    = ((obj->loadaddr & 0xfff00000) == 0xfff00000);
286
287         inode->i_mode    = adfs_atts2mode(sb, inode);
288         adfs_adfs2unix_time(&inode->i_mtime, inode);
289         inode->i_atime = inode->i_mtime;
290         inode->i_ctime = inode->i_mtime;
291
292         if (S_ISDIR(inode->i_mode)) {
293                 inode->i_op     = &adfs_dir_inode_operations;
294                 inode->i_fop    = &adfs_dir_operations;
295         } else if (S_ISREG(inode->i_mode)) {
296                 inode->i_op     = &adfs_file_inode_operations;
297                 inode->i_fop    = &adfs_file_operations;
298                 inode->i_mapping->a_ops = &adfs_aops;
299                 ADFS_I(inode)->mmu_private = inode->i_size;
300         }
301
302         insert_inode_hash(inode);
303
304 out:
305         return inode;
306 }
307
308 /*
309  * Validate and convert a changed access mode/time to their ADFS equivalents.
310  * adfs_write_inode will actually write the information back to the directory
311  * later.
312  */
313 int
314 adfs_notify_change(struct dentry *dentry, struct iattr *attr)
315 {
316         struct inode *inode = dentry->d_inode;
317         struct super_block *sb = inode->i_sb;
318         unsigned int ia_valid = attr->ia_valid;
319         int error;
320         
321         lock_kernel();
322
323         error = inode_change_ok(inode, attr);
324
325         /*
326          * we can't change the UID or GID of any file -
327          * we have a global UID/GID in the superblock
328          */
329         if ((ia_valid & ATTR_UID && attr->ia_uid != ADFS_SB(sb)->s_uid) ||
330             (ia_valid & ATTR_GID && attr->ia_gid != ADFS_SB(sb)->s_gid))
331                 error = -EPERM;
332
333         if (error)
334                 goto out;
335
336         if (ia_valid & ATTR_SIZE)
337                 error = vmtruncate(inode, attr->ia_size);
338
339         if (error)
340                 goto out;
341
342         if (ia_valid & ATTR_MTIME) {
343                 inode->i_mtime = attr->ia_mtime;
344                 adfs_unix2adfs_time(inode, attr->ia_mtime.tv_sec);
345         }
346         /*
347          * FIXME: should we make these == to i_mtime since we don't
348          * have the ability to represent them in our filesystem?
349          */
350         if (ia_valid & ATTR_ATIME)
351                 inode->i_atime = attr->ia_atime;
352         if (ia_valid & ATTR_CTIME)
353                 inode->i_ctime = attr->ia_ctime;
354         if (ia_valid & ATTR_MODE) {
355                 ADFS_I(inode)->attr = adfs_mode2atts(sb, inode);
356                 inode->i_mode = adfs_atts2mode(sb, inode);
357         }
358
359         /*
360          * FIXME: should we be marking this inode dirty even if
361          * we don't have any metadata to write back?
362          */
363         if (ia_valid & (ATTR_SIZE | ATTR_MTIME | ATTR_MODE))
364                 mark_inode_dirty(inode);
365 out:
366         unlock_kernel();
367         return error;
368 }
369
370 /*
371  * write an existing inode back to the directory, and therefore the disk.
372  * The adfs-specific inode data has already been updated by
373  * adfs_notify_change()
374  */
375 void adfs_write_inode(struct inode *inode, int unused)
376 {
377         struct super_block *sb = inode->i_sb;
378         struct object_info obj;
379
380         lock_kernel();
381         obj.file_id     = inode->i_ino;
382         obj.name_len    = 0;
383         obj.parent_id   = ADFS_I(inode)->parent_id;
384         obj.loadaddr    = ADFS_I(inode)->loadaddr;
385         obj.execaddr    = ADFS_I(inode)->execaddr;
386         obj.attr        = ADFS_I(inode)->attr;
387         obj.size        = inode->i_size;
388
389         adfs_dir_update(sb, &obj);
390         unlock_kernel();
391 }
392 MODULE_LICENSE("GPL");