inode.c 9.3 KB

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