inode.c 10 KB

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