inode.c 9.2 KB

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