inode.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. /*
  2. * linux/fs/ufs/inode.c
  3. *
  4. * Copyright (C) 1998
  5. * Daniel Pirkl <daniel.pirkl@email.cz>
  6. * Charles University, Faculty of Mathematics and Physics
  7. *
  8. * from
  9. *
  10. * linux/fs/ext2/inode.c
  11. *
  12. * Copyright (C) 1992, 1993, 1994, 1995
  13. * Remy Card (card@masi.ibp.fr)
  14. * Laboratoire MASI - Institut Blaise Pascal
  15. * Universite Pierre et Marie Curie (Paris VI)
  16. *
  17. * from
  18. *
  19. * linux/fs/minix/inode.c
  20. *
  21. * Copyright (C) 1991, 1992 Linus Torvalds
  22. *
  23. * Goal-directed block allocation by Stephen Tweedie (sct@dcs.ed.ac.uk), 1993
  24. * Big-endian to little-endian byte-swapping/bitmaps by
  25. * David S. Miller (davem@caip.rutgers.edu), 1995
  26. */
  27. #include <asm/uaccess.h>
  28. #include <asm/system.h>
  29. #include <linux/errno.h>
  30. #include <linux/fs.h>
  31. #include <linux/ufs_fs.h>
  32. #include <linux/time.h>
  33. #include <linux/stat.h>
  34. #include <linux/string.h>
  35. #include <linux/mm.h>
  36. #include <linux/smp_lock.h>
  37. #include <linux/buffer_head.h>
  38. #include "swab.h"
  39. #include "util.h"
  40. static u64 ufs_frag_map(struct inode *inode, sector_t frag);
  41. static int ufs_block_to_path(struct inode *inode, sector_t i_block, sector_t offsets[4])
  42. {
  43. struct ufs_sb_private_info *uspi = UFS_SB(inode->i_sb)->s_uspi;
  44. int ptrs = uspi->s_apb;
  45. int ptrs_bits = uspi->s_apbshift;
  46. const long direct_blocks = UFS_NDADDR,
  47. indirect_blocks = ptrs,
  48. double_blocks = (1 << (ptrs_bits * 2));
  49. int n = 0;
  50. UFSD("ptrs=uspi->s_apb = %d,double_blocks=%ld \n",ptrs,double_blocks);
  51. if (i_block < 0) {
  52. ufs_warning(inode->i_sb, "ufs_block_to_path", "block < 0");
  53. } else if (i_block < direct_blocks) {
  54. offsets[n++] = i_block;
  55. } else if ((i_block -= direct_blocks) < indirect_blocks) {
  56. offsets[n++] = UFS_IND_BLOCK;
  57. offsets[n++] = i_block;
  58. } else if ((i_block -= indirect_blocks) < double_blocks) {
  59. offsets[n++] = UFS_DIND_BLOCK;
  60. offsets[n++] = i_block >> ptrs_bits;
  61. offsets[n++] = i_block & (ptrs - 1);
  62. } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
  63. offsets[n++] = UFS_TIND_BLOCK;
  64. offsets[n++] = i_block >> (ptrs_bits * 2);
  65. offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
  66. offsets[n++] = i_block & (ptrs - 1);
  67. } else {
  68. ufs_warning(inode->i_sb, "ufs_block_to_path", "block > big");
  69. }
  70. return n;
  71. }
  72. /*
  73. * Returns the location of the fragment from
  74. * the begining of the filesystem.
  75. */
  76. static u64 ufs_frag_map(struct inode *inode, sector_t frag)
  77. {
  78. struct ufs_inode_info *ufsi = UFS_I(inode);
  79. struct super_block *sb = inode->i_sb;
  80. struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
  81. u64 mask = (u64) uspi->s_apbmask>>uspi->s_fpbshift;
  82. int shift = uspi->s_apbshift-uspi->s_fpbshift;
  83. sector_t offsets[4], *p;
  84. int depth = ufs_block_to_path(inode, frag >> uspi->s_fpbshift, offsets);
  85. u64 ret = 0L;
  86. __fs32 block;
  87. __fs64 u2_block = 0L;
  88. unsigned flags = UFS_SB(sb)->s_flags;
  89. u64 temp = 0L;
  90. UFSD(": frag = %llu depth = %d\n", (unsigned long long)frag, depth);
  91. UFSD(": uspi->s_fpbshift = %d ,uspi->s_apbmask = %x, mask=%llx\n",
  92. uspi->s_fpbshift, uspi->s_apbmask,
  93. (unsigned long long)mask);
  94. if (depth == 0)
  95. return 0;
  96. p = offsets;
  97. lock_kernel();
  98. if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
  99. goto ufs2;
  100. block = ufsi->i_u1.i_data[*p++];
  101. if (!block)
  102. goto out;
  103. while (--depth) {
  104. struct buffer_head *bh;
  105. sector_t n = *p++;
  106. bh = sb_bread(sb, uspi->s_sbbase + fs32_to_cpu(sb, block)+(n>>shift));
  107. if (!bh)
  108. goto out;
  109. block = ((__fs32 *) bh->b_data)[n & mask];
  110. brelse (bh);
  111. if (!block)
  112. goto out;
  113. }
  114. ret = (u64) (uspi->s_sbbase + fs32_to_cpu(sb, block) + (frag & uspi->s_fpbmask));
  115. goto out;
  116. ufs2:
  117. u2_block = ufsi->i_u1.u2_i_data[*p++];
  118. if (!u2_block)
  119. goto out;
  120. while (--depth) {
  121. struct buffer_head *bh;
  122. sector_t n = *p++;
  123. temp = (u64)(uspi->s_sbbase) + fs64_to_cpu(sb, u2_block);
  124. bh = sb_bread(sb, temp +(u64) (n>>shift));
  125. if (!bh)
  126. goto out;
  127. u2_block = ((__fs64 *)bh->b_data)[n & mask];
  128. brelse(bh);
  129. if (!u2_block)
  130. goto out;
  131. }
  132. temp = (u64)uspi->s_sbbase + fs64_to_cpu(sb, u2_block);
  133. ret = temp + (u64) (frag & uspi->s_fpbmask);
  134. out:
  135. unlock_kernel();
  136. return ret;
  137. }
  138. /**
  139. * ufs_inode_getfrag() - allocate new fragment(s)
  140. * @inode - pointer to inode
  141. * @fragment - number of `fragment' which hold pointer
  142. * to new allocated fragment(s)
  143. * @new_fragment - number of new allocated fragment(s)
  144. * @required - how many fragment(s) we require
  145. * @err - we set it if something wrong
  146. * @phys - pointer to where we save physical number of new allocated fragments,
  147. * NULL if we allocate not data(indirect blocks for example).
  148. * @new - we set it if we allocate new block
  149. * @locked_page - for ufs_new_fragments()
  150. */
  151. static struct buffer_head *
  152. ufs_inode_getfrag(struct inode *inode, unsigned int fragment,
  153. sector_t new_fragment, unsigned int required, int *err,
  154. long *phys, int *new, struct page *locked_page)
  155. {
  156. struct ufs_inode_info *ufsi = UFS_I(inode);
  157. struct super_block *sb = inode->i_sb;
  158. struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
  159. struct buffer_head * result;
  160. unsigned block, blockoff, lastfrag, lastblock, lastblockoff;
  161. unsigned tmp, goal;
  162. __fs32 * p, * p2;
  163. UFSD("ENTER, ino %lu, fragment %u, new_fragment %llu, required %u, "
  164. "metadata %d\n", inode->i_ino, fragment,
  165. (unsigned long long)new_fragment, required, !phys);
  166. /* TODO : to be done for write support
  167. if ( (flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
  168. goto ufs2;
  169. */
  170. block = ufs_fragstoblks (fragment);
  171. blockoff = ufs_fragnum (fragment);
  172. p = ufsi->i_u1.i_data + block;
  173. goal = 0;
  174. repeat:
  175. tmp = fs32_to_cpu(sb, *p);
  176. lastfrag = ufsi->i_lastfrag;
  177. if (tmp && fragment < lastfrag) {
  178. if (!phys) {
  179. result = sb_getblk(sb, uspi->s_sbbase + tmp + blockoff);
  180. if (tmp == fs32_to_cpu(sb, *p)) {
  181. UFSD("EXIT, result %u\n", tmp + blockoff);
  182. return result;
  183. }
  184. brelse (result);
  185. goto repeat;
  186. } else {
  187. *phys = tmp + blockoff;
  188. return NULL;
  189. }
  190. }
  191. lastblock = ufs_fragstoblks (lastfrag);
  192. lastblockoff = ufs_fragnum (lastfrag);
  193. /*
  194. * We will extend file into new block beyond last allocated block
  195. */
  196. if (lastblock < block) {
  197. /*
  198. * We must reallocate last allocated block
  199. */
  200. if (lastblockoff) {
  201. p2 = ufsi->i_u1.i_data + lastblock;
  202. tmp = ufs_new_fragments (inode, p2, lastfrag,
  203. fs32_to_cpu(sb, *p2), uspi->s_fpb - lastblockoff,
  204. err, locked_page);
  205. if (!tmp) {
  206. if (lastfrag != ufsi->i_lastfrag)
  207. goto repeat;
  208. else
  209. return NULL;
  210. }
  211. lastfrag = ufsi->i_lastfrag;
  212. }
  213. tmp = fs32_to_cpu(sb, ufsi->i_u1.i_data[lastblock]);
  214. if (tmp)
  215. goal = tmp + uspi->s_fpb;
  216. tmp = ufs_new_fragments (inode, p, fragment - blockoff,
  217. goal, required + blockoff,
  218. err, locked_page);
  219. }
  220. /*
  221. * We will extend last allocated block
  222. */
  223. else if (lastblock == block) {
  224. tmp = ufs_new_fragments(inode, p, fragment - (blockoff - lastblockoff),
  225. fs32_to_cpu(sb, *p), required + (blockoff - lastblockoff),
  226. err, locked_page);
  227. } else /* (lastblock > block) */ {
  228. /*
  229. * We will allocate new block before last allocated block
  230. */
  231. if (block) {
  232. tmp = fs32_to_cpu(sb, ufsi->i_u1.i_data[block-1]);
  233. if (tmp)
  234. goal = tmp + uspi->s_fpb;
  235. }
  236. tmp = ufs_new_fragments(inode, p, fragment - blockoff,
  237. goal, uspi->s_fpb, err, locked_page);
  238. }
  239. if (!tmp) {
  240. if ((!blockoff && *p) ||
  241. (blockoff && lastfrag != ufsi->i_lastfrag))
  242. goto repeat;
  243. *err = -ENOSPC;
  244. return NULL;
  245. }
  246. if (!phys) {
  247. result = sb_getblk(sb, tmp + blockoff);
  248. } else {
  249. *phys = tmp + blockoff;
  250. result = NULL;
  251. *err = 0;
  252. *new = 1;
  253. }
  254. inode->i_ctime = CURRENT_TIME_SEC;
  255. if (IS_SYNC(inode))
  256. ufs_sync_inode (inode);
  257. mark_inode_dirty(inode);
  258. UFSD("EXIT, result %u\n", tmp + blockoff);
  259. return result;
  260. /* This part : To be implemented ....
  261. Required only for writing, not required for READ-ONLY.
  262. ufs2:
  263. u2_block = ufs_fragstoblks(fragment);
  264. u2_blockoff = ufs_fragnum(fragment);
  265. p = ufsi->i_u1.u2_i_data + block;
  266. goal = 0;
  267. repeat2:
  268. tmp = fs32_to_cpu(sb, *p);
  269. lastfrag = ufsi->i_lastfrag;
  270. */
  271. }
  272. /**
  273. * ufs_inode_getblock() - allocate new block
  274. * @inode - pointer to inode
  275. * @bh - pointer to block which hold "pointer" to new allocated block
  276. * @fragment - number of `fragment' which hold pointer
  277. * to new allocated block
  278. * @new_fragment - number of new allocated fragment
  279. * (block will hold this fragment and also uspi->s_fpb-1)
  280. * @err - see ufs_inode_getfrag()
  281. * @phys - see ufs_inode_getfrag()
  282. * @new - see ufs_inode_getfrag()
  283. * @locked_page - see ufs_inode_getfrag()
  284. */
  285. static struct buffer_head *
  286. ufs_inode_getblock(struct inode *inode, struct buffer_head *bh,
  287. unsigned int fragment, sector_t new_fragment, int *err,
  288. long *phys, int *new, struct page *locked_page)
  289. {
  290. struct super_block *sb = inode->i_sb;
  291. struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
  292. struct buffer_head * result;
  293. unsigned tmp, goal, block, blockoff;
  294. __fs32 * p;
  295. block = ufs_fragstoblks (fragment);
  296. blockoff = ufs_fragnum (fragment);
  297. UFSD("ENTER, ino %lu, fragment %u, new_fragment %llu, metadata %d\n",
  298. inode->i_ino, fragment, (unsigned long long)new_fragment, !phys);
  299. result = NULL;
  300. if (!bh)
  301. goto out;
  302. if (!buffer_uptodate(bh)) {
  303. ll_rw_block (READ, 1, &bh);
  304. wait_on_buffer (bh);
  305. if (!buffer_uptodate(bh))
  306. goto out;
  307. }
  308. p = (__fs32 *) bh->b_data + block;
  309. repeat:
  310. tmp = fs32_to_cpu(sb, *p);
  311. if (tmp) {
  312. if (!phys) {
  313. result = sb_getblk(sb, uspi->s_sbbase + tmp + blockoff);
  314. if (tmp == fs32_to_cpu(sb, *p))
  315. goto out;
  316. brelse (result);
  317. goto repeat;
  318. } else {
  319. *phys = tmp + blockoff;
  320. goto out;
  321. }
  322. }
  323. if (block && (tmp = fs32_to_cpu(sb, ((__fs32*)bh->b_data)[block-1])))
  324. goal = tmp + uspi->s_fpb;
  325. else
  326. goal = bh->b_blocknr + uspi->s_fpb;
  327. tmp = ufs_new_fragments(inode, p, ufs_blknum(new_fragment), goal,
  328. uspi->s_fpb, err, locked_page);
  329. if (!tmp) {
  330. if (fs32_to_cpu(sb, *p))
  331. goto repeat;
  332. goto out;
  333. }
  334. if (!phys) {
  335. result = sb_getblk(sb, tmp + blockoff);
  336. } else {
  337. *phys = tmp + blockoff;
  338. *new = 1;
  339. }
  340. mark_buffer_dirty(bh);
  341. if (IS_SYNC(inode))
  342. sync_dirty_buffer(bh);
  343. inode->i_ctime = CURRENT_TIME_SEC;
  344. mark_inode_dirty(inode);
  345. UFSD("result %u\n", tmp + blockoff);
  346. out:
  347. brelse (bh);
  348. UFSD("EXIT\n");
  349. return result;
  350. }
  351. /**
  352. * ufs_getfrag_bloc() - `get_block_t' function, interface between UFS and
  353. * readpage, writepage and so on
  354. */
  355. int ufs_getfrag_block(struct inode *inode, sector_t fragment, struct buffer_head *bh_result, int create)
  356. {
  357. struct super_block * sb = inode->i_sb;
  358. struct ufs_sb_private_info * uspi = UFS_SB(sb)->s_uspi;
  359. struct buffer_head * bh;
  360. int ret, err, new;
  361. unsigned long ptr,phys;
  362. u64 phys64 = 0;
  363. if (!create) {
  364. phys64 = ufs_frag_map(inode, fragment);
  365. UFSD("phys64 = %llu\n", (unsigned long long)phys64);
  366. if (phys64)
  367. map_bh(bh_result, sb, phys64);
  368. return 0;
  369. }
  370. /* This code entered only while writing ....? */
  371. err = -EIO;
  372. new = 0;
  373. ret = 0;
  374. bh = NULL;
  375. lock_kernel();
  376. UFSD("ENTER, ino %lu, fragment %llu\n", inode->i_ino, (unsigned long long)fragment);
  377. if (fragment < 0)
  378. goto abort_negative;
  379. if (fragment >
  380. ((UFS_NDADDR + uspi->s_apb + uspi->s_2apb + uspi->s_3apb)
  381. << uspi->s_fpbshift))
  382. goto abort_too_big;
  383. err = 0;
  384. ptr = fragment;
  385. /*
  386. * ok, these macros clean the logic up a bit and make
  387. * it much more readable:
  388. */
  389. #define GET_INODE_DATABLOCK(x) \
  390. ufs_inode_getfrag(inode, x, fragment, 1, &err, &phys, &new, bh_result->b_page)
  391. #define GET_INODE_PTR(x) \
  392. ufs_inode_getfrag(inode, x, fragment, uspi->s_fpb, &err, NULL, NULL, NULL)
  393. #define GET_INDIRECT_DATABLOCK(x) \
  394. ufs_inode_getblock(inode, bh, x, fragment, \
  395. &err, &phys, &new, bh_result->b_page)
  396. #define GET_INDIRECT_PTR(x) \
  397. ufs_inode_getblock(inode, bh, x, fragment, \
  398. &err, NULL, NULL, NULL)
  399. if (ptr < UFS_NDIR_FRAGMENT) {
  400. bh = GET_INODE_DATABLOCK(ptr);
  401. goto out;
  402. }
  403. ptr -= UFS_NDIR_FRAGMENT;
  404. if (ptr < (1 << (uspi->s_apbshift + uspi->s_fpbshift))) {
  405. bh = GET_INODE_PTR(UFS_IND_FRAGMENT + (ptr >> uspi->s_apbshift));
  406. goto get_indirect;
  407. }
  408. ptr -= 1 << (uspi->s_apbshift + uspi->s_fpbshift);
  409. if (ptr < (1 << (uspi->s_2apbshift + uspi->s_fpbshift))) {
  410. bh = GET_INODE_PTR(UFS_DIND_FRAGMENT + (ptr >> uspi->s_2apbshift));
  411. goto get_double;
  412. }
  413. ptr -= 1 << (uspi->s_2apbshift + uspi->s_fpbshift);
  414. bh = GET_INODE_PTR(UFS_TIND_FRAGMENT + (ptr >> uspi->s_3apbshift));
  415. bh = GET_INDIRECT_PTR((ptr >> uspi->s_2apbshift) & uspi->s_apbmask);
  416. get_double:
  417. bh = GET_INDIRECT_PTR((ptr >> uspi->s_apbshift) & uspi->s_apbmask);
  418. get_indirect:
  419. bh = GET_INDIRECT_DATABLOCK(ptr & uspi->s_apbmask);
  420. #undef GET_INODE_DATABLOCK
  421. #undef GET_INODE_PTR
  422. #undef GET_INDIRECT_DATABLOCK
  423. #undef GET_INDIRECT_PTR
  424. out:
  425. if (err)
  426. goto abort;
  427. if (new)
  428. set_buffer_new(bh_result);
  429. map_bh(bh_result, sb, phys);
  430. abort:
  431. unlock_kernel();
  432. return err;
  433. abort_negative:
  434. ufs_warning(sb, "ufs_get_block", "block < 0");
  435. goto abort;
  436. abort_too_big:
  437. ufs_warning(sb, "ufs_get_block", "block > big");
  438. goto abort;
  439. }
  440. static struct buffer_head *ufs_getfrag(struct inode *inode,
  441. unsigned int fragment,
  442. int create, int *err)
  443. {
  444. struct buffer_head dummy;
  445. int error;
  446. dummy.b_state = 0;
  447. dummy.b_blocknr = -1000;
  448. error = ufs_getfrag_block(inode, fragment, &dummy, create);
  449. *err = error;
  450. if (!error && buffer_mapped(&dummy)) {
  451. struct buffer_head *bh;
  452. bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
  453. if (buffer_new(&dummy)) {
  454. memset(bh->b_data, 0, inode->i_sb->s_blocksize);
  455. set_buffer_uptodate(bh);
  456. mark_buffer_dirty(bh);
  457. }
  458. return bh;
  459. }
  460. return NULL;
  461. }
  462. struct buffer_head * ufs_bread (struct inode * inode, unsigned fragment,
  463. int create, int * err)
  464. {
  465. struct buffer_head * bh;
  466. UFSD("ENTER, ino %lu, fragment %u\n", inode->i_ino, fragment);
  467. bh = ufs_getfrag (inode, fragment, create, err);
  468. if (!bh || buffer_uptodate(bh))
  469. return bh;
  470. ll_rw_block (READ, 1, &bh);
  471. wait_on_buffer (bh);
  472. if (buffer_uptodate(bh))
  473. return bh;
  474. brelse (bh);
  475. *err = -EIO;
  476. return NULL;
  477. }
  478. static int ufs_writepage(struct page *page, struct writeback_control *wbc)
  479. {
  480. return block_write_full_page(page,ufs_getfrag_block,wbc);
  481. }
  482. static int ufs_readpage(struct file *file, struct page *page)
  483. {
  484. return block_read_full_page(page,ufs_getfrag_block);
  485. }
  486. static int ufs_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
  487. {
  488. return block_prepare_write(page,from,to,ufs_getfrag_block);
  489. }
  490. static sector_t ufs_bmap(struct address_space *mapping, sector_t block)
  491. {
  492. return generic_block_bmap(mapping,block,ufs_getfrag_block);
  493. }
  494. const struct address_space_operations ufs_aops = {
  495. .readpage = ufs_readpage,
  496. .writepage = ufs_writepage,
  497. .sync_page = block_sync_page,
  498. .prepare_write = ufs_prepare_write,
  499. .commit_write = generic_commit_write,
  500. .bmap = ufs_bmap
  501. };
  502. static void ufs_set_inode_ops(struct inode *inode)
  503. {
  504. if (S_ISREG(inode->i_mode)) {
  505. inode->i_op = &ufs_file_inode_operations;
  506. inode->i_fop = &ufs_file_operations;
  507. inode->i_mapping->a_ops = &ufs_aops;
  508. } else if (S_ISDIR(inode->i_mode)) {
  509. inode->i_op = &ufs_dir_inode_operations;
  510. inode->i_fop = &ufs_dir_operations;
  511. inode->i_mapping->a_ops = &ufs_aops;
  512. } else if (S_ISLNK(inode->i_mode)) {
  513. if (!inode->i_blocks)
  514. inode->i_op = &ufs_fast_symlink_inode_operations;
  515. else {
  516. inode->i_op = &page_symlink_inode_operations;
  517. inode->i_mapping->a_ops = &ufs_aops;
  518. }
  519. } else
  520. init_special_inode(inode, inode->i_mode,
  521. ufs_get_inode_dev(inode->i_sb, UFS_I(inode)));
  522. }
  523. static void ufs1_read_inode(struct inode *inode, struct ufs_inode *ufs_inode)
  524. {
  525. struct ufs_inode_info *ufsi = UFS_I(inode);
  526. struct super_block *sb = inode->i_sb;
  527. mode_t mode;
  528. unsigned i;
  529. /*
  530. * Copy data to the in-core inode.
  531. */
  532. inode->i_mode = mode = fs16_to_cpu(sb, ufs_inode->ui_mode);
  533. inode->i_nlink = fs16_to_cpu(sb, ufs_inode->ui_nlink);
  534. if (inode->i_nlink == 0)
  535. ufs_error (sb, "ufs_read_inode", "inode %lu has zero nlink\n", inode->i_ino);
  536. /*
  537. * Linux now has 32-bit uid and gid, so we can support EFT.
  538. */
  539. inode->i_uid = ufs_get_inode_uid(sb, ufs_inode);
  540. inode->i_gid = ufs_get_inode_gid(sb, ufs_inode);
  541. inode->i_size = fs64_to_cpu(sb, ufs_inode->ui_size);
  542. inode->i_atime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_atime.tv_sec);
  543. inode->i_ctime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_ctime.tv_sec);
  544. inode->i_mtime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_mtime.tv_sec);
  545. inode->i_mtime.tv_nsec = 0;
  546. inode->i_atime.tv_nsec = 0;
  547. inode->i_ctime.tv_nsec = 0;
  548. inode->i_blocks = fs32_to_cpu(sb, ufs_inode->ui_blocks);
  549. ufsi->i_flags = fs32_to_cpu(sb, ufs_inode->ui_flags);
  550. ufsi->i_gen = fs32_to_cpu(sb, ufs_inode->ui_gen);
  551. ufsi->i_shadow = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_shadow);
  552. ufsi->i_oeftflag = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_oeftflag);
  553. if (S_ISCHR(mode) || S_ISBLK(mode) || inode->i_blocks) {
  554. for (i = 0; i < (UFS_NDADDR + UFS_NINDIR); i++)
  555. ufsi->i_u1.i_data[i] = ufs_inode->ui_u2.ui_addr.ui_db[i];
  556. } else {
  557. for (i = 0; i < (UFS_NDADDR + UFS_NINDIR) * 4; i++)
  558. ufsi->i_u1.i_symlink[i] = ufs_inode->ui_u2.ui_symlink[i];
  559. }
  560. }
  561. static void ufs2_read_inode(struct inode *inode, struct ufs2_inode *ufs2_inode)
  562. {
  563. struct ufs_inode_info *ufsi = UFS_I(inode);
  564. struct super_block *sb = inode->i_sb;
  565. mode_t mode;
  566. unsigned i;
  567. UFSD("Reading ufs2 inode, ino %lu\n", inode->i_ino);
  568. /*
  569. * Copy data to the in-core inode.
  570. */
  571. inode->i_mode = mode = fs16_to_cpu(sb, ufs2_inode->ui_mode);
  572. inode->i_nlink = fs16_to_cpu(sb, ufs2_inode->ui_nlink);
  573. if (inode->i_nlink == 0)
  574. ufs_error (sb, "ufs_read_inode", "inode %lu has zero nlink\n", inode->i_ino);
  575. /*
  576. * Linux now has 32-bit uid and gid, so we can support EFT.
  577. */
  578. inode->i_uid = fs32_to_cpu(sb, ufs2_inode->ui_uid);
  579. inode->i_gid = fs32_to_cpu(sb, ufs2_inode->ui_gid);
  580. inode->i_size = fs64_to_cpu(sb, ufs2_inode->ui_size);
  581. inode->i_atime.tv_sec = fs32_to_cpu(sb, ufs2_inode->ui_atime.tv_sec);
  582. inode->i_ctime.tv_sec = fs32_to_cpu(sb, ufs2_inode->ui_ctime.tv_sec);
  583. inode->i_mtime.tv_sec = fs32_to_cpu(sb, ufs2_inode->ui_mtime.tv_sec);
  584. inode->i_mtime.tv_nsec = 0;
  585. inode->i_atime.tv_nsec = 0;
  586. inode->i_ctime.tv_nsec = 0;
  587. inode->i_blocks = fs64_to_cpu(sb, ufs2_inode->ui_blocks);
  588. ufsi->i_flags = fs32_to_cpu(sb, ufs2_inode->ui_flags);
  589. ufsi->i_gen = fs32_to_cpu(sb, ufs2_inode->ui_gen);
  590. /*
  591. ufsi->i_shadow = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_shadow);
  592. ufsi->i_oeftflag = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_oeftflag);
  593. */
  594. if (S_ISCHR(mode) || S_ISBLK(mode) || inode->i_blocks) {
  595. for (i = 0; i < (UFS_NDADDR + UFS_NINDIR); i++)
  596. ufsi->i_u1.u2_i_data[i] =
  597. ufs2_inode->ui_u2.ui_addr.ui_db[i];
  598. } else {
  599. for (i = 0; i < (UFS_NDADDR + UFS_NINDIR) * 4; i++)
  600. ufsi->i_u1.i_symlink[i] = ufs2_inode->ui_u2.ui_symlink[i];
  601. }
  602. }
  603. void ufs_read_inode(struct inode * inode)
  604. {
  605. struct ufs_inode_info *ufsi = UFS_I(inode);
  606. struct super_block * sb;
  607. struct ufs_sb_private_info * uspi;
  608. struct buffer_head * bh;
  609. UFSD("ENTER, ino %lu\n", inode->i_ino);
  610. sb = inode->i_sb;
  611. uspi = UFS_SB(sb)->s_uspi;
  612. if (inode->i_ino < UFS_ROOTINO ||
  613. inode->i_ino > (uspi->s_ncg * uspi->s_ipg)) {
  614. ufs_warning(sb, "ufs_read_inode", "bad inode number (%lu)\n",
  615. inode->i_ino);
  616. goto bad_inode;
  617. }
  618. bh = sb_bread(sb, uspi->s_sbbase + ufs_inotofsba(inode->i_ino));
  619. if (!bh) {
  620. ufs_warning(sb, "ufs_read_inode", "unable to read inode %lu\n",
  621. inode->i_ino);
  622. goto bad_inode;
  623. }
  624. if ((UFS_SB(sb)->s_flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
  625. struct ufs2_inode *ufs2_inode = (struct ufs2_inode *)bh->b_data;
  626. ufs2_read_inode(inode,
  627. ufs2_inode + ufs_inotofsbo(inode->i_ino));
  628. } else {
  629. struct ufs_inode *ufs_inode = (struct ufs_inode *)bh->b_data;
  630. ufs1_read_inode(inode, ufs_inode + ufs_inotofsbo(inode->i_ino));
  631. }
  632. inode->i_version++;
  633. ufsi->i_lastfrag =
  634. (inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift;
  635. ufsi->i_dir_start_lookup = 0;
  636. ufsi->i_osync = 0;
  637. ufs_set_inode_ops(inode);
  638. brelse(bh);
  639. UFSD("EXIT\n");
  640. return;
  641. bad_inode:
  642. make_bad_inode(inode);
  643. }
  644. static int ufs_update_inode(struct inode * inode, int do_sync)
  645. {
  646. struct ufs_inode_info *ufsi = UFS_I(inode);
  647. struct super_block * sb;
  648. struct ufs_sb_private_info * uspi;
  649. struct buffer_head * bh;
  650. struct ufs_inode * ufs_inode;
  651. unsigned i;
  652. unsigned flags;
  653. UFSD("ENTER, ino %lu\n", inode->i_ino);
  654. sb = inode->i_sb;
  655. uspi = UFS_SB(sb)->s_uspi;
  656. flags = UFS_SB(sb)->s_flags;
  657. if (inode->i_ino < UFS_ROOTINO ||
  658. inode->i_ino > (uspi->s_ncg * uspi->s_ipg)) {
  659. ufs_warning (sb, "ufs_read_inode", "bad inode number (%lu)\n", inode->i_ino);
  660. return -1;
  661. }
  662. bh = sb_bread(sb, ufs_inotofsba(inode->i_ino));
  663. if (!bh) {
  664. ufs_warning (sb, "ufs_read_inode", "unable to read inode %lu\n", inode->i_ino);
  665. return -1;
  666. }
  667. ufs_inode = (struct ufs_inode *) (bh->b_data + ufs_inotofsbo(inode->i_ino) * sizeof(struct ufs_inode));
  668. ufs_inode->ui_mode = cpu_to_fs16(sb, inode->i_mode);
  669. ufs_inode->ui_nlink = cpu_to_fs16(sb, inode->i_nlink);
  670. ufs_set_inode_uid(sb, ufs_inode, inode->i_uid);
  671. ufs_set_inode_gid(sb, ufs_inode, inode->i_gid);
  672. ufs_inode->ui_size = cpu_to_fs64(sb, inode->i_size);
  673. ufs_inode->ui_atime.tv_sec = cpu_to_fs32(sb, inode->i_atime.tv_sec);
  674. ufs_inode->ui_atime.tv_usec = 0;
  675. ufs_inode->ui_ctime.tv_sec = cpu_to_fs32(sb, inode->i_ctime.tv_sec);
  676. ufs_inode->ui_ctime.tv_usec = 0;
  677. ufs_inode->ui_mtime.tv_sec = cpu_to_fs32(sb, inode->i_mtime.tv_sec);
  678. ufs_inode->ui_mtime.tv_usec = 0;
  679. ufs_inode->ui_blocks = cpu_to_fs32(sb, inode->i_blocks);
  680. ufs_inode->ui_flags = cpu_to_fs32(sb, ufsi->i_flags);
  681. ufs_inode->ui_gen = cpu_to_fs32(sb, ufsi->i_gen);
  682. if ((flags & UFS_UID_MASK) == UFS_UID_EFT) {
  683. ufs_inode->ui_u3.ui_sun.ui_shadow = cpu_to_fs32(sb, ufsi->i_shadow);
  684. ufs_inode->ui_u3.ui_sun.ui_oeftflag = cpu_to_fs32(sb, ufsi->i_oeftflag);
  685. }
  686. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  687. /* ufs_inode->ui_u2.ui_addr.ui_db[0] = cpu_to_fs32(sb, inode->i_rdev); */
  688. ufs_inode->ui_u2.ui_addr.ui_db[0] = ufsi->i_u1.i_data[0];
  689. } else if (inode->i_blocks) {
  690. for (i = 0; i < (UFS_NDADDR + UFS_NINDIR); i++)
  691. ufs_inode->ui_u2.ui_addr.ui_db[i] = ufsi->i_u1.i_data[i];
  692. }
  693. else {
  694. for (i = 0; i < (UFS_NDADDR + UFS_NINDIR) * 4; i++)
  695. ufs_inode->ui_u2.ui_symlink[i] = ufsi->i_u1.i_symlink[i];
  696. }
  697. if (!inode->i_nlink)
  698. memset (ufs_inode, 0, sizeof(struct ufs_inode));
  699. mark_buffer_dirty(bh);
  700. if (do_sync)
  701. sync_dirty_buffer(bh);
  702. brelse (bh);
  703. UFSD("EXIT\n");
  704. return 0;
  705. }
  706. int ufs_write_inode (struct inode * inode, int wait)
  707. {
  708. int ret;
  709. lock_kernel();
  710. ret = ufs_update_inode (inode, wait);
  711. unlock_kernel();
  712. return ret;
  713. }
  714. int ufs_sync_inode (struct inode *inode)
  715. {
  716. return ufs_update_inode (inode, 1);
  717. }
  718. void ufs_delete_inode (struct inode * inode)
  719. {
  720. loff_t old_i_size;
  721. truncate_inode_pages(&inode->i_data, 0);
  722. /*UFS_I(inode)->i_dtime = CURRENT_TIME;*/
  723. lock_kernel();
  724. mark_inode_dirty(inode);
  725. ufs_update_inode(inode, IS_SYNC(inode));
  726. old_i_size = inode->i_size;
  727. inode->i_size = 0;
  728. if (inode->i_blocks && ufs_truncate(inode, old_i_size))
  729. ufs_warning(inode->i_sb, __FUNCTION__, "ufs_truncate failed\n");
  730. ufs_free_inode (inode);
  731. unlock_kernel();
  732. }