file.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. /*
  2. * linux/fs/affs/file.c
  3. *
  4. * (c) 1996 Hans-Joachim Widmaier - Rewritten
  5. *
  6. * (C) 1993 Ray Burr - Modified for Amiga FFS filesystem.
  7. *
  8. * (C) 1992 Eric Youngdale Modified for ISO 9660 filesystem.
  9. *
  10. * (C) 1991 Linus Torvalds - minix filesystem
  11. *
  12. * affs regular file handling primitives
  13. */
  14. #include "affs.h"
  15. #if PAGE_SIZE < 4096
  16. #error PAGE_SIZE must be at least 4096
  17. #endif
  18. static int affs_grow_extcache(struct inode *inode, u32 lc_idx);
  19. static struct buffer_head *affs_alloc_extblock(struct inode *inode, struct buffer_head *bh, u32 ext);
  20. static inline struct buffer_head *affs_get_extblock(struct inode *inode, u32 ext);
  21. static struct buffer_head *affs_get_extblock_slow(struct inode *inode, u32 ext);
  22. static int affs_file_open(struct inode *inode, struct file *filp);
  23. static int affs_file_release(struct inode *inode, struct file *filp);
  24. const struct file_operations affs_file_operations = {
  25. .llseek = generic_file_llseek,
  26. .read = do_sync_read,
  27. .aio_read = generic_file_aio_read,
  28. .write = do_sync_write,
  29. .aio_write = generic_file_aio_write,
  30. .mmap = generic_file_mmap,
  31. .open = affs_file_open,
  32. .release = affs_file_release,
  33. .fsync = file_fsync,
  34. .splice_read = generic_file_splice_read,
  35. };
  36. const struct inode_operations affs_file_inode_operations = {
  37. .truncate = affs_truncate,
  38. .setattr = affs_notify_change,
  39. };
  40. static int
  41. affs_file_open(struct inode *inode, struct file *filp)
  42. {
  43. if (atomic_read(&filp->f_count) != 1)
  44. return 0;
  45. pr_debug("AFFS: open(%d)\n", AFFS_I(inode)->i_opencnt);
  46. AFFS_I(inode)->i_opencnt++;
  47. return 0;
  48. }
  49. static int
  50. affs_file_release(struct inode *inode, struct file *filp)
  51. {
  52. if (atomic_read(&filp->f_count) != 0)
  53. return 0;
  54. pr_debug("AFFS: release(%d)\n", AFFS_I(inode)->i_opencnt);
  55. AFFS_I(inode)->i_opencnt--;
  56. if (!AFFS_I(inode)->i_opencnt)
  57. affs_free_prealloc(inode);
  58. return 0;
  59. }
  60. static int
  61. affs_grow_extcache(struct inode *inode, u32 lc_idx)
  62. {
  63. struct super_block *sb = inode->i_sb;
  64. struct buffer_head *bh;
  65. u32 lc_max;
  66. int i, j, key;
  67. if (!AFFS_I(inode)->i_lc) {
  68. char *ptr = (char *)get_zeroed_page(GFP_NOFS);
  69. if (!ptr)
  70. return -ENOMEM;
  71. AFFS_I(inode)->i_lc = (u32 *)ptr;
  72. AFFS_I(inode)->i_ac = (struct affs_ext_key *)(ptr + AFFS_CACHE_SIZE / 2);
  73. }
  74. lc_max = AFFS_LC_SIZE << AFFS_I(inode)->i_lc_shift;
  75. if (AFFS_I(inode)->i_extcnt > lc_max) {
  76. u32 lc_shift, lc_mask, tmp, off;
  77. /* need to recalculate linear cache, start from old size */
  78. lc_shift = AFFS_I(inode)->i_lc_shift;
  79. tmp = (AFFS_I(inode)->i_extcnt / AFFS_LC_SIZE) >> lc_shift;
  80. for (; tmp; tmp >>= 1)
  81. lc_shift++;
  82. lc_mask = (1 << lc_shift) - 1;
  83. /* fix idx and old size to new shift */
  84. lc_idx >>= (lc_shift - AFFS_I(inode)->i_lc_shift);
  85. AFFS_I(inode)->i_lc_size >>= (lc_shift - AFFS_I(inode)->i_lc_shift);
  86. /* first shrink old cache to make more space */
  87. off = 1 << (lc_shift - AFFS_I(inode)->i_lc_shift);
  88. for (i = 1, j = off; j < AFFS_LC_SIZE; i++, j += off)
  89. AFFS_I(inode)->i_ac[i] = AFFS_I(inode)->i_ac[j];
  90. AFFS_I(inode)->i_lc_shift = lc_shift;
  91. AFFS_I(inode)->i_lc_mask = lc_mask;
  92. }
  93. /* fill cache to the needed index */
  94. i = AFFS_I(inode)->i_lc_size;
  95. AFFS_I(inode)->i_lc_size = lc_idx + 1;
  96. for (; i <= lc_idx; i++) {
  97. if (!i) {
  98. AFFS_I(inode)->i_lc[0] = inode->i_ino;
  99. continue;
  100. }
  101. key = AFFS_I(inode)->i_lc[i - 1];
  102. j = AFFS_I(inode)->i_lc_mask + 1;
  103. // unlock cache
  104. for (; j > 0; j--) {
  105. bh = affs_bread(sb, key);
  106. if (!bh)
  107. goto err;
  108. key = be32_to_cpu(AFFS_TAIL(sb, bh)->extension);
  109. affs_brelse(bh);
  110. }
  111. // lock cache
  112. AFFS_I(inode)->i_lc[i] = key;
  113. }
  114. return 0;
  115. err:
  116. // lock cache
  117. return -EIO;
  118. }
  119. static struct buffer_head *
  120. affs_alloc_extblock(struct inode *inode, struct buffer_head *bh, u32 ext)
  121. {
  122. struct super_block *sb = inode->i_sb;
  123. struct buffer_head *new_bh;
  124. u32 blocknr, tmp;
  125. blocknr = affs_alloc_block(inode, bh->b_blocknr);
  126. if (!blocknr)
  127. return ERR_PTR(-ENOSPC);
  128. new_bh = affs_getzeroblk(sb, blocknr);
  129. if (!new_bh) {
  130. affs_free_block(sb, blocknr);
  131. return ERR_PTR(-EIO);
  132. }
  133. AFFS_HEAD(new_bh)->ptype = cpu_to_be32(T_LIST);
  134. AFFS_HEAD(new_bh)->key = cpu_to_be32(blocknr);
  135. AFFS_TAIL(sb, new_bh)->stype = cpu_to_be32(ST_FILE);
  136. AFFS_TAIL(sb, new_bh)->parent = cpu_to_be32(inode->i_ino);
  137. affs_fix_checksum(sb, new_bh);
  138. mark_buffer_dirty_inode(new_bh, inode);
  139. tmp = be32_to_cpu(AFFS_TAIL(sb, bh)->extension);
  140. if (tmp)
  141. affs_warning(sb, "alloc_ext", "previous extension set (%x)", tmp);
  142. AFFS_TAIL(sb, bh)->extension = cpu_to_be32(blocknr);
  143. affs_adjust_checksum(bh, blocknr - tmp);
  144. mark_buffer_dirty_inode(bh, inode);
  145. AFFS_I(inode)->i_extcnt++;
  146. mark_inode_dirty(inode);
  147. return new_bh;
  148. }
  149. static inline struct buffer_head *
  150. affs_get_extblock(struct inode *inode, u32 ext)
  151. {
  152. /* inline the simplest case: same extended block as last time */
  153. struct buffer_head *bh = AFFS_I(inode)->i_ext_bh;
  154. if (ext == AFFS_I(inode)->i_ext_last)
  155. atomic_inc(&bh->b_count);
  156. else
  157. /* we have to do more (not inlined) */
  158. bh = affs_get_extblock_slow(inode, ext);
  159. return bh;
  160. }
  161. static struct buffer_head *
  162. affs_get_extblock_slow(struct inode *inode, u32 ext)
  163. {
  164. struct super_block *sb = inode->i_sb;
  165. struct buffer_head *bh;
  166. u32 ext_key;
  167. u32 lc_idx, lc_off, ac_idx;
  168. u32 tmp, idx;
  169. if (ext == AFFS_I(inode)->i_ext_last + 1) {
  170. /* read the next extended block from the current one */
  171. bh = AFFS_I(inode)->i_ext_bh;
  172. ext_key = be32_to_cpu(AFFS_TAIL(sb, bh)->extension);
  173. if (ext < AFFS_I(inode)->i_extcnt)
  174. goto read_ext;
  175. if (ext > AFFS_I(inode)->i_extcnt)
  176. BUG();
  177. bh = affs_alloc_extblock(inode, bh, ext);
  178. if (IS_ERR(bh))
  179. return bh;
  180. goto store_ext;
  181. }
  182. if (ext == 0) {
  183. /* we seek back to the file header block */
  184. ext_key = inode->i_ino;
  185. goto read_ext;
  186. }
  187. if (ext >= AFFS_I(inode)->i_extcnt) {
  188. struct buffer_head *prev_bh;
  189. /* allocate a new extended block */
  190. if (ext > AFFS_I(inode)->i_extcnt)
  191. BUG();
  192. /* get previous extended block */
  193. prev_bh = affs_get_extblock(inode, ext - 1);
  194. if (IS_ERR(prev_bh))
  195. return prev_bh;
  196. bh = affs_alloc_extblock(inode, prev_bh, ext);
  197. affs_brelse(prev_bh);
  198. if (IS_ERR(bh))
  199. return bh;
  200. goto store_ext;
  201. }
  202. again:
  203. /* check if there is an extended cache and whether it's large enough */
  204. lc_idx = ext >> AFFS_I(inode)->i_lc_shift;
  205. lc_off = ext & AFFS_I(inode)->i_lc_mask;
  206. if (lc_idx >= AFFS_I(inode)->i_lc_size) {
  207. int err;
  208. err = affs_grow_extcache(inode, lc_idx);
  209. if (err)
  210. return ERR_PTR(err);
  211. goto again;
  212. }
  213. /* every n'th key we find in the linear cache */
  214. if (!lc_off) {
  215. ext_key = AFFS_I(inode)->i_lc[lc_idx];
  216. goto read_ext;
  217. }
  218. /* maybe it's still in the associative cache */
  219. ac_idx = (ext - lc_idx - 1) & AFFS_AC_MASK;
  220. if (AFFS_I(inode)->i_ac[ac_idx].ext == ext) {
  221. ext_key = AFFS_I(inode)->i_ac[ac_idx].key;
  222. goto read_ext;
  223. }
  224. /* try to find one of the previous extended blocks */
  225. tmp = ext;
  226. idx = ac_idx;
  227. while (--tmp, --lc_off > 0) {
  228. idx = (idx - 1) & AFFS_AC_MASK;
  229. if (AFFS_I(inode)->i_ac[idx].ext == tmp) {
  230. ext_key = AFFS_I(inode)->i_ac[idx].key;
  231. goto find_ext;
  232. }
  233. }
  234. /* fall back to the linear cache */
  235. ext_key = AFFS_I(inode)->i_lc[lc_idx];
  236. find_ext:
  237. /* read all extended blocks until we find the one we need */
  238. //unlock cache
  239. do {
  240. bh = affs_bread(sb, ext_key);
  241. if (!bh)
  242. goto err_bread;
  243. ext_key = be32_to_cpu(AFFS_TAIL(sb, bh)->extension);
  244. affs_brelse(bh);
  245. tmp++;
  246. } while (tmp < ext);
  247. //lock cache
  248. /* store it in the associative cache */
  249. // recalculate ac_idx?
  250. AFFS_I(inode)->i_ac[ac_idx].ext = ext;
  251. AFFS_I(inode)->i_ac[ac_idx].key = ext_key;
  252. read_ext:
  253. /* finally read the right extended block */
  254. //unlock cache
  255. bh = affs_bread(sb, ext_key);
  256. if (!bh)
  257. goto err_bread;
  258. //lock cache
  259. store_ext:
  260. /* release old cached extended block and store the new one */
  261. affs_brelse(AFFS_I(inode)->i_ext_bh);
  262. AFFS_I(inode)->i_ext_last = ext;
  263. AFFS_I(inode)->i_ext_bh = bh;
  264. atomic_inc(&bh->b_count);
  265. return bh;
  266. err_bread:
  267. affs_brelse(bh);
  268. return ERR_PTR(-EIO);
  269. }
  270. static int
  271. affs_get_block(struct inode *inode, sector_t block, struct buffer_head *bh_result, int create)
  272. {
  273. struct super_block *sb = inode->i_sb;
  274. struct buffer_head *ext_bh;
  275. u32 ext;
  276. pr_debug("AFFS: get_block(%u, %lu)\n", (u32)inode->i_ino, (unsigned long)block);
  277. if (block > (sector_t)0x7fffffffUL)
  278. BUG();
  279. if (block >= AFFS_I(inode)->i_blkcnt) {
  280. if (block > AFFS_I(inode)->i_blkcnt || !create)
  281. goto err_big;
  282. } else
  283. create = 0;
  284. //lock cache
  285. affs_lock_ext(inode);
  286. ext = (u32)block / AFFS_SB(sb)->s_hashsize;
  287. block -= ext * AFFS_SB(sb)->s_hashsize;
  288. ext_bh = affs_get_extblock(inode, ext);
  289. if (IS_ERR(ext_bh))
  290. goto err_ext;
  291. map_bh(bh_result, sb, (sector_t)be32_to_cpu(AFFS_BLOCK(sb, ext_bh, block)));
  292. if (create) {
  293. u32 blocknr = affs_alloc_block(inode, ext_bh->b_blocknr);
  294. if (!blocknr)
  295. goto err_alloc;
  296. set_buffer_new(bh_result);
  297. AFFS_I(inode)->mmu_private += AFFS_SB(sb)->s_data_blksize;
  298. AFFS_I(inode)->i_blkcnt++;
  299. /* store new block */
  300. if (bh_result->b_blocknr)
  301. affs_warning(sb, "get_block", "block already set (%x)", bh_result->b_blocknr);
  302. AFFS_BLOCK(sb, ext_bh, block) = cpu_to_be32(blocknr);
  303. AFFS_HEAD(ext_bh)->block_count = cpu_to_be32(block + 1);
  304. affs_adjust_checksum(ext_bh, blocknr - bh_result->b_blocknr + 1);
  305. bh_result->b_blocknr = blocknr;
  306. if (!block) {
  307. /* insert first block into header block */
  308. u32 tmp = be32_to_cpu(AFFS_HEAD(ext_bh)->first_data);
  309. if (tmp)
  310. affs_warning(sb, "get_block", "first block already set (%d)", tmp);
  311. AFFS_HEAD(ext_bh)->first_data = cpu_to_be32(blocknr);
  312. affs_adjust_checksum(ext_bh, blocknr - tmp);
  313. }
  314. }
  315. affs_brelse(ext_bh);
  316. //unlock cache
  317. affs_unlock_ext(inode);
  318. return 0;
  319. err_big:
  320. affs_error(inode->i_sb,"get_block","strange block request %d", block);
  321. return -EIO;
  322. err_ext:
  323. // unlock cache
  324. affs_unlock_ext(inode);
  325. return PTR_ERR(ext_bh);
  326. err_alloc:
  327. brelse(ext_bh);
  328. clear_buffer_mapped(bh_result);
  329. bh_result->b_bdev = NULL;
  330. // unlock cache
  331. affs_unlock_ext(inode);
  332. return -ENOSPC;
  333. }
  334. static int affs_writepage(struct page *page, struct writeback_control *wbc)
  335. {
  336. return block_write_full_page(page, affs_get_block, wbc);
  337. }
  338. static int affs_readpage(struct file *file, struct page *page)
  339. {
  340. return block_read_full_page(page, affs_get_block);
  341. }
  342. static int affs_write_begin(struct file *file, struct address_space *mapping,
  343. loff_t pos, unsigned len, unsigned flags,
  344. struct page **pagep, void **fsdata)
  345. {
  346. *pagep = NULL;
  347. return cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
  348. affs_get_block,
  349. &AFFS_I(mapping->host)->mmu_private);
  350. }
  351. static sector_t _affs_bmap(struct address_space *mapping, sector_t block)
  352. {
  353. return generic_block_bmap(mapping,block,affs_get_block);
  354. }
  355. const struct address_space_operations affs_aops = {
  356. .readpage = affs_readpage,
  357. .writepage = affs_writepage,
  358. .sync_page = block_sync_page,
  359. .write_begin = affs_write_begin,
  360. .write_end = generic_write_end,
  361. .bmap = _affs_bmap
  362. };
  363. static inline struct buffer_head *
  364. affs_bread_ino(struct inode *inode, int block, int create)
  365. {
  366. struct buffer_head *bh, tmp_bh;
  367. int err;
  368. tmp_bh.b_state = 0;
  369. err = affs_get_block(inode, block, &tmp_bh, create);
  370. if (!err) {
  371. bh = affs_bread(inode->i_sb, tmp_bh.b_blocknr);
  372. if (bh) {
  373. bh->b_state |= tmp_bh.b_state;
  374. return bh;
  375. }
  376. err = -EIO;
  377. }
  378. return ERR_PTR(err);
  379. }
  380. static inline struct buffer_head *
  381. affs_getzeroblk_ino(struct inode *inode, int block)
  382. {
  383. struct buffer_head *bh, tmp_bh;
  384. int err;
  385. tmp_bh.b_state = 0;
  386. err = affs_get_block(inode, block, &tmp_bh, 1);
  387. if (!err) {
  388. bh = affs_getzeroblk(inode->i_sb, tmp_bh.b_blocknr);
  389. if (bh) {
  390. bh->b_state |= tmp_bh.b_state;
  391. return bh;
  392. }
  393. err = -EIO;
  394. }
  395. return ERR_PTR(err);
  396. }
  397. static inline struct buffer_head *
  398. affs_getemptyblk_ino(struct inode *inode, int block)
  399. {
  400. struct buffer_head *bh, tmp_bh;
  401. int err;
  402. tmp_bh.b_state = 0;
  403. err = affs_get_block(inode, block, &tmp_bh, 1);
  404. if (!err) {
  405. bh = affs_getemptyblk(inode->i_sb, tmp_bh.b_blocknr);
  406. if (bh) {
  407. bh->b_state |= tmp_bh.b_state;
  408. return bh;
  409. }
  410. err = -EIO;
  411. }
  412. return ERR_PTR(err);
  413. }
  414. static int
  415. affs_do_readpage_ofs(struct file *file, struct page *page, unsigned from, unsigned to)
  416. {
  417. struct inode *inode = page->mapping->host;
  418. struct super_block *sb = inode->i_sb;
  419. struct buffer_head *bh;
  420. char *data;
  421. u32 bidx, boff, bsize;
  422. u32 tmp;
  423. pr_debug("AFFS: read_page(%u, %ld, %d, %d)\n", (u32)inode->i_ino, page->index, from, to);
  424. if (from > to || to > PAGE_CACHE_SIZE)
  425. BUG();
  426. kmap(page);
  427. data = page_address(page);
  428. bsize = AFFS_SB(sb)->s_data_blksize;
  429. tmp = (page->index << PAGE_CACHE_SHIFT) + from;
  430. bidx = tmp / bsize;
  431. boff = tmp % bsize;
  432. while (from < to) {
  433. bh = affs_bread_ino(inode, bidx, 0);
  434. if (IS_ERR(bh))
  435. return PTR_ERR(bh);
  436. tmp = min(bsize - boff, to - from);
  437. if (from + tmp > to || tmp > bsize)
  438. BUG();
  439. memcpy(data + from, AFFS_DATA(bh) + boff, tmp);
  440. affs_brelse(bh);
  441. bidx++;
  442. from += tmp;
  443. boff = 0;
  444. }
  445. flush_dcache_page(page);
  446. kunmap(page);
  447. return 0;
  448. }
  449. static int
  450. affs_extent_file_ofs(struct inode *inode, u32 newsize)
  451. {
  452. struct super_block *sb = inode->i_sb;
  453. struct buffer_head *bh, *prev_bh;
  454. u32 bidx, boff;
  455. u32 size, bsize;
  456. u32 tmp;
  457. pr_debug("AFFS: extent_file(%u, %d)\n", (u32)inode->i_ino, newsize);
  458. bsize = AFFS_SB(sb)->s_data_blksize;
  459. bh = NULL;
  460. size = AFFS_I(inode)->mmu_private;
  461. bidx = size / bsize;
  462. boff = size % bsize;
  463. if (boff) {
  464. bh = affs_bread_ino(inode, bidx, 0);
  465. if (IS_ERR(bh))
  466. return PTR_ERR(bh);
  467. tmp = min(bsize - boff, newsize - size);
  468. if (boff + tmp > bsize || tmp > bsize)
  469. BUG();
  470. memset(AFFS_DATA(bh) + boff, 0, tmp);
  471. AFFS_DATA_HEAD(bh)->size = cpu_to_be32(be32_to_cpu(AFFS_DATA_HEAD(bh)->size) + tmp);
  472. affs_fix_checksum(sb, bh);
  473. mark_buffer_dirty_inode(bh, inode);
  474. size += tmp;
  475. bidx++;
  476. } else if (bidx) {
  477. bh = affs_bread_ino(inode, bidx - 1, 0);
  478. if (IS_ERR(bh))
  479. return PTR_ERR(bh);
  480. }
  481. while (size < newsize) {
  482. prev_bh = bh;
  483. bh = affs_getzeroblk_ino(inode, bidx);
  484. if (IS_ERR(bh))
  485. goto out;
  486. tmp = min(bsize, newsize - size);
  487. if (tmp > bsize)
  488. BUG();
  489. AFFS_DATA_HEAD(bh)->ptype = cpu_to_be32(T_DATA);
  490. AFFS_DATA_HEAD(bh)->key = cpu_to_be32(inode->i_ino);
  491. AFFS_DATA_HEAD(bh)->sequence = cpu_to_be32(bidx);
  492. AFFS_DATA_HEAD(bh)->size = cpu_to_be32(tmp);
  493. affs_fix_checksum(sb, bh);
  494. bh->b_state &= ~(1UL << BH_New);
  495. mark_buffer_dirty_inode(bh, inode);
  496. if (prev_bh) {
  497. u32 tmp = be32_to_cpu(AFFS_DATA_HEAD(prev_bh)->next);
  498. if (tmp)
  499. affs_warning(sb, "extent_file_ofs", "next block already set for %d (%d)", bidx, tmp);
  500. AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr);
  501. affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp);
  502. mark_buffer_dirty_inode(prev_bh, inode);
  503. affs_brelse(prev_bh);
  504. }
  505. size += bsize;
  506. bidx++;
  507. }
  508. affs_brelse(bh);
  509. inode->i_size = AFFS_I(inode)->mmu_private = newsize;
  510. return 0;
  511. out:
  512. inode->i_size = AFFS_I(inode)->mmu_private = newsize;
  513. return PTR_ERR(bh);
  514. }
  515. static int
  516. affs_readpage_ofs(struct file *file, struct page *page)
  517. {
  518. struct inode *inode = page->mapping->host;
  519. u32 to;
  520. int err;
  521. pr_debug("AFFS: read_page(%u, %ld)\n", (u32)inode->i_ino, page->index);
  522. to = PAGE_CACHE_SIZE;
  523. if (((page->index + 1) << PAGE_CACHE_SHIFT) > inode->i_size) {
  524. to = inode->i_size & ~PAGE_CACHE_MASK;
  525. memset(page_address(page) + to, 0, PAGE_CACHE_SIZE - to);
  526. }
  527. err = affs_do_readpage_ofs(file, page, 0, to);
  528. if (!err)
  529. SetPageUptodate(page);
  530. unlock_page(page);
  531. return err;
  532. }
  533. static int affs_write_begin_ofs(struct file *file, struct address_space *mapping,
  534. loff_t pos, unsigned len, unsigned flags,
  535. struct page **pagep, void **fsdata)
  536. {
  537. struct inode *inode = mapping->host;
  538. struct page *page;
  539. pgoff_t index;
  540. int err = 0;
  541. pr_debug("AFFS: write_begin(%u, %llu, %llu)\n", (u32)inode->i_ino, (unsigned long long)pos, (unsigned long long)pos + len);
  542. if (pos > AFFS_I(inode)->mmu_private) {
  543. /* XXX: this probably leaves a too-big i_size in case of
  544. * failure. Should really be updating i_size at write_end time
  545. */
  546. err = affs_extent_file_ofs(inode, pos);
  547. if (err)
  548. return err;
  549. }
  550. index = pos >> PAGE_CACHE_SHIFT;
  551. page = __grab_cache_page(mapping, index);
  552. if (!page)
  553. return -ENOMEM;
  554. *pagep = page;
  555. if (PageUptodate(page))
  556. return 0;
  557. /* XXX: inefficient but safe in the face of short writes */
  558. err = affs_do_readpage_ofs(file, page, 0, PAGE_CACHE_SIZE);
  559. if (err) {
  560. unlock_page(page);
  561. page_cache_release(page);
  562. }
  563. return err;
  564. }
  565. static int affs_write_end_ofs(struct file *file, struct address_space *mapping,
  566. loff_t pos, unsigned len, unsigned copied,
  567. struct page *page, void *fsdata)
  568. {
  569. struct inode *inode = mapping->host;
  570. struct super_block *sb = inode->i_sb;
  571. struct buffer_head *bh, *prev_bh;
  572. char *data;
  573. u32 bidx, boff, bsize;
  574. unsigned from, to;
  575. u32 tmp;
  576. int written;
  577. from = pos & (PAGE_CACHE_SIZE - 1);
  578. to = pos + len;
  579. /*
  580. * XXX: not sure if this can handle short copies (len < copied), but
  581. * we don't have to, because the page should always be uptodate here,
  582. * due to write_begin.
  583. */
  584. pr_debug("AFFS: write_begin(%u, %llu, %llu)\n", (u32)inode->i_ino, (unsigned long long)pos, (unsigned long long)pos + len);
  585. bsize = AFFS_SB(sb)->s_data_blksize;
  586. data = page_address(page);
  587. bh = NULL;
  588. written = 0;
  589. tmp = (page->index << PAGE_CACHE_SHIFT) + from;
  590. bidx = tmp / bsize;
  591. boff = tmp % bsize;
  592. if (boff) {
  593. bh = affs_bread_ino(inode, bidx, 0);
  594. if (IS_ERR(bh))
  595. return PTR_ERR(bh);
  596. tmp = min(bsize - boff, to - from);
  597. if (boff + tmp > bsize || tmp > bsize)
  598. BUG();
  599. memcpy(AFFS_DATA(bh) + boff, data + from, tmp);
  600. AFFS_DATA_HEAD(bh)->size = cpu_to_be32(be32_to_cpu(AFFS_DATA_HEAD(bh)->size) + tmp);
  601. affs_fix_checksum(sb, bh);
  602. mark_buffer_dirty_inode(bh, inode);
  603. written += tmp;
  604. from += tmp;
  605. bidx++;
  606. } else if (bidx) {
  607. bh = affs_bread_ino(inode, bidx - 1, 0);
  608. if (IS_ERR(bh))
  609. return PTR_ERR(bh);
  610. }
  611. while (from + bsize <= to) {
  612. prev_bh = bh;
  613. bh = affs_getemptyblk_ino(inode, bidx);
  614. if (IS_ERR(bh))
  615. goto out;
  616. memcpy(AFFS_DATA(bh), data + from, bsize);
  617. if (buffer_new(bh)) {
  618. AFFS_DATA_HEAD(bh)->ptype = cpu_to_be32(T_DATA);
  619. AFFS_DATA_HEAD(bh)->key = cpu_to_be32(inode->i_ino);
  620. AFFS_DATA_HEAD(bh)->sequence = cpu_to_be32(bidx);
  621. AFFS_DATA_HEAD(bh)->size = cpu_to_be32(bsize);
  622. AFFS_DATA_HEAD(bh)->next = 0;
  623. bh->b_state &= ~(1UL << BH_New);
  624. if (prev_bh) {
  625. u32 tmp = be32_to_cpu(AFFS_DATA_HEAD(prev_bh)->next);
  626. if (tmp)
  627. affs_warning(sb, "commit_write_ofs", "next block already set for %d (%d)", bidx, tmp);
  628. AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr);
  629. affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp);
  630. mark_buffer_dirty_inode(prev_bh, inode);
  631. }
  632. }
  633. affs_brelse(prev_bh);
  634. affs_fix_checksum(sb, bh);
  635. mark_buffer_dirty_inode(bh, inode);
  636. written += bsize;
  637. from += bsize;
  638. bidx++;
  639. }
  640. if (from < to) {
  641. prev_bh = bh;
  642. bh = affs_bread_ino(inode, bidx, 1);
  643. if (IS_ERR(bh))
  644. goto out;
  645. tmp = min(bsize, to - from);
  646. if (tmp > bsize)
  647. BUG();
  648. memcpy(AFFS_DATA(bh), data + from, tmp);
  649. if (buffer_new(bh)) {
  650. AFFS_DATA_HEAD(bh)->ptype = cpu_to_be32(T_DATA);
  651. AFFS_DATA_HEAD(bh)->key = cpu_to_be32(inode->i_ino);
  652. AFFS_DATA_HEAD(bh)->sequence = cpu_to_be32(bidx);
  653. AFFS_DATA_HEAD(bh)->size = cpu_to_be32(tmp);
  654. AFFS_DATA_HEAD(bh)->next = 0;
  655. bh->b_state &= ~(1UL << BH_New);
  656. if (prev_bh) {
  657. u32 tmp = be32_to_cpu(AFFS_DATA_HEAD(prev_bh)->next);
  658. if (tmp)
  659. affs_warning(sb, "commit_write_ofs", "next block already set for %d (%d)", bidx, tmp);
  660. AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr);
  661. affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp);
  662. mark_buffer_dirty_inode(prev_bh, inode);
  663. }
  664. } else if (be32_to_cpu(AFFS_DATA_HEAD(bh)->size) < tmp)
  665. AFFS_DATA_HEAD(bh)->size = cpu_to_be32(tmp);
  666. affs_brelse(prev_bh);
  667. affs_fix_checksum(sb, bh);
  668. mark_buffer_dirty_inode(bh, inode);
  669. written += tmp;
  670. from += tmp;
  671. bidx++;
  672. }
  673. SetPageUptodate(page);
  674. done:
  675. affs_brelse(bh);
  676. tmp = (page->index << PAGE_CACHE_SHIFT) + from;
  677. if (tmp > inode->i_size)
  678. inode->i_size = AFFS_I(inode)->mmu_private = tmp;
  679. unlock_page(page);
  680. page_cache_release(page);
  681. return written;
  682. out:
  683. bh = prev_bh;
  684. if (!written)
  685. written = PTR_ERR(bh);
  686. goto done;
  687. }
  688. const struct address_space_operations affs_aops_ofs = {
  689. .readpage = affs_readpage_ofs,
  690. //.writepage = affs_writepage_ofs,
  691. //.sync_page = affs_sync_page_ofs,
  692. .write_begin = affs_write_begin_ofs,
  693. .write_end = affs_write_end_ofs
  694. };
  695. /* Free any preallocated blocks. */
  696. void
  697. affs_free_prealloc(struct inode *inode)
  698. {
  699. struct super_block *sb = inode->i_sb;
  700. pr_debug("AFFS: free_prealloc(ino=%lu)\n", inode->i_ino);
  701. while (AFFS_I(inode)->i_pa_cnt) {
  702. AFFS_I(inode)->i_pa_cnt--;
  703. affs_free_block(sb, ++AFFS_I(inode)->i_lastalloc);
  704. }
  705. }
  706. /* Truncate (or enlarge) a file to the requested size. */
  707. void
  708. affs_truncate(struct inode *inode)
  709. {
  710. struct super_block *sb = inode->i_sb;
  711. u32 ext, ext_key;
  712. u32 last_blk, blkcnt, blk;
  713. u32 size;
  714. struct buffer_head *ext_bh;
  715. int i;
  716. pr_debug("AFFS: truncate(inode=%d, oldsize=%u, newsize=%u)\n",
  717. (u32)inode->i_ino, (u32)AFFS_I(inode)->mmu_private, (u32)inode->i_size);
  718. last_blk = 0;
  719. ext = 0;
  720. if (inode->i_size) {
  721. last_blk = ((u32)inode->i_size - 1) / AFFS_SB(sb)->s_data_blksize;
  722. ext = last_blk / AFFS_SB(sb)->s_hashsize;
  723. }
  724. if (inode->i_size > AFFS_I(inode)->mmu_private) {
  725. struct address_space *mapping = inode->i_mapping;
  726. struct page *page;
  727. void *fsdata;
  728. u32 size = inode->i_size;
  729. int res;
  730. res = mapping->a_ops->write_begin(NULL, mapping, size, 0, 0, &page, &fsdata);
  731. if (!res)
  732. res = mapping->a_ops->write_end(NULL, mapping, size, 0, 0, page, fsdata);
  733. mark_inode_dirty(inode);
  734. return;
  735. } else if (inode->i_size == AFFS_I(inode)->mmu_private)
  736. return;
  737. // lock cache
  738. ext_bh = affs_get_extblock(inode, ext);
  739. if (IS_ERR(ext_bh)) {
  740. affs_warning(sb, "truncate", "unexpected read error for ext block %u (%d)",
  741. ext, PTR_ERR(ext_bh));
  742. return;
  743. }
  744. if (AFFS_I(inode)->i_lc) {
  745. /* clear linear cache */
  746. i = (ext + 1) >> AFFS_I(inode)->i_lc_shift;
  747. if (AFFS_I(inode)->i_lc_size > i) {
  748. AFFS_I(inode)->i_lc_size = i;
  749. for (; i < AFFS_LC_SIZE; i++)
  750. AFFS_I(inode)->i_lc[i] = 0;
  751. }
  752. /* clear associative cache */
  753. for (i = 0; i < AFFS_AC_SIZE; i++)
  754. if (AFFS_I(inode)->i_ac[i].ext >= ext)
  755. AFFS_I(inode)->i_ac[i].ext = 0;
  756. }
  757. ext_key = be32_to_cpu(AFFS_TAIL(sb, ext_bh)->extension);
  758. blkcnt = AFFS_I(inode)->i_blkcnt;
  759. i = 0;
  760. blk = last_blk;
  761. if (inode->i_size) {
  762. i = last_blk % AFFS_SB(sb)->s_hashsize + 1;
  763. blk++;
  764. } else
  765. AFFS_HEAD(ext_bh)->first_data = 0;
  766. size = AFFS_SB(sb)->s_hashsize;
  767. if (size > blkcnt - blk + i)
  768. size = blkcnt - blk + i;
  769. for (; i < size; i++, blk++) {
  770. affs_free_block(sb, be32_to_cpu(AFFS_BLOCK(sb, ext_bh, i)));
  771. AFFS_BLOCK(sb, ext_bh, i) = 0;
  772. }
  773. AFFS_TAIL(sb, ext_bh)->extension = 0;
  774. affs_fix_checksum(sb, ext_bh);
  775. mark_buffer_dirty_inode(ext_bh, inode);
  776. affs_brelse(ext_bh);
  777. if (inode->i_size) {
  778. AFFS_I(inode)->i_blkcnt = last_blk + 1;
  779. AFFS_I(inode)->i_extcnt = ext + 1;
  780. if (AFFS_SB(sb)->s_flags & SF_OFS) {
  781. struct buffer_head *bh = affs_bread_ino(inode, last_blk, 0);
  782. u32 tmp;
  783. if (IS_ERR(ext_bh)) {
  784. affs_warning(sb, "truncate", "unexpected read error for last block %u (%d)",
  785. ext, PTR_ERR(ext_bh));
  786. return;
  787. }
  788. tmp = be32_to_cpu(AFFS_DATA_HEAD(bh)->next);
  789. AFFS_DATA_HEAD(bh)->next = 0;
  790. affs_adjust_checksum(bh, -tmp);
  791. affs_brelse(bh);
  792. }
  793. } else {
  794. AFFS_I(inode)->i_blkcnt = 0;
  795. AFFS_I(inode)->i_extcnt = 1;
  796. }
  797. AFFS_I(inode)->mmu_private = inode->i_size;
  798. // unlock cache
  799. while (ext_key) {
  800. ext_bh = affs_bread(sb, ext_key);
  801. size = AFFS_SB(sb)->s_hashsize;
  802. if (size > blkcnt - blk)
  803. size = blkcnt - blk;
  804. for (i = 0; i < size; i++, blk++)
  805. affs_free_block(sb, be32_to_cpu(AFFS_BLOCK(sb, ext_bh, i)));
  806. affs_free_block(sb, ext_key);
  807. ext_key = be32_to_cpu(AFFS_TAIL(sb, ext_bh)->extension);
  808. affs_brelse(ext_bh);
  809. }
  810. affs_free_prealloc(inode);
  811. }