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/smp_lock.h> 11 #include <linux/buffer_head.h> 12 #include "adfs.h" 13 14 /* 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. 17 */ 18 static int 19 adfs_get_block(struct inode *inode, sector_t block, struct buffer_head *bh, 20 int create) 21 { 22 if (!create) { 23 if (block >= inode->i_blocks) 24 goto abort_toobig; 25 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; 30 } 31 /* don't support allocation of blocks yet */ 32 return -EIO; 33 34 abort_toobig: 35 return 0; 36 } 37 38 static int adfs_writepage(struct page *page, struct writeback_control *wbc) 39 { 40 return block_write_full_page(page, adfs_get_block, wbc); 41 } 42 43 static int adfs_readpage(struct file *file, struct page *page) 44 { 45 return block_read_full_page(page, adfs_get_block); 46 } 47 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) 51 { 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); 56 } 57 58 static sector_t _adfs_bmap(struct address_space *mapping, sector_t block) 59 { 60 return generic_block_bmap(mapping, block, adfs_get_block); 61 } 62 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 70 }; 71 72 static inline unsigned int 73 adfs_filetype(struct inode *inode) 74 { 75 unsigned int type; 76 77 if (ADFS_I(inode)->stamped) 78 type = (ADFS_I(inode)->loadaddr >> 8) & 0xfff; 79 else 80 type = (unsigned int) -1; 81 82 return type; 83 } 84 85 /* 86 * Convert ADFS attributes and filetype to Linux permission. 87 */ 88 static umode_t 89 adfs_atts2mode(struct super_block *sb, struct inode *inode) 90 { 91 unsigned int filetype, attr = ADFS_I(inode)->attr; 92 umode_t mode, rmask; 93 struct adfs_sb_info *asb = ADFS_SB(sb); 94 95 if (attr & ADFS_NDA_DIRECTORY) { 96 mode = S_IRUGO & asb->s_owner_mask; 97 return S_IFDIR | S_IXUGO | mode; 98 } 99 100 filetype = adfs_filetype(inode); 101 102 switch (filetype) { 103 case 0xfc0: /* LinkFS */ 104 return S_IFLNK|S_IRWXUGO; 105 106 case 0xfe6: /* UnixExec */ 107 rmask = S_IRUGO | S_IXUGO; 108 break; 109 110 default: 111 rmask = S_IRUGO; 112 } 113 114 mode = S_IFREG; 115 116 if (attr & ADFS_NDA_OWNER_READ) 117 mode |= rmask & asb->s_owner_mask; 118 119 if (attr & ADFS_NDA_OWNER_WRITE) 120 mode |= S_IWUGO & asb->s_owner_mask; 121 122 if (attr & ADFS_NDA_PUBLIC_READ) 123 mode |= rmask & asb->s_other_mask; 124 125 if (attr & ADFS_NDA_PUBLIC_WRITE) 126 mode |= S_IWUGO & asb->s_other_mask; 127 return mode; 128 } 129 130 /* 131 * Convert Linux permission to ADFS attribute. We try to do the reverse 132 * of atts2mode, but there is not a 1:1 translation. 133 */ 134 static int 135 adfs_mode2atts(struct super_block *sb, struct inode *inode) 136 { 137 umode_t mode; 138 int attr; 139 struct adfs_sb_info *asb = ADFS_SB(sb); 140 141 /* FIXME: should we be able to alter a link? */ 142 if (S_ISLNK(inode->i_mode)) 143 return ADFS_I(inode)->attr; 144 145 if (S_ISDIR(inode->i_mode)) 146 attr = ADFS_NDA_DIRECTORY; 147 else 148 attr = 0; 149 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; 155 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; 162 163 return attr; 164 } 165 166 /* 167 * Convert an ADFS time to Unix time. ADFS has a 40-bit centi-second time 168 * referenced to 1 Jan 1900 (til 2248) 169 */ 170 static void 171 adfs_adfs2unix_time(struct timespec *tv, struct inode *inode) 172 { 173 unsigned int high, low; 174 175 if (ADFS_I(inode)->stamped == 0) 176 goto cur_time; 177 178 high = ADFS_I(inode)->loadaddr << 24; 179 low = ADFS_I(inode)->execaddr; 180 181 high |= low >> 8; 182 low &= 255; 183 184 /* Files dated pre 01 Jan 1970 00:00:00. */ 185 if (high < 0x336e996a) 186 goto too_early; 187 188 /* Files dated post 18 Jan 2038 03:14:05. */ 189 if (high >= 0x656e9969) 190 goto too_late; 191 192 /* discard 2208988800 (0x336e996a00) seconds of time */ 193 high -= 0x336e996a; 194 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; 199 200 cur_time: 201 *tv = CURRENT_TIME_SEC; 202 return; 203 204 too_early: 205 tv->tv_sec = tv->tv_nsec = 0; 206 return; 207 208 too_late: 209 tv->tv_sec = 0x7ffffffd; 210 tv->tv_nsec = 0; 211 return; 212 } 213 214 /* 215 * Convert an Unix time to ADFS time. We only do this if the entry has a 216 * time/date stamp already. 217 */ 218 static void 219 adfs_unix2adfs_time(struct inode *inode, unsigned int secs) 220 { 221 unsigned int high, low; 222 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; 227 228 ADFS_I(inode)->loadaddr = (high >> 24) | 229 (ADFS_I(inode)->loadaddr & ~0xff); 230 ADFS_I(inode)->execaddr = (low & 255) | (high << 8); 231 } 232 } 233 234 /* 235 * Fill in the inode information from the object information. 236 * 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. 242 * 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). 245 */ 246 struct inode * 247 adfs_iget(struct super_block *sb, struct object_info *obj) 248 { 249 struct inode *inode; 250 251 inode = new_inode(sb); 252 if (!inode) 253 goto out; 254 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; 262 263 /* 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. 268 */ 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); 274 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; 279 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; 288 } 289 290 insert_inode_hash(inode); 291 292 out: 293 return inode; 294 } 295 296 /* 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. 300 */ 301 int 302 adfs_notify_change(struct dentry *dentry, struct iattr *attr) 303 { 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; 308 309 lock_kernel(); 310 311 error = inode_change_ok(inode, attr); 312 313 /* 314 * we can't change the UID or GID of any file - 315 * we have a global UID/GID in the superblock 316 */ 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; 320 321 if (error) 322 goto out; 323 324 if (ia_valid & ATTR_SIZE) 325 error = vmtruncate(inode, attr->ia_size); 326 327 if (error) 328 goto out; 329 330 if (ia_valid & ATTR_MTIME) { 331 inode->i_mtime = attr->ia_mtime; 332 adfs_unix2adfs_time(inode, attr->ia_mtime.tv_sec); 333 } 334 /* 335 * FIXME: should we make these == to i_mtime since we don't 336 * have the ability to represent them in our filesystem? 337 */ 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); 345 } 346 347 /* 348 * FIXME: should we be marking this inode dirty even if 349 * we don't have any metadata to write back? 350 */ 351 if (ia_valid & (ATTR_SIZE | ATTR_MTIME | ATTR_MODE)) 352 mark_inode_dirty(inode); 353 out: 354 unlock_kernel(); 355 return error; 356 } 357 358 /* 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() 362 */ 363 int adfs_write_inode(struct inode *inode, int wait) 364 { 365 struct super_block *sb = inode->i_sb; 366 struct object_info obj; 367 int ret; 368 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; 377 378 ret = adfs_dir_update(sb, &obj, wait); 379 unlock_kernel(); 380 return ret; 381 } 382