MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / fs / adfs / inode.c
blobfee82147f687b9607e102e4fa4251680a1b43e76
1 /*
2 * linux/fs/adfs/inode.c
4 * Copyright (C) 1997-1999 Russell King
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>
21 #include "adfs.h"
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.
27 static int
28 adfs_get_block(struct inode *inode, sector_t block, struct buffer_head *bh,
29 int create)
31 if (block < 0)
32 goto abort_negative;
34 if (!create) {
35 if (block >= inode->i_blocks)
36 goto abort_toobig;
38 block = __adfs_block_map(inode->i_sb, inode->i_ino, block);
39 if (block)
40 map_bh(bh, inode->i_sb, block);
41 return 0;
43 /* don't support allocation of blocks yet */
44 return -EIO;
46 abort_negative:
47 adfs_error(inode->i_sb, "block %d < 0", block);
48 return -EIO;
50 abort_toobig:
51 return 0;
54 static int adfs_writepage(struct page *page, struct writeback_control *wbc)
56 return block_write_full_page(page, adfs_get_block, wbc);
59 static int adfs_readpage(struct file *file, struct page *page)
61 return block_read_full_page(page, adfs_get_block);
64 static int adfs_prepare_write(struct file *file, struct page *page, unsigned int from, unsigned int to)
66 return cont_prepare_write(page, from, to, adfs_get_block,
67 &ADFS_I(page->mapping->host)->mmu_private);
70 static sector_t _adfs_bmap(struct address_space *mapping, sector_t block)
72 return generic_block_bmap(mapping, block, adfs_get_block);
75 static struct address_space_operations adfs_aops = {
76 .readpage = adfs_readpage,
77 .writepage = adfs_writepage,
78 .sync_page = block_sync_page,
79 .prepare_write = adfs_prepare_write,
80 .commit_write = generic_commit_write,
81 .bmap = _adfs_bmap
84 static inline unsigned int
85 adfs_filetype(struct inode *inode)
87 unsigned int type;
89 if (ADFS_I(inode)->stamped)
90 type = (ADFS_I(inode)->loadaddr >> 8) & 0xfff;
91 else
92 type = (unsigned int) -1;
94 return type;
98 * Convert ADFS attributes and filetype to Linux permission.
100 static umode_t
101 adfs_atts2mode(struct super_block *sb, struct inode *inode)
103 unsigned int filetype, attr = ADFS_I(inode)->attr;
104 umode_t mode, rmask;
105 struct adfs_sb_info *asb = ADFS_SB(sb);
107 if (attr & ADFS_NDA_DIRECTORY) {
108 mode = S_IRUGO & asb->s_owner_mask;
109 return S_IFDIR | S_IXUGO | mode;
112 filetype = adfs_filetype(inode);
114 switch (filetype) {
115 case 0xfc0: /* LinkFS */
116 return S_IFLNK|S_IRWXUGO;
118 case 0xfe6: /* UnixExec */
119 rmask = S_IRUGO | S_IXUGO;
120 break;
122 default:
123 rmask = S_IRUGO;
126 mode = S_IFREG;
128 if (attr & ADFS_NDA_OWNER_READ)
129 mode |= rmask & asb->s_owner_mask;
131 if (attr & ADFS_NDA_OWNER_WRITE)
132 mode |= S_IWUGO & asb->s_owner_mask;
134 if (attr & ADFS_NDA_PUBLIC_READ)
135 mode |= rmask & asb->s_other_mask;
137 if (attr & ADFS_NDA_PUBLIC_WRITE)
138 mode |= S_IWUGO & asb->s_other_mask;
139 return mode;
143 * Convert Linux permission to ADFS attribute. We try to do the reverse
144 * of atts2mode, but there is not a 1:1 translation.
146 static int
147 adfs_mode2atts(struct super_block *sb, struct inode *inode)
149 umode_t mode;
150 int attr;
151 struct adfs_sb_info *asb = ADFS_SB(sb);
153 /* FIXME: should we be able to alter a link? */
154 if (S_ISLNK(inode->i_mode))
155 return ADFS_I(inode)->attr;
157 if (S_ISDIR(inode->i_mode))
158 attr = ADFS_NDA_DIRECTORY;
159 else
160 attr = 0;
162 mode = inode->i_mode & asb->s_owner_mask;
163 if (mode & S_IRUGO)
164 attr |= ADFS_NDA_OWNER_READ;
165 if (mode & S_IWUGO)
166 attr |= ADFS_NDA_OWNER_WRITE;
168 mode = inode->i_mode & asb->s_other_mask;
169 mode &= ~asb->s_owner_mask;
170 if (mode & S_IRUGO)
171 attr |= ADFS_NDA_PUBLIC_READ;
172 if (mode & S_IWUGO)
173 attr |= ADFS_NDA_PUBLIC_WRITE;
175 return attr;
179 * Convert an ADFS time to Unix time. ADFS has a 40-bit centi-second time
180 * referenced to 1 Jan 1900 (til 2248)
182 static void
183 adfs_adfs2unix_time(struct timespec *tv, struct inode *inode)
185 unsigned int high, low;
187 if (ADFS_I(inode)->stamped == 0)
188 goto cur_time;
190 high = ADFS_I(inode)->loadaddr << 24;
191 low = ADFS_I(inode)->execaddr;
193 high |= low >> 8;
194 low &= 255;
196 /* Files dated pre 01 Jan 1970 00:00:00. */
197 if (high < 0x336e996a)
198 goto too_early;
200 /* Files dated post 18 Jan 2038 03:14:05. */
201 if (high >= 0x656e9969)
202 goto too_late;
204 /* discard 2208988800 (0x336e996a00) seconds of time */
205 high -= 0x336e996a;
207 /* convert 40-bit centi-seconds to 32-bit seconds */
208 tv->tv_sec = (((high % 100) << 8) + low) / 100 + (high / 100 << 8);
209 tv->tv_nsec = 0;
210 return;
212 cur_time:
213 *tv = CURRENT_TIME;
214 return;
216 too_early:
217 tv->tv_sec = tv->tv_nsec = 0;
218 return;
220 too_late:
221 tv->tv_sec = 0x7ffffffd;
222 tv->tv_nsec = 0;
223 return;
227 * Convert an Unix time to ADFS time. We only do this if the entry has a
228 * time/date stamp already.
230 static void
231 adfs_unix2adfs_time(struct inode *inode, unsigned int secs)
233 unsigned int high, low;
235 if (ADFS_I(inode)->stamped) {
236 /* convert 32-bit seconds to 40-bit centi-seconds */
237 low = (secs & 255) * 100;
238 high = (secs / 256) * 100 + (low >> 8) + 0x336e996a;
240 ADFS_I(inode)->loadaddr = (high >> 24) |
241 (ADFS_I(inode)->loadaddr & ~0xff);
242 ADFS_I(inode)->execaddr = (low & 255) | (high << 8);
247 * Fill in the inode information from the object information.
249 * Note that this is an inode-less filesystem, so we can't use the inode
250 * number to reference the metadata on the media. Instead, we use the
251 * inode number to hold the object ID, which in turn will tell us where
252 * the data is held. We also save the parent object ID, and with these
253 * two, we can locate the metadata.
255 * This does mean that we rely on an objects parent remaining the same at
256 * all times - we cannot cope with a cross-directory rename (yet).
258 struct inode *
259 adfs_iget(struct super_block *sb, struct object_info *obj)
261 struct inode *inode;
263 inode = new_inode(sb);
264 if (!inode)
265 goto out;
267 inode->i_uid = ADFS_SB(sb)->s_uid;
268 inode->i_gid = ADFS_SB(sb)->s_gid;
269 inode->i_ino = obj->file_id;
270 inode->i_size = obj->size;
271 inode->i_nlink = 2;
272 inode->i_blksize = PAGE_SIZE;
273 inode->i_blocks = (inode->i_size + sb->s_blocksize - 1) >>
274 sb->s_blocksize_bits;
277 * we need to save the parent directory ID so that
278 * write_inode can update the directory information
279 * for this file. This will need special handling
280 * for cross-directory renames.
282 ADFS_I(inode)->parent_id = obj->parent_id;
283 ADFS_I(inode)->loadaddr = obj->loadaddr;
284 ADFS_I(inode)->execaddr = obj->execaddr;
285 ADFS_I(inode)->attr = obj->attr;
286 ADFS_I(inode)->stamped = ((obj->loadaddr & 0xfff00000) == 0xfff00000);
288 inode->i_mode = adfs_atts2mode(sb, inode);
289 adfs_adfs2unix_time(&inode->i_mtime, inode);
290 inode->i_atime = inode->i_mtime;
291 inode->i_ctime = inode->i_mtime;
293 if (S_ISDIR(inode->i_mode)) {
294 inode->i_op = &adfs_dir_inode_operations;
295 inode->i_fop = &adfs_dir_operations;
296 } else if (S_ISREG(inode->i_mode)) {
297 inode->i_op = &adfs_file_inode_operations;
298 inode->i_fop = &adfs_file_operations;
299 inode->i_mapping->a_ops = &adfs_aops;
300 ADFS_I(inode)->mmu_private = inode->i_size;
303 insert_inode_hash(inode);
305 out:
306 return inode;
310 * Validate and convert a changed access mode/time to their ADFS equivalents.
311 * adfs_write_inode will actually write the information back to the directory
312 * later.
315 adfs_notify_change(struct dentry *dentry, struct iattr *attr)
317 struct inode *inode = dentry->d_inode;
318 struct super_block *sb = inode->i_sb;
319 unsigned int ia_valid = attr->ia_valid;
320 int error;
322 lock_kernel();
324 error = inode_change_ok(inode, attr);
327 * we can't change the UID or GID of any file -
328 * we have a global UID/GID in the superblock
330 if ((ia_valid & ATTR_UID && attr->ia_uid != ADFS_SB(sb)->s_uid) ||
331 (ia_valid & ATTR_GID && attr->ia_gid != ADFS_SB(sb)->s_gid))
332 error = -EPERM;
334 if (error)
335 goto out;
337 if (ia_valid & ATTR_SIZE)
338 error = vmtruncate(inode, attr->ia_size);
340 if (error)
341 goto out;
343 if (ia_valid & ATTR_MTIME) {
344 inode->i_mtime = attr->ia_mtime;
345 adfs_unix2adfs_time(inode, attr->ia_mtime.tv_sec);
348 * FIXME: should we make these == to i_mtime since we don't
349 * have the ability to represent them in our filesystem?
351 if (ia_valid & ATTR_ATIME)
352 inode->i_atime = attr->ia_atime;
353 if (ia_valid & ATTR_CTIME)
354 inode->i_ctime = attr->ia_ctime;
355 if (ia_valid & ATTR_MODE) {
356 ADFS_I(inode)->attr = adfs_mode2atts(sb, inode);
357 inode->i_mode = adfs_atts2mode(sb, inode);
361 * FIXME: should we be marking this inode dirty even if
362 * we don't have any metadata to write back?
364 if (ia_valid & (ATTR_SIZE | ATTR_MTIME | ATTR_MODE))
365 mark_inode_dirty(inode);
366 out:
367 unlock_kernel();
368 return error;
372 * write an existing inode back to the directory, and therefore the disk.
373 * The adfs-specific inode data has already been updated by
374 * adfs_notify_change()
376 int adfs_write_inode(struct inode *inode, int unused)
378 struct super_block *sb = inode->i_sb;
379 struct object_info obj;
380 int ret;
382 lock_kernel();
383 obj.file_id = inode->i_ino;
384 obj.name_len = 0;
385 obj.parent_id = ADFS_I(inode)->parent_id;
386 obj.loadaddr = ADFS_I(inode)->loadaddr;
387 obj.execaddr = ADFS_I(inode)->execaddr;
388 obj.attr = ADFS_I(inode)->attr;
389 obj.size = inode->i_size;
391 ret = adfs_dir_update(sb, &obj);
392 unlock_kernel();
393 return ret;
395 MODULE_LICENSE("GPL");