inode.c 11 KB

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