memcg: fix refcnt going negative
[pohmelfs.git] / fs / adfs / inode.c
blob3f57ce4bee5d4371649c2a67354dcc16f4eacb48
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/smp_lock.h>
11 #include <linux/buffer_head.h>
12 #include "adfs.h"
15 * Lookup/Create a block at offset 'block' into 'inode'. We currently do
16 * not support creation of new blocks, so we return -EIO for this case.
18 static int
19 adfs_get_block(struct inode *inode, sector_t block, struct buffer_head *bh,
20 int create)
22 if (!create) {
23 if (block >= inode->i_blocks)
24 goto abort_toobig;
26 block = __adfs_block_map(inode->i_sb, inode->i_ino, block);
27 if (block)
28 map_bh(bh, inode->i_sb, block);
29 return 0;
31 /* don't support allocation of blocks yet */
32 return -EIO;
34 abort_toobig:
35 return 0;
38 static int adfs_writepage(struct page *page, struct writeback_control *wbc)
40 return block_write_full_page(page, adfs_get_block, wbc);
43 static int adfs_readpage(struct file *file, struct page *page)
45 return block_read_full_page(page, adfs_get_block);
48 static int adfs_write_begin(struct file *file, struct address_space *mapping,
49 loff_t pos, unsigned len, unsigned flags,
50 struct page **pagep, void **fsdata)
52 *pagep = NULL;
53 return cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
54 adfs_get_block,
55 &ADFS_I(mapping->host)->mmu_private);
58 static sector_t _adfs_bmap(struct address_space *mapping, sector_t block)
60 return generic_block_bmap(mapping, block, adfs_get_block);
63 static const struct address_space_operations adfs_aops = {
64 .readpage = adfs_readpage,
65 .writepage = adfs_writepage,
66 .sync_page = block_sync_page,
67 .write_begin = adfs_write_begin,
68 .write_end = generic_write_end,
69 .bmap = _adfs_bmap
72 static inline unsigned int
73 adfs_filetype(struct inode *inode)
75 unsigned int type;
77 if (ADFS_I(inode)->stamped)
78 type = (ADFS_I(inode)->loadaddr >> 8) & 0xfff;
79 else
80 type = (unsigned int) -1;
82 return type;
86 * Convert ADFS attributes and filetype to Linux permission.
88 static umode_t
89 adfs_atts2mode(struct super_block *sb, struct inode *inode)
91 unsigned int filetype, attr = ADFS_I(inode)->attr;
92 umode_t mode, rmask;
93 struct adfs_sb_info *asb = ADFS_SB(sb);
95 if (attr & ADFS_NDA_DIRECTORY) {
96 mode = S_IRUGO & asb->s_owner_mask;
97 return S_IFDIR | S_IXUGO | mode;
100 filetype = adfs_filetype(inode);
102 switch (filetype) {
103 case 0xfc0: /* LinkFS */
104 return S_IFLNK|S_IRWXUGO;
106 case 0xfe6: /* UnixExec */
107 rmask = S_IRUGO | S_IXUGO;
108 break;
110 default:
111 rmask = S_IRUGO;
114 mode = S_IFREG;
116 if (attr & ADFS_NDA_OWNER_READ)
117 mode |= rmask & asb->s_owner_mask;
119 if (attr & ADFS_NDA_OWNER_WRITE)
120 mode |= S_IWUGO & asb->s_owner_mask;
122 if (attr & ADFS_NDA_PUBLIC_READ)
123 mode |= rmask & asb->s_other_mask;
125 if (attr & ADFS_NDA_PUBLIC_WRITE)
126 mode |= S_IWUGO & asb->s_other_mask;
127 return mode;
131 * Convert Linux permission to ADFS attribute. We try to do the reverse
132 * of atts2mode, but there is not a 1:1 translation.
134 static int
135 adfs_mode2atts(struct super_block *sb, struct inode *inode)
137 umode_t mode;
138 int attr;
139 struct adfs_sb_info *asb = ADFS_SB(sb);
141 /* FIXME: should we be able to alter a link? */
142 if (S_ISLNK(inode->i_mode))
143 return ADFS_I(inode)->attr;
145 if (S_ISDIR(inode->i_mode))
146 attr = ADFS_NDA_DIRECTORY;
147 else
148 attr = 0;
150 mode = inode->i_mode & asb->s_owner_mask;
151 if (mode & S_IRUGO)
152 attr |= ADFS_NDA_OWNER_READ;
153 if (mode & S_IWUGO)
154 attr |= ADFS_NDA_OWNER_WRITE;
156 mode = inode->i_mode & asb->s_other_mask;
157 mode &= ~asb->s_owner_mask;
158 if (mode & S_IRUGO)
159 attr |= ADFS_NDA_PUBLIC_READ;
160 if (mode & S_IWUGO)
161 attr |= ADFS_NDA_PUBLIC_WRITE;
163 return attr;
167 * Convert an ADFS time to Unix time. ADFS has a 40-bit centi-second time
168 * referenced to 1 Jan 1900 (til 2248)
170 static void
171 adfs_adfs2unix_time(struct timespec *tv, struct inode *inode)
173 unsigned int high, low;
175 if (ADFS_I(inode)->stamped == 0)
176 goto cur_time;
178 high = ADFS_I(inode)->loadaddr << 24;
179 low = ADFS_I(inode)->execaddr;
181 high |= low >> 8;
182 low &= 255;
184 /* Files dated pre 01 Jan 1970 00:00:00. */
185 if (high < 0x336e996a)
186 goto too_early;
188 /* Files dated post 18 Jan 2038 03:14:05. */
189 if (high >= 0x656e9969)
190 goto too_late;
192 /* discard 2208988800 (0x336e996a00) seconds of time */
193 high -= 0x336e996a;
195 /* convert 40-bit centi-seconds to 32-bit seconds */
196 tv->tv_sec = (((high % 100) << 8) + low) / 100 + (high / 100 << 8);
197 tv->tv_nsec = 0;
198 return;
200 cur_time:
201 *tv = CURRENT_TIME_SEC;
202 return;
204 too_early:
205 tv->tv_sec = tv->tv_nsec = 0;
206 return;
208 too_late:
209 tv->tv_sec = 0x7ffffffd;
210 tv->tv_nsec = 0;
211 return;
215 * Convert an Unix time to ADFS time. We only do this if the entry has a
216 * time/date stamp already.
218 static void
219 adfs_unix2adfs_time(struct inode *inode, unsigned int secs)
221 unsigned int high, low;
223 if (ADFS_I(inode)->stamped) {
224 /* convert 32-bit seconds to 40-bit centi-seconds */
225 low = (secs & 255) * 100;
226 high = (secs / 256) * 100 + (low >> 8) + 0x336e996a;
228 ADFS_I(inode)->loadaddr = (high >> 24) |
229 (ADFS_I(inode)->loadaddr & ~0xff);
230 ADFS_I(inode)->execaddr = (low & 255) | (high << 8);
235 * Fill in the inode information from the object information.
237 * Note that this is an inode-less filesystem, so we can't use the inode
238 * number to reference the metadata on the media. Instead, we use the
239 * inode number to hold the object ID, which in turn will tell us where
240 * the data is held. We also save the parent object ID, and with these
241 * two, we can locate the metadata.
243 * This does mean that we rely on an objects parent remaining the same at
244 * all times - we cannot cope with a cross-directory rename (yet).
246 struct inode *
247 adfs_iget(struct super_block *sb, struct object_info *obj)
249 struct inode *inode;
251 inode = new_inode(sb);
252 if (!inode)
253 goto out;
255 inode->i_uid = ADFS_SB(sb)->s_uid;
256 inode->i_gid = ADFS_SB(sb)->s_gid;
257 inode->i_ino = obj->file_id;
258 inode->i_size = obj->size;
259 inode->i_nlink = 2;
260 inode->i_blocks = (inode->i_size + sb->s_blocksize - 1) >>
261 sb->s_blocksize_bits;
264 * we need to save the parent directory ID so that
265 * write_inode can update the directory information
266 * for this file. This will need special handling
267 * for cross-directory renames.
269 ADFS_I(inode)->parent_id = obj->parent_id;
270 ADFS_I(inode)->loadaddr = obj->loadaddr;
271 ADFS_I(inode)->execaddr = obj->execaddr;
272 ADFS_I(inode)->attr = obj->attr;
273 ADFS_I(inode)->stamped = ((obj->loadaddr & 0xfff00000) == 0xfff00000);
275 inode->i_mode = adfs_atts2mode(sb, inode);
276 adfs_adfs2unix_time(&inode->i_mtime, inode);
277 inode->i_atime = inode->i_mtime;
278 inode->i_ctime = inode->i_mtime;
280 if (S_ISDIR(inode->i_mode)) {
281 inode->i_op = &adfs_dir_inode_operations;
282 inode->i_fop = &adfs_dir_operations;
283 } else if (S_ISREG(inode->i_mode)) {
284 inode->i_op = &adfs_file_inode_operations;
285 inode->i_fop = &adfs_file_operations;
286 inode->i_mapping->a_ops = &adfs_aops;
287 ADFS_I(inode)->mmu_private = inode->i_size;
290 insert_inode_hash(inode);
292 out:
293 return inode;
297 * Validate and convert a changed access mode/time to their ADFS equivalents.
298 * adfs_write_inode will actually write the information back to the directory
299 * later.
302 adfs_notify_change(struct dentry *dentry, struct iattr *attr)
304 struct inode *inode = dentry->d_inode;
305 struct super_block *sb = inode->i_sb;
306 unsigned int ia_valid = attr->ia_valid;
307 int error;
309 lock_kernel();
311 error = inode_change_ok(inode, attr);
314 * we can't change the UID or GID of any file -
315 * we have a global UID/GID in the superblock
317 if ((ia_valid & ATTR_UID && attr->ia_uid != ADFS_SB(sb)->s_uid) ||
318 (ia_valid & ATTR_GID && attr->ia_gid != ADFS_SB(sb)->s_gid))
319 error = -EPERM;
321 if (error)
322 goto out;
324 if (ia_valid & ATTR_SIZE)
325 error = vmtruncate(inode, attr->ia_size);
327 if (error)
328 goto out;
330 if (ia_valid & ATTR_MTIME) {
331 inode->i_mtime = attr->ia_mtime;
332 adfs_unix2adfs_time(inode, attr->ia_mtime.tv_sec);
335 * FIXME: should we make these == to i_mtime since we don't
336 * have the ability to represent them in our filesystem?
338 if (ia_valid & ATTR_ATIME)
339 inode->i_atime = attr->ia_atime;
340 if (ia_valid & ATTR_CTIME)
341 inode->i_ctime = attr->ia_ctime;
342 if (ia_valid & ATTR_MODE) {
343 ADFS_I(inode)->attr = adfs_mode2atts(sb, inode);
344 inode->i_mode = adfs_atts2mode(sb, inode);
348 * FIXME: should we be marking this inode dirty even if
349 * we don't have any metadata to write back?
351 if (ia_valid & (ATTR_SIZE | ATTR_MTIME | ATTR_MODE))
352 mark_inode_dirty(inode);
353 out:
354 unlock_kernel();
355 return error;
359 * write an existing inode back to the directory, and therefore the disk.
360 * The adfs-specific inode data has already been updated by
361 * adfs_notify_change()
363 int adfs_write_inode(struct inode *inode, int wait)
365 struct super_block *sb = inode->i_sb;
366 struct object_info obj;
367 int ret;
369 lock_kernel();
370 obj.file_id = inode->i_ino;
371 obj.name_len = 0;
372 obj.parent_id = ADFS_I(inode)->parent_id;
373 obj.loadaddr = ADFS_I(inode)->loadaddr;
374 obj.execaddr = ADFS_I(inode)->execaddr;
375 obj.attr = ADFS_I(inode)->attr;
376 obj.size = inode->i_size;
378 ret = adfs_dir_update(sb, &obj, wait);
379 unlock_kernel();
380 return ret;