inode.c 9.4 KB

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