inode.c 9.7 KB

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