inode.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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 <linux/errno.h>
  29. #include <linux/fs.h>
  30. #include <linux/time.h>
  31. #include <linux/stat.h>
  32. #include <linux/string.h>
  33. #include <linux/mm.h>
  34. #include <linux/buffer_head.h>
  35. #include <linux/writeback.h>
  36. #include "ufs_fs.h"
  37. #include "ufs.h"
  38. #include "swab.h"
  39. #include "util.h"
  40. static u64 ufs_frag_map(struct inode *inode, sector_t frag, bool needs_lock);
  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 < direct_blocks) {
  52. offsets[n++] = i_block;
  53. } else if ((i_block -= direct_blocks) < indirect_blocks) {
  54. offsets[n++] = UFS_IND_BLOCK;
  55. offsets[n++] = i_block;
  56. } else if ((i_block -= indirect_blocks) < double_blocks) {
  57. offsets[n++] = UFS_DIND_BLOCK;
  58. offsets[n++] = i_block >> ptrs_bits;
  59. offsets[n++] = i_block & (ptrs - 1);
  60. } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
  61. offsets[n++] = UFS_TIND_BLOCK;
  62. offsets[n++] = i_block >> (ptrs_bits * 2);
  63. offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
  64. offsets[n++] = i_block & (ptrs - 1);
  65. } else {
  66. ufs_warning(inode->i_sb, "ufs_block_to_path", "block > big");
  67. }
  68. return n;
  69. }
  70. /*
  71. * Returns the location of the fragment from
  72. * the beginning of the filesystem.
  73. */
  74. static u64 ufs_frag_map(struct inode *inode, sector_t frag, bool needs_lock)
  75. {
  76. struct ufs_inode_info *ufsi = UFS_I(inode);
  77. struct super_block *sb = inode->i_sb;
  78. struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
  79. u64 mask = (u64) uspi->s_apbmask>>uspi->s_fpbshift;
  80. int shift = uspi->s_apbshift-uspi->s_fpbshift;
  81. sector_t offsets[4], *p;
  82. int depth = ufs_block_to_path(inode, frag >> uspi->s_fpbshift, offsets);
  83. u64 ret = 0L;
  84. __fs32 block;
  85. __fs64 u2_block = 0L;
  86. unsigned flags = UFS_SB(sb)->s_flags;
  87. u64 temp = 0L;
  88. UFSD(": frag = %llu depth = %d\n", (unsigned long long)frag, depth);
  89. UFSD(": uspi->s_fpbshift = %d ,uspi->s_apbmask = %x, mask=%llx\n",
  90. uspi->s_fpbshift, uspi->s_apbmask,
  91. (unsigned long long)mask);
  92. if (depth == 0)
  93. return 0;
  94. p = offsets;
  95. if (needs_lock)
  96. lock_ufs(sb);
  97. if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
  98. goto ufs2;
  99. block = ufsi->i_u1.i_data[*p++];
  100. if (!block)
  101. goto out;
  102. while (--depth) {
  103. struct buffer_head *bh;
  104. sector_t n = *p++;
  105. bh = sb_bread(sb, uspi->s_sbbase + fs32_to_cpu(sb, block)+(n>>shift));
  106. if (!bh)
  107. goto out;
  108. block = ((__fs32 *) bh->b_data)[n & mask];
  109. brelse (bh);
  110. if (!block)
  111. goto out;
  112. }
  113. ret = (u64) (uspi->s_sbbase + fs32_to_cpu(sb, block) + (frag & uspi->s_fpbmask));
  114. goto out;
  115. ufs2:
  116. u2_block = ufsi->i_u1.u2_i_data[*p++];
  117. if (!u2_block)
  118. goto out;
  119. while (--depth) {
  120. struct buffer_head *bh;
  121. sector_t n = *p++;
  122. temp = (u64)(uspi->s_sbbase) + fs64_to_cpu(sb, u2_block);
  123. bh = sb_bread(sb, temp +(u64) (n>>shift));
  124. if (!bh)
  125. goto out;
  126. u2_block = ((__fs64 *)bh->b_data)[n & mask];
  127. brelse(bh);
  128. if (!u2_block)
  129. goto out;
  130. }
  131. temp = (u64)uspi->s_sbbase + fs64_to_cpu(sb, u2_block);
  132. ret = temp + (u64) (frag & uspi->s_fpbmask);
  133. out:
  134. if (needs_lock)
  135. unlock_ufs(sb);
  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, u64 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 blockoff, lastblockoff;
  161. u64 tmp, goal, lastfrag, block, lastblock;
  162. void *p, *p2;
  163. UFSD("ENTER, ino %lu, fragment %llu, new_fragment %llu, required %u, "
  164. "metadata %d\n", inode->i_ino, (unsigned long long)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 = ufs_get_direct_data_ptr(uspi, ufsi, block);
  173. goal = 0;
  174. repeat:
  175. tmp = ufs_data_ptr_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 == ufs_data_ptr_to_cpu(sb, p)) {
  181. UFSD("EXIT, result %llu\n",
  182. (unsigned long long)tmp + blockoff);
  183. return result;
  184. }
  185. brelse (result);
  186. goto repeat;
  187. } else {
  188. *phys = uspi->s_sbbase + tmp + blockoff;
  189. return NULL;
  190. }
  191. }
  192. lastblock = ufs_fragstoblks (lastfrag);
  193. lastblockoff = ufs_fragnum (lastfrag);
  194. /*
  195. * We will extend file into new block beyond last allocated block
  196. */
  197. if (lastblock < block) {
  198. /*
  199. * We must reallocate last allocated block
  200. */
  201. if (lastblockoff) {
  202. p2 = ufs_get_direct_data_ptr(uspi, ufsi, lastblock);
  203. tmp = ufs_new_fragments(inode, p2, lastfrag,
  204. ufs_data_ptr_to_cpu(sb, p2),
  205. uspi->s_fpb - lastblockoff,
  206. err, locked_page);
  207. if (!tmp) {
  208. if (lastfrag != ufsi->i_lastfrag)
  209. goto repeat;
  210. else
  211. return NULL;
  212. }
  213. lastfrag = ufsi->i_lastfrag;
  214. }
  215. tmp = ufs_data_ptr_to_cpu(sb,
  216. ufs_get_direct_data_ptr(uspi, ufsi,
  217. lastblock));
  218. if (tmp)
  219. goal = tmp + uspi->s_fpb;
  220. tmp = ufs_new_fragments (inode, p, fragment - blockoff,
  221. goal, required + blockoff,
  222. err,
  223. phys != NULL ? locked_page : NULL);
  224. } else if (lastblock == block) {
  225. /*
  226. * We will extend last allocated block
  227. */
  228. tmp = ufs_new_fragments(inode, p, fragment -
  229. (blockoff - lastblockoff),
  230. ufs_data_ptr_to_cpu(sb, p),
  231. required + (blockoff - lastblockoff),
  232. err, phys != NULL ? locked_page : NULL);
  233. } else /* (lastblock > block) */ {
  234. /*
  235. * We will allocate new block before last allocated block
  236. */
  237. if (block) {
  238. tmp = ufs_data_ptr_to_cpu(sb,
  239. ufs_get_direct_data_ptr(uspi, ufsi, block - 1));
  240. if (tmp)
  241. goal = tmp + uspi->s_fpb;
  242. }
  243. tmp = ufs_new_fragments(inode, p, fragment - blockoff,
  244. goal, uspi->s_fpb, err,
  245. phys != NULL ? locked_page : NULL);
  246. }
  247. if (!tmp) {
  248. if ((!blockoff && ufs_data_ptr_to_cpu(sb, p)) ||
  249. (blockoff && lastfrag != ufsi->i_lastfrag))
  250. goto repeat;
  251. *err = -ENOSPC;
  252. return NULL;
  253. }
  254. if (!phys) {
  255. result = sb_getblk(sb, uspi->s_sbbase + tmp + blockoff);
  256. } else {
  257. *phys = uspi->s_sbbase + tmp + blockoff;
  258. result = NULL;
  259. *err = 0;
  260. *new = 1;
  261. }
  262. inode->i_ctime = CURRENT_TIME_SEC;
  263. if (IS_SYNC(inode))
  264. ufs_sync_inode (inode);
  265. mark_inode_dirty(inode);
  266. UFSD("EXIT, result %llu\n", (unsigned long long)tmp + blockoff);
  267. return result;
  268. /* This part : To be implemented ....
  269. Required only for writing, not required for READ-ONLY.
  270. ufs2:
  271. u2_block = ufs_fragstoblks(fragment);
  272. u2_blockoff = ufs_fragnum(fragment);
  273. p = ufsi->i_u1.u2_i_data + block;
  274. goal = 0;
  275. repeat2:
  276. tmp = fs32_to_cpu(sb, *p);
  277. lastfrag = ufsi->i_lastfrag;
  278. */
  279. }
  280. /**
  281. * ufs_inode_getblock() - allocate new block
  282. * @inode - pointer to inode
  283. * @bh - pointer to block which hold "pointer" to new allocated block
  284. * @fragment - number of `fragment' which hold pointer
  285. * to new allocated block
  286. * @new_fragment - number of new allocated fragment
  287. * (block will hold this fragment and also uspi->s_fpb-1)
  288. * @err - see ufs_inode_getfrag()
  289. * @phys - see ufs_inode_getfrag()
  290. * @new - see ufs_inode_getfrag()
  291. * @locked_page - see ufs_inode_getfrag()
  292. */
  293. static struct buffer_head *
  294. ufs_inode_getblock(struct inode *inode, struct buffer_head *bh,
  295. u64 fragment, sector_t new_fragment, int *err,
  296. long *phys, int *new, struct page *locked_page)
  297. {
  298. struct super_block *sb = inode->i_sb;
  299. struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
  300. struct buffer_head * result;
  301. unsigned blockoff;
  302. u64 tmp, goal, block;
  303. void *p;
  304. block = ufs_fragstoblks (fragment);
  305. blockoff = ufs_fragnum (fragment);
  306. UFSD("ENTER, ino %lu, fragment %llu, new_fragment %llu, metadata %d\n",
  307. inode->i_ino, (unsigned long long)fragment,
  308. (unsigned long long)new_fragment, !phys);
  309. result = NULL;
  310. if (!bh)
  311. goto out;
  312. if (!buffer_uptodate(bh)) {
  313. ll_rw_block (READ, 1, &bh);
  314. wait_on_buffer (bh);
  315. if (!buffer_uptodate(bh))
  316. goto out;
  317. }
  318. if (uspi->fs_magic == UFS2_MAGIC)
  319. p = (__fs64 *)bh->b_data + block;
  320. else
  321. p = (__fs32 *)bh->b_data + block;
  322. repeat:
  323. tmp = ufs_data_ptr_to_cpu(sb, p);
  324. if (tmp) {
  325. if (!phys) {
  326. result = sb_getblk(sb, uspi->s_sbbase + tmp + blockoff);
  327. if (tmp == ufs_data_ptr_to_cpu(sb, p))
  328. goto out;
  329. brelse (result);
  330. goto repeat;
  331. } else {
  332. *phys = uspi->s_sbbase + tmp + blockoff;
  333. goto out;
  334. }
  335. }
  336. if (block && (uspi->fs_magic == UFS2_MAGIC ?
  337. (tmp = fs64_to_cpu(sb, ((__fs64 *)bh->b_data)[block-1])) :
  338. (tmp = fs32_to_cpu(sb, ((__fs32 *)bh->b_data)[block-1]))))
  339. goal = tmp + uspi->s_fpb;
  340. else
  341. goal = bh->b_blocknr + uspi->s_fpb;
  342. tmp = ufs_new_fragments(inode, p, ufs_blknum(new_fragment), goal,
  343. uspi->s_fpb, err, locked_page);
  344. if (!tmp) {
  345. if (ufs_data_ptr_to_cpu(sb, p))
  346. goto repeat;
  347. goto out;
  348. }
  349. if (!phys) {
  350. result = sb_getblk(sb, uspi->s_sbbase + tmp + blockoff);
  351. } else {
  352. *phys = uspi->s_sbbase + tmp + blockoff;
  353. *new = 1;
  354. }
  355. mark_buffer_dirty(bh);
  356. if (IS_SYNC(inode))
  357. sync_dirty_buffer(bh);
  358. inode->i_ctime = CURRENT_TIME_SEC;
  359. mark_inode_dirty(inode);
  360. UFSD("result %llu\n", (unsigned long long)tmp + blockoff);
  361. out:
  362. brelse (bh);
  363. UFSD("EXIT\n");
  364. return result;
  365. }
  366. /**
  367. * ufs_getfrag_block() - `get_block_t' function, interface between UFS and
  368. * readpage, writepage and so on
  369. */
  370. int ufs_getfrag_block(struct inode *inode, sector_t fragment, struct buffer_head *bh_result, int create)
  371. {
  372. struct super_block * sb = inode->i_sb;
  373. struct ufs_sb_info * sbi = UFS_SB(sb);
  374. struct ufs_sb_private_info * uspi = sbi->s_uspi;
  375. struct buffer_head * bh;
  376. int ret, err, new;
  377. unsigned long ptr,phys;
  378. u64 phys64 = 0;
  379. bool needs_lock = (sbi->mutex_owner != current);
  380. if (!create) {
  381. phys64 = ufs_frag_map(inode, fragment, needs_lock);
  382. UFSD("phys64 = %llu\n", (unsigned long long)phys64);
  383. if (phys64)
  384. map_bh(bh_result, sb, phys64);
  385. return 0;
  386. }
  387. /* This code entered only while writing ....? */
  388. err = -EIO;
  389. new = 0;
  390. ret = 0;
  391. bh = NULL;
  392. if (needs_lock)
  393. lock_ufs(sb);
  394. UFSD("ENTER, ino %lu, fragment %llu\n", inode->i_ino, (unsigned long long)fragment);
  395. if (fragment >
  396. ((UFS_NDADDR + uspi->s_apb + uspi->s_2apb + uspi->s_3apb)
  397. << uspi->s_fpbshift))
  398. goto abort_too_big;
  399. err = 0;
  400. ptr = fragment;
  401. /*
  402. * ok, these macros clean the logic up a bit and make
  403. * it much more readable:
  404. */
  405. #define GET_INODE_DATABLOCK(x) \
  406. ufs_inode_getfrag(inode, x, fragment, 1, &err, &phys, &new,\
  407. bh_result->b_page)
  408. #define GET_INODE_PTR(x) \
  409. ufs_inode_getfrag(inode, x, fragment, uspi->s_fpb, &err, NULL, NULL,\
  410. bh_result->b_page)
  411. #define GET_INDIRECT_DATABLOCK(x) \
  412. ufs_inode_getblock(inode, bh, x, fragment, \
  413. &err, &phys, &new, bh_result->b_page)
  414. #define GET_INDIRECT_PTR(x) \
  415. ufs_inode_getblock(inode, bh, x, fragment, \
  416. &err, NULL, NULL, NULL)
  417. if (ptr < UFS_NDIR_FRAGMENT) {
  418. bh = GET_INODE_DATABLOCK(ptr);
  419. goto out;
  420. }
  421. ptr -= UFS_NDIR_FRAGMENT;
  422. if (ptr < (1 << (uspi->s_apbshift + uspi->s_fpbshift))) {
  423. bh = GET_INODE_PTR(UFS_IND_FRAGMENT + (ptr >> uspi->s_apbshift));
  424. goto get_indirect;
  425. }
  426. ptr -= 1 << (uspi->s_apbshift + uspi->s_fpbshift);
  427. if (ptr < (1 << (uspi->s_2apbshift + uspi->s_fpbshift))) {
  428. bh = GET_INODE_PTR(UFS_DIND_FRAGMENT + (ptr >> uspi->s_2apbshift));
  429. goto get_double;
  430. }
  431. ptr -= 1 << (uspi->s_2apbshift + uspi->s_fpbshift);
  432. bh = GET_INODE_PTR(UFS_TIND_FRAGMENT + (ptr >> uspi->s_3apbshift));
  433. bh = GET_INDIRECT_PTR((ptr >> uspi->s_2apbshift) & uspi->s_apbmask);
  434. get_double:
  435. bh = GET_INDIRECT_PTR((ptr >> uspi->s_apbshift) & uspi->s_apbmask);
  436. get_indirect:
  437. bh = GET_INDIRECT_DATABLOCK(ptr & uspi->s_apbmask);
  438. #undef GET_INODE_DATABLOCK
  439. #undef GET_INODE_PTR
  440. #undef GET_INDIRECT_DATABLOCK
  441. #undef GET_INDIRECT_PTR
  442. out:
  443. if (err)
  444. goto abort;
  445. if (new)
  446. set_buffer_new(bh_result);
  447. map_bh(bh_result, sb, phys);
  448. abort:
  449. if (needs_lock)
  450. unlock_ufs(sb);
  451. return err;
  452. abort_too_big:
  453. ufs_warning(sb, "ufs_get_block", "block > big");
  454. goto abort;
  455. }
  456. static int ufs_writepage(struct page *page, struct writeback_control *wbc)
  457. {
  458. return block_write_full_page(page,ufs_getfrag_block,wbc);
  459. }
  460. static int ufs_readpage(struct file *file, struct page *page)
  461. {
  462. return block_read_full_page(page,ufs_getfrag_block);
  463. }
  464. int ufs_prepare_chunk(struct page *page, loff_t pos, unsigned len)
  465. {
  466. return __block_write_begin(page, pos, len, ufs_getfrag_block);
  467. }
  468. static int ufs_write_begin(struct file *file, struct address_space *mapping,
  469. loff_t pos, unsigned len, unsigned flags,
  470. struct page **pagep, void **fsdata)
  471. {
  472. int ret;
  473. ret = block_write_begin(mapping, pos, len, flags, pagep,
  474. ufs_getfrag_block);
  475. if (unlikely(ret)) {
  476. loff_t isize = mapping->host->i_size;
  477. if (pos + len > isize)
  478. vmtruncate(mapping->host, isize);
  479. }
  480. return ret;
  481. }
  482. static sector_t ufs_bmap(struct address_space *mapping, sector_t block)
  483. {
  484. return generic_block_bmap(mapping,block,ufs_getfrag_block);
  485. }
  486. const struct address_space_operations ufs_aops = {
  487. .readpage = ufs_readpage,
  488. .writepage = ufs_writepage,
  489. .write_begin = ufs_write_begin,
  490. .write_end = generic_write_end,
  491. .bmap = ufs_bmap
  492. };
  493. static void ufs_set_inode_ops(struct inode *inode)
  494. {
  495. if (S_ISREG(inode->i_mode)) {
  496. inode->i_op = &ufs_file_inode_operations;
  497. inode->i_fop = &ufs_file_operations;
  498. inode->i_mapping->a_ops = &ufs_aops;
  499. } else if (S_ISDIR(inode->i_mode)) {
  500. inode->i_op = &ufs_dir_inode_operations;
  501. inode->i_fop = &ufs_dir_operations;
  502. inode->i_mapping->a_ops = &ufs_aops;
  503. } else if (S_ISLNK(inode->i_mode)) {
  504. if (!inode->i_blocks)
  505. inode->i_op = &ufs_fast_symlink_inode_operations;
  506. else {
  507. inode->i_op = &ufs_symlink_inode_operations;
  508. inode->i_mapping->a_ops = &ufs_aops;
  509. }
  510. } else
  511. init_special_inode(inode, inode->i_mode,
  512. ufs_get_inode_dev(inode->i_sb, UFS_I(inode)));
  513. }
  514. static int ufs1_read_inode(struct inode *inode, struct ufs_inode *ufs_inode)
  515. {
  516. struct ufs_inode_info *ufsi = UFS_I(inode);
  517. struct super_block *sb = inode->i_sb;
  518. umode_t mode;
  519. /*
  520. * Copy data to the in-core inode.
  521. */
  522. inode->i_mode = mode = fs16_to_cpu(sb, ufs_inode->ui_mode);
  523. set_nlink(inode, fs16_to_cpu(sb, ufs_inode->ui_nlink));
  524. if (inode->i_nlink == 0) {
  525. ufs_error (sb, "ufs_read_inode", "inode %lu has zero nlink\n", inode->i_ino);
  526. return -1;
  527. }
  528. /*
  529. * Linux now has 32-bit uid and gid, so we can support EFT.
  530. */
  531. inode->i_uid = ufs_get_inode_uid(sb, ufs_inode);
  532. inode->i_gid = ufs_get_inode_gid(sb, ufs_inode);
  533. inode->i_size = fs64_to_cpu(sb, ufs_inode->ui_size);
  534. inode->i_atime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_atime.tv_sec);
  535. inode->i_ctime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_ctime.tv_sec);
  536. inode->i_mtime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_mtime.tv_sec);
  537. inode->i_mtime.tv_nsec = 0;
  538. inode->i_atime.tv_nsec = 0;
  539. inode->i_ctime.tv_nsec = 0;
  540. inode->i_blocks = fs32_to_cpu(sb, ufs_inode->ui_blocks);
  541. inode->i_generation = fs32_to_cpu(sb, ufs_inode->ui_gen);
  542. ufsi->i_flags = fs32_to_cpu(sb, ufs_inode->ui_flags);
  543. ufsi->i_shadow = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_shadow);
  544. ufsi->i_oeftflag = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_oeftflag);
  545. if (S_ISCHR(mode) || S_ISBLK(mode) || inode->i_blocks) {
  546. memcpy(ufsi->i_u1.i_data, &ufs_inode->ui_u2.ui_addr,
  547. sizeof(ufs_inode->ui_u2.ui_addr));
  548. } else {
  549. memcpy(ufsi->i_u1.i_symlink, ufs_inode->ui_u2.ui_symlink,
  550. sizeof(ufs_inode->ui_u2.ui_symlink) - 1);
  551. ufsi->i_u1.i_symlink[sizeof(ufs_inode->ui_u2.ui_symlink) - 1] = 0;
  552. }
  553. return 0;
  554. }
  555. static int ufs2_read_inode(struct inode *inode, struct ufs2_inode *ufs2_inode)
  556. {
  557. struct ufs_inode_info *ufsi = UFS_I(inode);
  558. struct super_block *sb = inode->i_sb;
  559. umode_t mode;
  560. UFSD("Reading ufs2 inode, ino %lu\n", inode->i_ino);
  561. /*
  562. * Copy data to the in-core inode.
  563. */
  564. inode->i_mode = mode = fs16_to_cpu(sb, ufs2_inode->ui_mode);
  565. set_nlink(inode, fs16_to_cpu(sb, ufs2_inode->ui_nlink));
  566. if (inode->i_nlink == 0) {
  567. ufs_error (sb, "ufs_read_inode", "inode %lu has zero nlink\n", inode->i_ino);
  568. return -1;
  569. }
  570. /*
  571. * Linux now has 32-bit uid and gid, so we can support EFT.
  572. */
  573. inode->i_uid = fs32_to_cpu(sb, ufs2_inode->ui_uid);
  574. inode->i_gid = fs32_to_cpu(sb, ufs2_inode->ui_gid);
  575. inode->i_size = fs64_to_cpu(sb, ufs2_inode->ui_size);
  576. inode->i_atime.tv_sec = fs64_to_cpu(sb, ufs2_inode->ui_atime);
  577. inode->i_ctime.tv_sec = fs64_to_cpu(sb, ufs2_inode->ui_ctime);
  578. inode->i_mtime.tv_sec = fs64_to_cpu(sb, ufs2_inode->ui_mtime);
  579. inode->i_atime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_atimensec);
  580. inode->i_ctime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_ctimensec);
  581. inode->i_mtime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_mtimensec);
  582. inode->i_blocks = fs64_to_cpu(sb, ufs2_inode->ui_blocks);
  583. inode->i_generation = fs32_to_cpu(sb, ufs2_inode->ui_gen);
  584. ufsi->i_flags = fs32_to_cpu(sb, ufs2_inode->ui_flags);
  585. /*
  586. ufsi->i_shadow = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_shadow);
  587. ufsi->i_oeftflag = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_oeftflag);
  588. */
  589. if (S_ISCHR(mode) || S_ISBLK(mode) || inode->i_blocks) {
  590. memcpy(ufsi->i_u1.u2_i_data, &ufs2_inode->ui_u2.ui_addr,
  591. sizeof(ufs2_inode->ui_u2.ui_addr));
  592. } else {
  593. memcpy(ufsi->i_u1.i_symlink, ufs2_inode->ui_u2.ui_symlink,
  594. sizeof(ufs2_inode->ui_u2.ui_symlink) - 1);
  595. ufsi->i_u1.i_symlink[sizeof(ufs2_inode->ui_u2.ui_symlink) - 1] = 0;
  596. }
  597. return 0;
  598. }
  599. struct inode *ufs_iget(struct super_block *sb, unsigned long ino)
  600. {
  601. struct ufs_inode_info *ufsi;
  602. struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
  603. struct buffer_head * bh;
  604. struct inode *inode;
  605. int err;
  606. UFSD("ENTER, ino %lu\n", ino);
  607. if (ino < UFS_ROOTINO || ino > (uspi->s_ncg * uspi->s_ipg)) {
  608. ufs_warning(sb, "ufs_read_inode", "bad inode number (%lu)\n",
  609. ino);
  610. return ERR_PTR(-EIO);
  611. }
  612. inode = iget_locked(sb, ino);
  613. if (!inode)
  614. return ERR_PTR(-ENOMEM);
  615. if (!(inode->i_state & I_NEW))
  616. return inode;
  617. ufsi = UFS_I(inode);
  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. err = 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. err = ufs1_read_inode(inode,
  631. ufs_inode + ufs_inotofsbo(inode->i_ino));
  632. }
  633. if (err)
  634. goto bad_inode;
  635. inode->i_version++;
  636. ufsi->i_lastfrag =
  637. (inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift;
  638. ufsi->i_dir_start_lookup = 0;
  639. ufsi->i_osync = 0;
  640. ufs_set_inode_ops(inode);
  641. brelse(bh);
  642. UFSD("EXIT\n");
  643. unlock_new_inode(inode);
  644. return inode;
  645. bad_inode:
  646. iget_failed(inode);
  647. return ERR_PTR(-EIO);
  648. }
  649. static void ufs1_update_inode(struct inode *inode, struct ufs_inode *ufs_inode)
  650. {
  651. struct super_block *sb = inode->i_sb;
  652. struct ufs_inode_info *ufsi = UFS_I(inode);
  653. ufs_inode->ui_mode = cpu_to_fs16(sb, inode->i_mode);
  654. ufs_inode->ui_nlink = cpu_to_fs16(sb, inode->i_nlink);
  655. ufs_set_inode_uid(sb, ufs_inode, inode->i_uid);
  656. ufs_set_inode_gid(sb, ufs_inode, inode->i_gid);
  657. ufs_inode->ui_size = cpu_to_fs64(sb, inode->i_size);
  658. ufs_inode->ui_atime.tv_sec = cpu_to_fs32(sb, inode->i_atime.tv_sec);
  659. ufs_inode->ui_atime.tv_usec = 0;
  660. ufs_inode->ui_ctime.tv_sec = cpu_to_fs32(sb, inode->i_ctime.tv_sec);
  661. ufs_inode->ui_ctime.tv_usec = 0;
  662. ufs_inode->ui_mtime.tv_sec = cpu_to_fs32(sb, inode->i_mtime.tv_sec);
  663. ufs_inode->ui_mtime.tv_usec = 0;
  664. ufs_inode->ui_blocks = cpu_to_fs32(sb, inode->i_blocks);
  665. ufs_inode->ui_flags = cpu_to_fs32(sb, ufsi->i_flags);
  666. ufs_inode->ui_gen = cpu_to_fs32(sb, inode->i_generation);
  667. if ((UFS_SB(sb)->s_flags & UFS_UID_MASK) == UFS_UID_EFT) {
  668. ufs_inode->ui_u3.ui_sun.ui_shadow = cpu_to_fs32(sb, ufsi->i_shadow);
  669. ufs_inode->ui_u3.ui_sun.ui_oeftflag = cpu_to_fs32(sb, ufsi->i_oeftflag);
  670. }
  671. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  672. /* ufs_inode->ui_u2.ui_addr.ui_db[0] = cpu_to_fs32(sb, inode->i_rdev); */
  673. ufs_inode->ui_u2.ui_addr.ui_db[0] = ufsi->i_u1.i_data[0];
  674. } else if (inode->i_blocks) {
  675. memcpy(&ufs_inode->ui_u2.ui_addr, ufsi->i_u1.i_data,
  676. sizeof(ufs_inode->ui_u2.ui_addr));
  677. }
  678. else {
  679. memcpy(&ufs_inode->ui_u2.ui_symlink, ufsi->i_u1.i_symlink,
  680. sizeof(ufs_inode->ui_u2.ui_symlink));
  681. }
  682. if (!inode->i_nlink)
  683. memset (ufs_inode, 0, sizeof(struct ufs_inode));
  684. }
  685. static void ufs2_update_inode(struct inode *inode, struct ufs2_inode *ufs_inode)
  686. {
  687. struct super_block *sb = inode->i_sb;
  688. struct ufs_inode_info *ufsi = UFS_I(inode);
  689. UFSD("ENTER\n");
  690. ufs_inode->ui_mode = cpu_to_fs16(sb, inode->i_mode);
  691. ufs_inode->ui_nlink = cpu_to_fs16(sb, inode->i_nlink);
  692. ufs_inode->ui_uid = cpu_to_fs32(sb, inode->i_uid);
  693. ufs_inode->ui_gid = cpu_to_fs32(sb, inode->i_gid);
  694. ufs_inode->ui_size = cpu_to_fs64(sb, inode->i_size);
  695. ufs_inode->ui_atime = cpu_to_fs64(sb, inode->i_atime.tv_sec);
  696. ufs_inode->ui_atimensec = cpu_to_fs32(sb, inode->i_atime.tv_nsec);
  697. ufs_inode->ui_ctime = cpu_to_fs64(sb, inode->i_ctime.tv_sec);
  698. ufs_inode->ui_ctimensec = cpu_to_fs32(sb, inode->i_ctime.tv_nsec);
  699. ufs_inode->ui_mtime = cpu_to_fs64(sb, inode->i_mtime.tv_sec);
  700. ufs_inode->ui_mtimensec = cpu_to_fs32(sb, inode->i_mtime.tv_nsec);
  701. ufs_inode->ui_blocks = cpu_to_fs64(sb, inode->i_blocks);
  702. ufs_inode->ui_flags = cpu_to_fs32(sb, ufsi->i_flags);
  703. ufs_inode->ui_gen = cpu_to_fs32(sb, inode->i_generation);
  704. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  705. /* ufs_inode->ui_u2.ui_addr.ui_db[0] = cpu_to_fs32(sb, inode->i_rdev); */
  706. ufs_inode->ui_u2.ui_addr.ui_db[0] = ufsi->i_u1.u2_i_data[0];
  707. } else if (inode->i_blocks) {
  708. memcpy(&ufs_inode->ui_u2.ui_addr, ufsi->i_u1.u2_i_data,
  709. sizeof(ufs_inode->ui_u2.ui_addr));
  710. } else {
  711. memcpy(&ufs_inode->ui_u2.ui_symlink, ufsi->i_u1.i_symlink,
  712. sizeof(ufs_inode->ui_u2.ui_symlink));
  713. }
  714. if (!inode->i_nlink)
  715. memset (ufs_inode, 0, sizeof(struct ufs2_inode));
  716. UFSD("EXIT\n");
  717. }
  718. static int ufs_update_inode(struct inode * inode, int do_sync)
  719. {
  720. struct super_block *sb = inode->i_sb;
  721. struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
  722. struct buffer_head * bh;
  723. UFSD("ENTER, ino %lu\n", inode->i_ino);
  724. if (inode->i_ino < UFS_ROOTINO ||
  725. inode->i_ino > (uspi->s_ncg * uspi->s_ipg)) {
  726. ufs_warning (sb, "ufs_read_inode", "bad inode number (%lu)\n", inode->i_ino);
  727. return -1;
  728. }
  729. bh = sb_bread(sb, ufs_inotofsba(inode->i_ino));
  730. if (!bh) {
  731. ufs_warning (sb, "ufs_read_inode", "unable to read inode %lu\n", inode->i_ino);
  732. return -1;
  733. }
  734. if (uspi->fs_magic == UFS2_MAGIC) {
  735. struct ufs2_inode *ufs2_inode = (struct ufs2_inode *)bh->b_data;
  736. ufs2_update_inode(inode,
  737. ufs2_inode + ufs_inotofsbo(inode->i_ino));
  738. } else {
  739. struct ufs_inode *ufs_inode = (struct ufs_inode *) bh->b_data;
  740. ufs1_update_inode(inode, ufs_inode + ufs_inotofsbo(inode->i_ino));
  741. }
  742. mark_buffer_dirty(bh);
  743. if (do_sync)
  744. sync_dirty_buffer(bh);
  745. brelse (bh);
  746. UFSD("EXIT\n");
  747. return 0;
  748. }
  749. int ufs_write_inode(struct inode *inode, struct writeback_control *wbc)
  750. {
  751. int ret;
  752. lock_ufs(inode->i_sb);
  753. ret = ufs_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
  754. unlock_ufs(inode->i_sb);
  755. return ret;
  756. }
  757. int ufs_sync_inode (struct inode *inode)
  758. {
  759. return ufs_update_inode (inode, 1);
  760. }
  761. void ufs_evict_inode(struct inode * inode)
  762. {
  763. int want_delete = 0;
  764. if (!inode->i_nlink && !is_bad_inode(inode))
  765. want_delete = 1;
  766. truncate_inode_pages(&inode->i_data, 0);
  767. if (want_delete) {
  768. loff_t old_i_size;
  769. /*UFS_I(inode)->i_dtime = CURRENT_TIME;*/
  770. lock_ufs(inode->i_sb);
  771. mark_inode_dirty(inode);
  772. ufs_update_inode(inode, IS_SYNC(inode));
  773. old_i_size = inode->i_size;
  774. inode->i_size = 0;
  775. if (inode->i_blocks && ufs_truncate(inode, old_i_size))
  776. ufs_warning(inode->i_sb, __func__, "ufs_truncate failed\n");
  777. unlock_ufs(inode->i_sb);
  778. }
  779. invalidate_inode_buffers(inode);
  780. end_writeback(inode);
  781. if (want_delete) {
  782. lock_ufs(inode->i_sb);
  783. ufs_free_inode (inode);
  784. unlock_ufs(inode->i_sb);
  785. }
  786. }