inode.c 25 KB

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