file.c 24 KB

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