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