inode.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/affs/inode.c
  4. *
  5. * (c) 1996 Hans-Joachim Widmaier - Rewritten
  6. *
  7. * (C) 1993 Ray Burr - Modified for Amiga FFS filesystem.
  8. *
  9. * (C) 1992 Eric Youngdale Modified for ISO9660 filesystem.
  10. *
  11. * (C) 1991 Linus Torvalds - minix filesystem
  12. */
  13. #include <linux/sched.h>
  14. #include <linux/cred.h>
  15. #include <linux/gfp.h>
  16. #include "affs.h"
  17. struct inode *affs_iget(struct super_block *sb, unsigned long ino)
  18. {
  19. struct affs_sb_info *sbi = AFFS_SB(sb);
  20. struct buffer_head *bh;
  21. struct affs_tail *tail;
  22. struct inode *inode;
  23. u32 block;
  24. u32 size;
  25. u32 prot;
  26. u16 id;
  27. inode = iget_locked(sb, ino);
  28. if (!inode)
  29. return ERR_PTR(-ENOMEM);
  30. if (!(inode->i_state & I_NEW))
  31. return inode;
  32. pr_debug("affs_iget(%lu)\n", inode->i_ino);
  33. block = inode->i_ino;
  34. bh = affs_bread(sb, block);
  35. if (!bh) {
  36. affs_warning(sb, "read_inode", "Cannot read block %d", block);
  37. goto bad_inode;
  38. }
  39. if (affs_checksum_block(sb, bh) || be32_to_cpu(AFFS_HEAD(bh)->ptype) != T_SHORT) {
  40. affs_warning(sb,"read_inode",
  41. "Checksum or type (ptype=%d) error on inode %d",
  42. AFFS_HEAD(bh)->ptype, block);
  43. goto bad_inode;
  44. }
  45. tail = AFFS_TAIL(sb, bh);
  46. prot = be32_to_cpu(tail->protect);
  47. inode->i_size = 0;
  48. set_nlink(inode, 1);
  49. inode->i_mode = 0;
  50. AFFS_I(inode)->i_extcnt = 1;
  51. AFFS_I(inode)->i_ext_last = ~1;
  52. AFFS_I(inode)->i_protect = prot;
  53. atomic_set(&AFFS_I(inode)->i_opencnt, 0);
  54. AFFS_I(inode)->i_blkcnt = 0;
  55. AFFS_I(inode)->i_lc = NULL;
  56. AFFS_I(inode)->i_lc_size = 0;
  57. AFFS_I(inode)->i_lc_shift = 0;
  58. AFFS_I(inode)->i_lc_mask = 0;
  59. AFFS_I(inode)->i_ac = NULL;
  60. AFFS_I(inode)->i_ext_bh = NULL;
  61. AFFS_I(inode)->mmu_private = 0;
  62. AFFS_I(inode)->i_lastalloc = 0;
  63. AFFS_I(inode)->i_pa_cnt = 0;
  64. if (affs_test_opt(sbi->s_flags, SF_SETMODE))
  65. inode->i_mode = sbi->s_mode;
  66. else
  67. inode->i_mode = affs_prot_to_mode(prot);
  68. id = be16_to_cpu(tail->uid);
  69. if (id == 0 || affs_test_opt(sbi->s_flags, SF_SETUID))
  70. inode->i_uid = sbi->s_uid;
  71. else if (id == 0xFFFF && affs_test_opt(sbi->s_flags, SF_MUFS))
  72. i_uid_write(inode, 0);
  73. else
  74. i_uid_write(inode, id);
  75. id = be16_to_cpu(tail->gid);
  76. if (id == 0 || affs_test_opt(sbi->s_flags, SF_SETGID))
  77. inode->i_gid = sbi->s_gid;
  78. else if (id == 0xFFFF && affs_test_opt(sbi->s_flags, SF_MUFS))
  79. i_gid_write(inode, 0);
  80. else
  81. i_gid_write(inode, id);
  82. switch (be32_to_cpu(tail->stype)) {
  83. case ST_ROOT:
  84. inode->i_uid = sbi->s_uid;
  85. inode->i_gid = sbi->s_gid;
  86. /* fall through */
  87. case ST_USERDIR:
  88. if (be32_to_cpu(tail->stype) == ST_USERDIR ||
  89. affs_test_opt(sbi->s_flags, SF_SETMODE)) {
  90. if (inode->i_mode & S_IRUSR)
  91. inode->i_mode |= S_IXUSR;
  92. if (inode->i_mode & S_IRGRP)
  93. inode->i_mode |= S_IXGRP;
  94. if (inode->i_mode & S_IROTH)
  95. inode->i_mode |= S_IXOTH;
  96. inode->i_mode |= S_IFDIR;
  97. } else
  98. inode->i_mode = S_IRUGO | S_IXUGO | S_IWUSR | S_IFDIR;
  99. /* Maybe it should be controlled by mount parameter? */
  100. //inode->i_mode |= S_ISVTX;
  101. inode->i_op = &affs_dir_inode_operations;
  102. inode->i_fop = &affs_dir_operations;
  103. break;
  104. case ST_LINKDIR:
  105. #if 0
  106. affs_warning(sb, "read_inode", "inode is LINKDIR");
  107. goto bad_inode;
  108. #else
  109. inode->i_mode |= S_IFDIR;
  110. /* ... and leave ->i_op and ->i_fop pointing to empty */
  111. break;
  112. #endif
  113. case ST_LINKFILE:
  114. affs_warning(sb, "read_inode", "inode is LINKFILE");
  115. goto bad_inode;
  116. case ST_FILE:
  117. size = be32_to_cpu(tail->size);
  118. inode->i_mode |= S_IFREG;
  119. AFFS_I(inode)->mmu_private = inode->i_size = size;
  120. if (inode->i_size) {
  121. AFFS_I(inode)->i_blkcnt = (size - 1) /
  122. sbi->s_data_blksize + 1;
  123. AFFS_I(inode)->i_extcnt = (AFFS_I(inode)->i_blkcnt - 1) /
  124. sbi->s_hashsize + 1;
  125. }
  126. if (tail->link_chain)
  127. set_nlink(inode, 2);
  128. inode->i_mapping->a_ops = affs_test_opt(sbi->s_flags, SF_OFS) ?
  129. &affs_aops_ofs : &affs_aops;
  130. inode->i_op = &affs_file_inode_operations;
  131. inode->i_fop = &affs_file_operations;
  132. break;
  133. case ST_SOFTLINK:
  134. inode->i_size = strlen((char *)AFFS_HEAD(bh)->table);
  135. inode->i_mode |= S_IFLNK;
  136. inode_nohighmem(inode);
  137. inode->i_op = &affs_symlink_inode_operations;
  138. inode->i_data.a_ops = &affs_symlink_aops;
  139. break;
  140. }
  141. inode->i_mtime.tv_sec = inode->i_atime.tv_sec = inode->i_ctime.tv_sec
  142. = (be32_to_cpu(tail->change.days) * (24 * 60 * 60) +
  143. be32_to_cpu(tail->change.mins) * 60 +
  144. be32_to_cpu(tail->change.ticks) / 50 +
  145. ((8 * 365 + 2) * 24 * 60 * 60)) +
  146. sys_tz.tz_minuteswest * 60;
  147. inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = inode->i_atime.tv_nsec = 0;
  148. affs_brelse(bh);
  149. unlock_new_inode(inode);
  150. return inode;
  151. bad_inode:
  152. affs_brelse(bh);
  153. iget_failed(inode);
  154. return ERR_PTR(-EIO);
  155. }
  156. int
  157. affs_write_inode(struct inode *inode, struct writeback_control *wbc)
  158. {
  159. struct super_block *sb = inode->i_sb;
  160. struct buffer_head *bh;
  161. struct affs_tail *tail;
  162. uid_t uid;
  163. gid_t gid;
  164. pr_debug("write_inode(%lu)\n", inode->i_ino);
  165. if (!inode->i_nlink)
  166. // possibly free block
  167. return 0;
  168. bh = affs_bread(sb, inode->i_ino);
  169. if (!bh) {
  170. affs_error(sb,"write_inode","Cannot read block %lu",inode->i_ino);
  171. return -EIO;
  172. }
  173. tail = AFFS_TAIL(sb, bh);
  174. if (tail->stype == cpu_to_be32(ST_ROOT)) {
  175. affs_secs_to_datestamp(inode->i_mtime.tv_sec,
  176. &AFFS_ROOT_TAIL(sb, bh)->root_change);
  177. } else {
  178. tail->protect = cpu_to_be32(AFFS_I(inode)->i_protect);
  179. tail->size = cpu_to_be32(inode->i_size);
  180. affs_secs_to_datestamp(inode->i_mtime.tv_sec, &tail->change);
  181. if (!(inode->i_ino == AFFS_SB(sb)->s_root_block)) {
  182. uid = i_uid_read(inode);
  183. gid = i_gid_read(inode);
  184. if (affs_test_opt(AFFS_SB(sb)->s_flags, SF_MUFS)) {
  185. if (uid == 0 || uid == 0xFFFF)
  186. uid = uid ^ ~0;
  187. if (gid == 0 || gid == 0xFFFF)
  188. gid = gid ^ ~0;
  189. }
  190. if (!affs_test_opt(AFFS_SB(sb)->s_flags, SF_SETUID))
  191. tail->uid = cpu_to_be16(uid);
  192. if (!affs_test_opt(AFFS_SB(sb)->s_flags, SF_SETGID))
  193. tail->gid = cpu_to_be16(gid);
  194. }
  195. }
  196. affs_fix_checksum(sb, bh);
  197. mark_buffer_dirty_inode(bh, inode);
  198. affs_brelse(bh);
  199. affs_free_prealloc(inode);
  200. return 0;
  201. }
  202. int
  203. affs_notify_change(struct dentry *dentry, struct iattr *attr)
  204. {
  205. struct inode *inode = d_inode(dentry);
  206. int error;
  207. pr_debug("notify_change(%lu,0x%x)\n", inode->i_ino, attr->ia_valid);
  208. error = setattr_prepare(dentry, attr);
  209. if (error)
  210. goto out;
  211. if (((attr->ia_valid & ATTR_UID) &&
  212. affs_test_opt(AFFS_SB(inode->i_sb)->s_flags, SF_SETUID)) ||
  213. ((attr->ia_valid & ATTR_GID) &&
  214. affs_test_opt(AFFS_SB(inode->i_sb)->s_flags, SF_SETGID)) ||
  215. ((attr->ia_valid & ATTR_MODE) &&
  216. (AFFS_SB(inode->i_sb)->s_flags &
  217. (AFFS_MOUNT_SF_SETMODE | AFFS_MOUNT_SF_IMMUTABLE)))) {
  218. if (!affs_test_opt(AFFS_SB(inode->i_sb)->s_flags, SF_QUIET))
  219. error = -EPERM;
  220. goto out;
  221. }
  222. if ((attr->ia_valid & ATTR_SIZE) &&
  223. attr->ia_size != i_size_read(inode)) {
  224. error = inode_newsize_ok(inode, attr->ia_size);
  225. if (error)
  226. return error;
  227. truncate_setsize(inode, attr->ia_size);
  228. affs_truncate(inode);
  229. }
  230. setattr_copy(inode, attr);
  231. mark_inode_dirty(inode);
  232. if (attr->ia_valid & ATTR_MODE)
  233. affs_mode_to_prot(inode);
  234. out:
  235. return error;
  236. }
  237. void
  238. affs_evict_inode(struct inode *inode)
  239. {
  240. unsigned long cache_page;
  241. pr_debug("evict_inode(ino=%lu, nlink=%u)\n",
  242. inode->i_ino, inode->i_nlink);
  243. truncate_inode_pages_final(&inode->i_data);
  244. if (!inode->i_nlink) {
  245. inode->i_size = 0;
  246. affs_truncate(inode);
  247. }
  248. invalidate_inode_buffers(inode);
  249. clear_inode(inode);
  250. affs_free_prealloc(inode);
  251. cache_page = (unsigned long)AFFS_I(inode)->i_lc;
  252. if (cache_page) {
  253. pr_debug("freeing ext cache\n");
  254. AFFS_I(inode)->i_lc = NULL;
  255. AFFS_I(inode)->i_ac = NULL;
  256. free_page(cache_page);
  257. }
  258. affs_brelse(AFFS_I(inode)->i_ext_bh);
  259. AFFS_I(inode)->i_ext_last = ~1;
  260. AFFS_I(inode)->i_ext_bh = NULL;
  261. if (!inode->i_nlink)
  262. affs_free_block(inode->i_sb, inode->i_ino);
  263. }
  264. struct inode *
  265. affs_new_inode(struct inode *dir)
  266. {
  267. struct super_block *sb = dir->i_sb;
  268. struct inode *inode;
  269. u32 block;
  270. struct buffer_head *bh;
  271. if (!(inode = new_inode(sb)))
  272. goto err_inode;
  273. if (!(block = affs_alloc_block(dir, dir->i_ino)))
  274. goto err_block;
  275. bh = affs_getzeroblk(sb, block);
  276. if (!bh)
  277. goto err_bh;
  278. mark_buffer_dirty_inode(bh, inode);
  279. affs_brelse(bh);
  280. inode->i_uid = current_fsuid();
  281. inode->i_gid = current_fsgid();
  282. inode->i_ino = block;
  283. set_nlink(inode, 1);
  284. inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
  285. atomic_set(&AFFS_I(inode)->i_opencnt, 0);
  286. AFFS_I(inode)->i_blkcnt = 0;
  287. AFFS_I(inode)->i_lc = NULL;
  288. AFFS_I(inode)->i_lc_size = 0;
  289. AFFS_I(inode)->i_lc_shift = 0;
  290. AFFS_I(inode)->i_lc_mask = 0;
  291. AFFS_I(inode)->i_ac = NULL;
  292. AFFS_I(inode)->i_ext_bh = NULL;
  293. AFFS_I(inode)->mmu_private = 0;
  294. AFFS_I(inode)->i_protect = 0;
  295. AFFS_I(inode)->i_lastalloc = 0;
  296. AFFS_I(inode)->i_pa_cnt = 0;
  297. AFFS_I(inode)->i_extcnt = 1;
  298. AFFS_I(inode)->i_ext_last = ~1;
  299. insert_inode_hash(inode);
  300. return inode;
  301. err_bh:
  302. affs_free_block(sb, block);
  303. err_block:
  304. iput(inode);
  305. err_inode:
  306. return NULL;
  307. }
  308. /*
  309. * Add an entry to a directory. Create the header block
  310. * and insert it into the hash table.
  311. */
  312. int
  313. affs_add_entry(struct inode *dir, struct inode *inode, struct dentry *dentry, s32 type)
  314. {
  315. struct super_block *sb = dir->i_sb;
  316. struct buffer_head *inode_bh = NULL;
  317. struct buffer_head *bh;
  318. u32 block = 0;
  319. int retval;
  320. pr_debug("%s(dir=%lu, inode=%lu, \"%pd\", type=%d)\n", __func__,
  321. dir->i_ino, inode->i_ino, dentry, type);
  322. retval = -EIO;
  323. bh = affs_bread(sb, inode->i_ino);
  324. if (!bh)
  325. goto done;
  326. affs_lock_link(inode);
  327. switch (type) {
  328. case ST_LINKFILE:
  329. case ST_LINKDIR:
  330. retval = -ENOSPC;
  331. block = affs_alloc_block(dir, dir->i_ino);
  332. if (!block)
  333. goto err;
  334. retval = -EIO;
  335. inode_bh = bh;
  336. bh = affs_getzeroblk(sb, block);
  337. if (!bh)
  338. goto err;
  339. break;
  340. default:
  341. break;
  342. }
  343. AFFS_HEAD(bh)->ptype = cpu_to_be32(T_SHORT);
  344. AFFS_HEAD(bh)->key = cpu_to_be32(bh->b_blocknr);
  345. affs_copy_name(AFFS_TAIL(sb, bh)->name, dentry);
  346. AFFS_TAIL(sb, bh)->stype = cpu_to_be32(type);
  347. AFFS_TAIL(sb, bh)->parent = cpu_to_be32(dir->i_ino);
  348. if (inode_bh) {
  349. __be32 chain;
  350. chain = AFFS_TAIL(sb, inode_bh)->link_chain;
  351. AFFS_TAIL(sb, bh)->original = cpu_to_be32(inode->i_ino);
  352. AFFS_TAIL(sb, bh)->link_chain = chain;
  353. AFFS_TAIL(sb, inode_bh)->link_chain = cpu_to_be32(block);
  354. affs_adjust_checksum(inode_bh, block - be32_to_cpu(chain));
  355. mark_buffer_dirty_inode(inode_bh, inode);
  356. set_nlink(inode, 2);
  357. ihold(inode);
  358. }
  359. affs_fix_checksum(sb, bh);
  360. mark_buffer_dirty_inode(bh, inode);
  361. dentry->d_fsdata = (void *)(long)bh->b_blocknr;
  362. affs_lock_dir(dir);
  363. retval = affs_insert_hash(dir, bh);
  364. mark_buffer_dirty_inode(bh, inode);
  365. affs_unlock_dir(dir);
  366. affs_unlock_link(inode);
  367. d_instantiate(dentry, inode);
  368. done:
  369. affs_brelse(inode_bh);
  370. affs_brelse(bh);
  371. return retval;
  372. err:
  373. if (block)
  374. affs_free_block(sb, block);
  375. affs_unlock_link(inode);
  376. goto done;
  377. }