inode.c 9.4 KB

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