inode.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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->i_op = &affs_symlink_inode_operations;
  134. inode->i_data.a_ops = &affs_symlink_aops;
  135. break;
  136. }
  137. inode->i_mtime.tv_sec = inode->i_atime.tv_sec = inode->i_ctime.tv_sec
  138. = (be32_to_cpu(tail->change.days) * (24 * 60 * 60) +
  139. be32_to_cpu(tail->change.mins) * 60 +
  140. be32_to_cpu(tail->change.ticks) / 50 +
  141. ((8 * 365 + 2) * 24 * 60 * 60)) +
  142. sys_tz.tz_minuteswest * 60;
  143. inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = inode->i_atime.tv_nsec = 0;
  144. affs_brelse(bh);
  145. unlock_new_inode(inode);
  146. return inode;
  147. bad_inode:
  148. affs_brelse(bh);
  149. iget_failed(inode);
  150. return ERR_PTR(-EIO);
  151. }
  152. int
  153. affs_write_inode(struct inode *inode, struct writeback_control *wbc)
  154. {
  155. struct super_block *sb = inode->i_sb;
  156. struct buffer_head *bh;
  157. struct affs_tail *tail;
  158. uid_t uid;
  159. gid_t gid;
  160. pr_debug("write_inode(%lu)\n", inode->i_ino);
  161. if (!inode->i_nlink)
  162. // possibly free block
  163. return 0;
  164. bh = affs_bread(sb, inode->i_ino);
  165. if (!bh) {
  166. affs_error(sb,"write_inode","Cannot read block %lu",inode->i_ino);
  167. return -EIO;
  168. }
  169. tail = AFFS_TAIL(sb, bh);
  170. if (tail->stype == cpu_to_be32(ST_ROOT)) {
  171. secs_to_datestamp(inode->i_mtime.tv_sec,&AFFS_ROOT_TAIL(sb, bh)->root_change);
  172. } else {
  173. tail->protect = cpu_to_be32(AFFS_I(inode)->i_protect);
  174. tail->size = cpu_to_be32(inode->i_size);
  175. secs_to_datestamp(inode->i_mtime.tv_sec,&tail->change);
  176. if (!(inode->i_ino == AFFS_SB(sb)->s_root_block)) {
  177. uid = i_uid_read(inode);
  178. gid = i_gid_read(inode);
  179. if (affs_test_opt(AFFS_SB(sb)->s_flags, SF_MUFS)) {
  180. if (uid == 0 || uid == 0xFFFF)
  181. uid = uid ^ ~0;
  182. if (gid == 0 || gid == 0xFFFF)
  183. gid = gid ^ ~0;
  184. }
  185. if (!affs_test_opt(AFFS_SB(sb)->s_flags, SF_SETUID))
  186. tail->uid = cpu_to_be16(uid);
  187. if (!affs_test_opt(AFFS_SB(sb)->s_flags, SF_SETGID))
  188. tail->gid = cpu_to_be16(gid);
  189. }
  190. }
  191. affs_fix_checksum(sb, bh);
  192. mark_buffer_dirty_inode(bh, inode);
  193. affs_brelse(bh);
  194. affs_free_prealloc(inode);
  195. return 0;
  196. }
  197. int
  198. affs_notify_change(struct dentry *dentry, struct iattr *attr)
  199. {
  200. struct inode *inode = d_inode(dentry);
  201. int error;
  202. pr_debug("notify_change(%lu,0x%x)\n", inode->i_ino, attr->ia_valid);
  203. error = inode_change_ok(inode,attr);
  204. if (error)
  205. goto out;
  206. if (((attr->ia_valid & ATTR_UID) &&
  207. affs_test_opt(AFFS_SB(inode->i_sb)->s_flags, SF_SETUID)) ||
  208. ((attr->ia_valid & ATTR_GID) &&
  209. affs_test_opt(AFFS_SB(inode->i_sb)->s_flags, SF_SETGID)) ||
  210. ((attr->ia_valid & ATTR_MODE) &&
  211. (AFFS_SB(inode->i_sb)->s_flags &
  212. (AFFS_MOUNT_SF_SETMODE | AFFS_MOUNT_SF_IMMUTABLE)))) {
  213. if (!affs_test_opt(AFFS_SB(inode->i_sb)->s_flags, SF_QUIET))
  214. error = -EPERM;
  215. goto out;
  216. }
  217. if ((attr->ia_valid & ATTR_SIZE) &&
  218. attr->ia_size != i_size_read(inode)) {
  219. error = inode_newsize_ok(inode, attr->ia_size);
  220. if (error)
  221. return error;
  222. truncate_setsize(inode, attr->ia_size);
  223. affs_truncate(inode);
  224. }
  225. setattr_copy(inode, attr);
  226. mark_inode_dirty(inode);
  227. if (attr->ia_valid & ATTR_MODE)
  228. mode_to_prot(inode);
  229. out:
  230. return error;
  231. }
  232. void
  233. affs_evict_inode(struct inode *inode)
  234. {
  235. unsigned long cache_page;
  236. pr_debug("evict_inode(ino=%lu, nlink=%u)\n",
  237. inode->i_ino, inode->i_nlink);
  238. truncate_inode_pages_final(&inode->i_data);
  239. if (!inode->i_nlink) {
  240. inode->i_size = 0;
  241. affs_truncate(inode);
  242. }
  243. invalidate_inode_buffers(inode);
  244. clear_inode(inode);
  245. affs_free_prealloc(inode);
  246. cache_page = (unsigned long)AFFS_I(inode)->i_lc;
  247. if (cache_page) {
  248. pr_debug("freeing ext cache\n");
  249. AFFS_I(inode)->i_lc = NULL;
  250. AFFS_I(inode)->i_ac = NULL;
  251. free_page(cache_page);
  252. }
  253. affs_brelse(AFFS_I(inode)->i_ext_bh);
  254. AFFS_I(inode)->i_ext_last = ~1;
  255. AFFS_I(inode)->i_ext_bh = NULL;
  256. if (!inode->i_nlink)
  257. affs_free_block(inode->i_sb, inode->i_ino);
  258. }
  259. struct inode *
  260. affs_new_inode(struct inode *dir)
  261. {
  262. struct super_block *sb = dir->i_sb;
  263. struct inode *inode;
  264. u32 block;
  265. struct buffer_head *bh;
  266. if (!(inode = new_inode(sb)))
  267. goto err_inode;
  268. if (!(block = affs_alloc_block(dir, dir->i_ino)))
  269. goto err_block;
  270. bh = affs_getzeroblk(sb, block);
  271. if (!bh)
  272. goto err_bh;
  273. mark_buffer_dirty_inode(bh, inode);
  274. affs_brelse(bh);
  275. inode->i_uid = current_fsuid();
  276. inode->i_gid = current_fsgid();
  277. inode->i_ino = block;
  278. set_nlink(inode, 1);
  279. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
  280. atomic_set(&AFFS_I(inode)->i_opencnt, 0);
  281. AFFS_I(inode)->i_blkcnt = 0;
  282. AFFS_I(inode)->i_lc = NULL;
  283. AFFS_I(inode)->i_lc_size = 0;
  284. AFFS_I(inode)->i_lc_shift = 0;
  285. AFFS_I(inode)->i_lc_mask = 0;
  286. AFFS_I(inode)->i_ac = NULL;
  287. AFFS_I(inode)->i_ext_bh = NULL;
  288. AFFS_I(inode)->mmu_private = 0;
  289. AFFS_I(inode)->i_protect = 0;
  290. AFFS_I(inode)->i_lastalloc = 0;
  291. AFFS_I(inode)->i_pa_cnt = 0;
  292. AFFS_I(inode)->i_extcnt = 1;
  293. AFFS_I(inode)->i_ext_last = ~1;
  294. insert_inode_hash(inode);
  295. return inode;
  296. err_bh:
  297. affs_free_block(sb, block);
  298. err_block:
  299. iput(inode);
  300. err_inode:
  301. return NULL;
  302. }
  303. /*
  304. * Add an entry to a directory. Create the header block
  305. * and insert it into the hash table.
  306. */
  307. int
  308. affs_add_entry(struct inode *dir, struct inode *inode, struct dentry *dentry, s32 type)
  309. {
  310. struct super_block *sb = dir->i_sb;
  311. struct buffer_head *inode_bh = NULL;
  312. struct buffer_head *bh;
  313. u32 block = 0;
  314. int retval;
  315. pr_debug("%s(dir=%lu, inode=%lu, \"%pd\", type=%d)\n", __func__,
  316. dir->i_ino, inode->i_ino, dentry, type);
  317. retval = -EIO;
  318. bh = affs_bread(sb, inode->i_ino);
  319. if (!bh)
  320. goto done;
  321. affs_lock_link(inode);
  322. switch (type) {
  323. case ST_LINKFILE:
  324. case ST_LINKDIR:
  325. retval = -ENOSPC;
  326. block = affs_alloc_block(dir, dir->i_ino);
  327. if (!block)
  328. goto err;
  329. retval = -EIO;
  330. inode_bh = bh;
  331. bh = affs_getzeroblk(sb, block);
  332. if (!bh)
  333. goto err;
  334. break;
  335. default:
  336. break;
  337. }
  338. AFFS_HEAD(bh)->ptype = cpu_to_be32(T_SHORT);
  339. AFFS_HEAD(bh)->key = cpu_to_be32(bh->b_blocknr);
  340. affs_copy_name(AFFS_TAIL(sb, bh)->name, dentry);
  341. AFFS_TAIL(sb, bh)->stype = cpu_to_be32(type);
  342. AFFS_TAIL(sb, bh)->parent = cpu_to_be32(dir->i_ino);
  343. if (inode_bh) {
  344. __be32 chain;
  345. chain = AFFS_TAIL(sb, inode_bh)->link_chain;
  346. AFFS_TAIL(sb, bh)->original = cpu_to_be32(inode->i_ino);
  347. AFFS_TAIL(sb, bh)->link_chain = chain;
  348. AFFS_TAIL(sb, inode_bh)->link_chain = cpu_to_be32(block);
  349. affs_adjust_checksum(inode_bh, block - be32_to_cpu(chain));
  350. mark_buffer_dirty_inode(inode_bh, inode);
  351. set_nlink(inode, 2);
  352. ihold(inode);
  353. }
  354. affs_fix_checksum(sb, bh);
  355. mark_buffer_dirty_inode(bh, inode);
  356. dentry->d_fsdata = (void *)(long)bh->b_blocknr;
  357. affs_lock_dir(dir);
  358. retval = affs_insert_hash(dir, bh);
  359. mark_buffer_dirty_inode(bh, inode);
  360. affs_unlock_dir(dir);
  361. affs_unlock_link(inode);
  362. d_instantiate(dentry, inode);
  363. done:
  364. affs_brelse(inode_bh);
  365. affs_brelse(bh);
  366. return retval;
  367. err:
  368. if (block)
  369. affs_free_block(sb, block);
  370. affs_unlock_link(inode);
  371. goto done;
  372. }