aops.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * Copyright (C) 2002, 2004 Oracle. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public
  17. * License along with this program; if not, write to the
  18. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. * Boston, MA 021110-1307, USA.
  20. */
  21. #include <linux/fs.h>
  22. #include <linux/slab.h>
  23. #include <linux/highmem.h>
  24. #include <linux/pagemap.h>
  25. #include <asm/byteorder.h>
  26. #include <linux/swap.h>
  27. #include <linux/pipe_fs_i.h>
  28. #include <linux/mpage.h>
  29. #define MLOG_MASK_PREFIX ML_FILE_IO
  30. #include <cluster/masklog.h>
  31. #include "ocfs2.h"
  32. #include "alloc.h"
  33. #include "aops.h"
  34. #include "dlmglue.h"
  35. #include "extent_map.h"
  36. #include "file.h"
  37. #include "inode.h"
  38. #include "journal.h"
  39. #include "suballoc.h"
  40. #include "super.h"
  41. #include "symlink.h"
  42. #include "buffer_head_io.h"
  43. static int ocfs2_symlink_get_block(struct inode *inode, sector_t iblock,
  44. struct buffer_head *bh_result, int create)
  45. {
  46. int err = -EIO;
  47. int status;
  48. struct ocfs2_dinode *fe = NULL;
  49. struct buffer_head *bh = NULL;
  50. struct buffer_head *buffer_cache_bh = NULL;
  51. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  52. void *kaddr;
  53. mlog_entry("(0x%p, %llu, 0x%p, %d)\n", inode,
  54. (unsigned long long)iblock, bh_result, create);
  55. BUG_ON(ocfs2_inode_is_fast_symlink(inode));
  56. if ((iblock << inode->i_sb->s_blocksize_bits) > PATH_MAX + 1) {
  57. mlog(ML_ERROR, "block offset > PATH_MAX: %llu",
  58. (unsigned long long)iblock);
  59. goto bail;
  60. }
  61. status = ocfs2_read_inode_block(inode, &bh);
  62. if (status < 0) {
  63. mlog_errno(status);
  64. goto bail;
  65. }
  66. fe = (struct ocfs2_dinode *) bh->b_data;
  67. if ((u64)iblock >= ocfs2_clusters_to_blocks(inode->i_sb,
  68. le32_to_cpu(fe->i_clusters))) {
  69. mlog(ML_ERROR, "block offset is outside the allocated size: "
  70. "%llu\n", (unsigned long long)iblock);
  71. goto bail;
  72. }
  73. /* We don't use the page cache to create symlink data, so if
  74. * need be, copy it over from the buffer cache. */
  75. if (!buffer_uptodate(bh_result) && ocfs2_inode_is_new(inode)) {
  76. u64 blkno = le64_to_cpu(fe->id2.i_list.l_recs[0].e_blkno) +
  77. iblock;
  78. buffer_cache_bh = sb_getblk(osb->sb, blkno);
  79. if (!buffer_cache_bh) {
  80. mlog(ML_ERROR, "couldn't getblock for symlink!\n");
  81. goto bail;
  82. }
  83. /* we haven't locked out transactions, so a commit
  84. * could've happened. Since we've got a reference on
  85. * the bh, even if it commits while we're doing the
  86. * copy, the data is still good. */
  87. if (buffer_jbd(buffer_cache_bh)
  88. && ocfs2_inode_is_new(inode)) {
  89. kaddr = kmap_atomic(bh_result->b_page, KM_USER0);
  90. if (!kaddr) {
  91. mlog(ML_ERROR, "couldn't kmap!\n");
  92. goto bail;
  93. }
  94. memcpy(kaddr + (bh_result->b_size * iblock),
  95. buffer_cache_bh->b_data,
  96. bh_result->b_size);
  97. kunmap_atomic(kaddr, KM_USER0);
  98. set_buffer_uptodate(bh_result);
  99. }
  100. brelse(buffer_cache_bh);
  101. }
  102. map_bh(bh_result, inode->i_sb,
  103. le64_to_cpu(fe->id2.i_list.l_recs[0].e_blkno) + iblock);
  104. err = 0;
  105. bail:
  106. brelse(bh);
  107. mlog_exit(err);
  108. return err;
  109. }
  110. static int ocfs2_get_block(struct inode *inode, sector_t iblock,
  111. struct buffer_head *bh_result, int create)
  112. {
  113. int err = 0;
  114. unsigned int ext_flags;
  115. u64 max_blocks = bh_result->b_size >> inode->i_blkbits;
  116. u64 p_blkno, count, past_eof;
  117. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  118. mlog_entry("(0x%p, %llu, 0x%p, %d)\n", inode,
  119. (unsigned long long)iblock, bh_result, create);
  120. if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_SYSTEM_FILE)
  121. mlog(ML_NOTICE, "get_block on system inode 0x%p (%lu)\n",
  122. inode, inode->i_ino);
  123. if (S_ISLNK(inode->i_mode)) {
  124. /* this always does I/O for some reason. */
  125. err = ocfs2_symlink_get_block(inode, iblock, bh_result, create);
  126. goto bail;
  127. }
  128. err = ocfs2_extent_map_get_blocks(inode, iblock, &p_blkno, &count,
  129. &ext_flags);
  130. if (err) {
  131. mlog(ML_ERROR, "Error %d from get_blocks(0x%p, %llu, 1, "
  132. "%llu, NULL)\n", err, inode, (unsigned long long)iblock,
  133. (unsigned long long)p_blkno);
  134. goto bail;
  135. }
  136. if (max_blocks < count)
  137. count = max_blocks;
  138. /*
  139. * ocfs2 never allocates in this function - the only time we
  140. * need to use BH_New is when we're extending i_size on a file
  141. * system which doesn't support holes, in which case BH_New
  142. * allows block_prepare_write() to zero.
  143. *
  144. * If we see this on a sparse file system, then a truncate has
  145. * raced us and removed the cluster. In this case, we clear
  146. * the buffers dirty and uptodate bits and let the buffer code
  147. * ignore it as a hole.
  148. */
  149. if (create && p_blkno == 0 && ocfs2_sparse_alloc(osb)) {
  150. clear_buffer_dirty(bh_result);
  151. clear_buffer_uptodate(bh_result);
  152. goto bail;
  153. }
  154. /* Treat the unwritten extent as a hole for zeroing purposes. */
  155. if (p_blkno && !(ext_flags & OCFS2_EXT_UNWRITTEN))
  156. map_bh(bh_result, inode->i_sb, p_blkno);
  157. bh_result->b_size = count << inode->i_blkbits;
  158. if (!ocfs2_sparse_alloc(osb)) {
  159. if (p_blkno == 0) {
  160. err = -EIO;
  161. mlog(ML_ERROR,
  162. "iblock = %llu p_blkno = %llu blkno=(%llu)\n",
  163. (unsigned long long)iblock,
  164. (unsigned long long)p_blkno,
  165. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  166. mlog(ML_ERROR, "Size %llu, clusters %u\n", (unsigned long long)i_size_read(inode), OCFS2_I(inode)->ip_clusters);
  167. dump_stack();
  168. }
  169. past_eof = ocfs2_blocks_for_bytes(inode->i_sb, i_size_read(inode));
  170. mlog(0, "Inode %lu, past_eof = %llu\n", inode->i_ino,
  171. (unsigned long long)past_eof);
  172. if (create && (iblock >= past_eof))
  173. set_buffer_new(bh_result);
  174. }
  175. bail:
  176. if (err < 0)
  177. err = -EIO;
  178. mlog_exit(err);
  179. return err;
  180. }
  181. int ocfs2_read_inline_data(struct inode *inode, struct page *page,
  182. struct buffer_head *di_bh)
  183. {
  184. void *kaddr;
  185. loff_t size;
  186. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  187. if (!(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL)) {
  188. ocfs2_error(inode->i_sb, "Inode %llu lost inline data flag",
  189. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  190. return -EROFS;
  191. }
  192. size = i_size_read(inode);
  193. if (size > PAGE_CACHE_SIZE ||
  194. size > ocfs2_max_inline_data(inode->i_sb)) {
  195. ocfs2_error(inode->i_sb,
  196. "Inode %llu has with inline data has bad size: %Lu",
  197. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  198. (unsigned long long)size);
  199. return -EROFS;
  200. }
  201. kaddr = kmap_atomic(page, KM_USER0);
  202. if (size)
  203. memcpy(kaddr, di->id2.i_data.id_data, size);
  204. /* Clear the remaining part of the page */
  205. memset(kaddr + size, 0, PAGE_CACHE_SIZE - size);
  206. flush_dcache_page(page);
  207. kunmap_atomic(kaddr, KM_USER0);
  208. SetPageUptodate(page);
  209. return 0;
  210. }
  211. static int ocfs2_readpage_inline(struct inode *inode, struct page *page)
  212. {
  213. int ret;
  214. struct buffer_head *di_bh = NULL;
  215. BUG_ON(!PageLocked(page));
  216. BUG_ON(!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL));
  217. ret = ocfs2_read_inode_block(inode, &di_bh);
  218. if (ret) {
  219. mlog_errno(ret);
  220. goto out;
  221. }
  222. ret = ocfs2_read_inline_data(inode, page, di_bh);
  223. out:
  224. unlock_page(page);
  225. brelse(di_bh);
  226. return ret;
  227. }
  228. static int ocfs2_readpage(struct file *file, struct page *page)
  229. {
  230. struct inode *inode = page->mapping->host;
  231. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  232. loff_t start = (loff_t)page->index << PAGE_CACHE_SHIFT;
  233. int ret, unlock = 1;
  234. mlog_entry("(0x%p, %lu)\n", file, (page ? page->index : 0));
  235. ret = ocfs2_inode_lock_with_page(inode, NULL, 0, page);
  236. if (ret != 0) {
  237. if (ret == AOP_TRUNCATED_PAGE)
  238. unlock = 0;
  239. mlog_errno(ret);
  240. goto out;
  241. }
  242. if (down_read_trylock(&oi->ip_alloc_sem) == 0) {
  243. ret = AOP_TRUNCATED_PAGE;
  244. goto out_inode_unlock;
  245. }
  246. /*
  247. * i_size might have just been updated as we grabed the meta lock. We
  248. * might now be discovering a truncate that hit on another node.
  249. * block_read_full_page->get_block freaks out if it is asked to read
  250. * beyond the end of a file, so we check here. Callers
  251. * (generic_file_read, vm_ops->fault) are clever enough to check i_size
  252. * and notice that the page they just read isn't needed.
  253. *
  254. * XXX sys_readahead() seems to get that wrong?
  255. */
  256. if (start >= i_size_read(inode)) {
  257. zero_user(page, 0, PAGE_SIZE);
  258. SetPageUptodate(page);
  259. ret = 0;
  260. goto out_alloc;
  261. }
  262. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  263. ret = ocfs2_readpage_inline(inode, page);
  264. else
  265. ret = block_read_full_page(page, ocfs2_get_block);
  266. unlock = 0;
  267. out_alloc:
  268. up_read(&OCFS2_I(inode)->ip_alloc_sem);
  269. out_inode_unlock:
  270. ocfs2_inode_unlock(inode, 0);
  271. out:
  272. if (unlock)
  273. unlock_page(page);
  274. mlog_exit(ret);
  275. return ret;
  276. }
  277. /*
  278. * This is used only for read-ahead. Failures or difficult to handle
  279. * situations are safe to ignore.
  280. *
  281. * Right now, we don't bother with BH_Boundary - in-inode extent lists
  282. * are quite large (243 extents on 4k blocks), so most inodes don't
  283. * grow out to a tree. If need be, detecting boundary extents could
  284. * trivially be added in a future version of ocfs2_get_block().
  285. */
  286. static int ocfs2_readpages(struct file *filp, struct address_space *mapping,
  287. struct list_head *pages, unsigned nr_pages)
  288. {
  289. int ret, err = -EIO;
  290. struct inode *inode = mapping->host;
  291. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  292. loff_t start;
  293. struct page *last;
  294. /*
  295. * Use the nonblocking flag for the dlm code to avoid page
  296. * lock inversion, but don't bother with retrying.
  297. */
  298. ret = ocfs2_inode_lock_full(inode, NULL, 0, OCFS2_LOCK_NONBLOCK);
  299. if (ret)
  300. return err;
  301. if (down_read_trylock(&oi->ip_alloc_sem) == 0) {
  302. ocfs2_inode_unlock(inode, 0);
  303. return err;
  304. }
  305. /*
  306. * Don't bother with inline-data. There isn't anything
  307. * to read-ahead in that case anyway...
  308. */
  309. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  310. goto out_unlock;
  311. /*
  312. * Check whether a remote node truncated this file - we just
  313. * drop out in that case as it's not worth handling here.
  314. */
  315. last = list_entry(pages->prev, struct page, lru);
  316. start = (loff_t)last->index << PAGE_CACHE_SHIFT;
  317. if (start >= i_size_read(inode))
  318. goto out_unlock;
  319. err = mpage_readpages(mapping, pages, nr_pages, ocfs2_get_block);
  320. out_unlock:
  321. up_read(&oi->ip_alloc_sem);
  322. ocfs2_inode_unlock(inode, 0);
  323. return err;
  324. }
  325. /* Note: Because we don't support holes, our allocation has
  326. * already happened (allocation writes zeros to the file data)
  327. * so we don't have to worry about ordered writes in
  328. * ocfs2_writepage.
  329. *
  330. * ->writepage is called during the process of invalidating the page cache
  331. * during blocked lock processing. It can't block on any cluster locks
  332. * to during block mapping. It's relying on the fact that the block
  333. * mapping can't have disappeared under the dirty pages that it is
  334. * being asked to write back.
  335. */
  336. static int ocfs2_writepage(struct page *page, struct writeback_control *wbc)
  337. {
  338. int ret;
  339. mlog_entry("(0x%p)\n", page);
  340. ret = block_write_full_page(page, ocfs2_get_block, wbc);
  341. mlog_exit(ret);
  342. return ret;
  343. }
  344. /*
  345. * This is called from ocfs2_write_zero_page() which has handled it's
  346. * own cluster locking and has ensured allocation exists for those
  347. * blocks to be written.
  348. */
  349. int ocfs2_prepare_write_nolock(struct inode *inode, struct page *page,
  350. unsigned from, unsigned to)
  351. {
  352. int ret;
  353. ret = block_prepare_write(page, from, to, ocfs2_get_block);
  354. return ret;
  355. }
  356. /* Taken from ext3. We don't necessarily need the full blown
  357. * functionality yet, but IMHO it's better to cut and paste the whole
  358. * thing so we can avoid introducing our own bugs (and easily pick up
  359. * their fixes when they happen) --Mark */
  360. int walk_page_buffers( handle_t *handle,
  361. struct buffer_head *head,
  362. unsigned from,
  363. unsigned to,
  364. int *partial,
  365. int (*fn)( handle_t *handle,
  366. struct buffer_head *bh))
  367. {
  368. struct buffer_head *bh;
  369. unsigned block_start, block_end;
  370. unsigned blocksize = head->b_size;
  371. int err, ret = 0;
  372. struct buffer_head *next;
  373. for ( bh = head, block_start = 0;
  374. ret == 0 && (bh != head || !block_start);
  375. block_start = block_end, bh = next)
  376. {
  377. next = bh->b_this_page;
  378. block_end = block_start + blocksize;
  379. if (block_end <= from || block_start >= to) {
  380. if (partial && !buffer_uptodate(bh))
  381. *partial = 1;
  382. continue;
  383. }
  384. err = (*fn)(handle, bh);
  385. if (!ret)
  386. ret = err;
  387. }
  388. return ret;
  389. }
  390. handle_t *ocfs2_start_walk_page_trans(struct inode *inode,
  391. struct page *page,
  392. unsigned from,
  393. unsigned to)
  394. {
  395. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  396. handle_t *handle;
  397. int ret = 0;
  398. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  399. if (IS_ERR(handle)) {
  400. ret = -ENOMEM;
  401. mlog_errno(ret);
  402. goto out;
  403. }
  404. if (ocfs2_should_order_data(inode)) {
  405. ret = ocfs2_jbd2_file_inode(handle, inode);
  406. #ifdef CONFIG_OCFS2_COMPAT_JBD
  407. ret = walk_page_buffers(handle,
  408. page_buffers(page),
  409. from, to, NULL,
  410. ocfs2_journal_dirty_data);
  411. #endif
  412. if (ret < 0)
  413. mlog_errno(ret);
  414. }
  415. out:
  416. if (ret) {
  417. if (!IS_ERR(handle))
  418. ocfs2_commit_trans(osb, handle);
  419. handle = ERR_PTR(ret);
  420. }
  421. return handle;
  422. }
  423. static sector_t ocfs2_bmap(struct address_space *mapping, sector_t block)
  424. {
  425. sector_t status;
  426. u64 p_blkno = 0;
  427. int err = 0;
  428. struct inode *inode = mapping->host;
  429. mlog_entry("(block = %llu)\n", (unsigned long long)block);
  430. /* We don't need to lock journal system files, since they aren't
  431. * accessed concurrently from multiple nodes.
  432. */
  433. if (!INODE_JOURNAL(inode)) {
  434. err = ocfs2_inode_lock(inode, NULL, 0);
  435. if (err) {
  436. if (err != -ENOENT)
  437. mlog_errno(err);
  438. goto bail;
  439. }
  440. down_read(&OCFS2_I(inode)->ip_alloc_sem);
  441. }
  442. if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL))
  443. err = ocfs2_extent_map_get_blocks(inode, block, &p_blkno, NULL,
  444. NULL);
  445. if (!INODE_JOURNAL(inode)) {
  446. up_read(&OCFS2_I(inode)->ip_alloc_sem);
  447. ocfs2_inode_unlock(inode, 0);
  448. }
  449. if (err) {
  450. mlog(ML_ERROR, "get_blocks() failed, block = %llu\n",
  451. (unsigned long long)block);
  452. mlog_errno(err);
  453. goto bail;
  454. }
  455. bail:
  456. status = err ? 0 : p_blkno;
  457. mlog_exit((int)status);
  458. return status;
  459. }
  460. /*
  461. * TODO: Make this into a generic get_blocks function.
  462. *
  463. * From do_direct_io in direct-io.c:
  464. * "So what we do is to permit the ->get_blocks function to populate
  465. * bh.b_size with the size of IO which is permitted at this offset and
  466. * this i_blkbits."
  467. *
  468. * This function is called directly from get_more_blocks in direct-io.c.
  469. *
  470. * called like this: dio->get_blocks(dio->inode, fs_startblk,
  471. * fs_count, map_bh, dio->rw == WRITE);
  472. */
  473. static int ocfs2_direct_IO_get_blocks(struct inode *inode, sector_t iblock,
  474. struct buffer_head *bh_result, int create)
  475. {
  476. int ret;
  477. u64 p_blkno, inode_blocks, contig_blocks;
  478. unsigned int ext_flags;
  479. unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
  480. unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
  481. /* This function won't even be called if the request isn't all
  482. * nicely aligned and of the right size, so there's no need
  483. * for us to check any of that. */
  484. inode_blocks = ocfs2_blocks_for_bytes(inode->i_sb, i_size_read(inode));
  485. /*
  486. * Any write past EOF is not allowed because we'd be extending.
  487. */
  488. if (create && (iblock + max_blocks) > inode_blocks) {
  489. ret = -EIO;
  490. goto bail;
  491. }
  492. /* This figures out the size of the next contiguous block, and
  493. * our logical offset */
  494. ret = ocfs2_extent_map_get_blocks(inode, iblock, &p_blkno,
  495. &contig_blocks, &ext_flags);
  496. if (ret) {
  497. mlog(ML_ERROR, "get_blocks() failed iblock=%llu\n",
  498. (unsigned long long)iblock);
  499. ret = -EIO;
  500. goto bail;
  501. }
  502. if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)) && !p_blkno && create) {
  503. ocfs2_error(inode->i_sb,
  504. "Inode %llu has a hole at block %llu\n",
  505. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  506. (unsigned long long)iblock);
  507. ret = -EROFS;
  508. goto bail;
  509. }
  510. /*
  511. * get_more_blocks() expects us to describe a hole by clearing
  512. * the mapped bit on bh_result().
  513. *
  514. * Consider an unwritten extent as a hole.
  515. */
  516. if (p_blkno && !(ext_flags & OCFS2_EXT_UNWRITTEN))
  517. map_bh(bh_result, inode->i_sb, p_blkno);
  518. else {
  519. /*
  520. * ocfs2_prepare_inode_for_write() should have caught
  521. * the case where we'd be filling a hole and triggered
  522. * a buffered write instead.
  523. */
  524. if (create) {
  525. ret = -EIO;
  526. mlog_errno(ret);
  527. goto bail;
  528. }
  529. clear_buffer_mapped(bh_result);
  530. }
  531. /* make sure we don't map more than max_blocks blocks here as
  532. that's all the kernel will handle at this point. */
  533. if (max_blocks < contig_blocks)
  534. contig_blocks = max_blocks;
  535. bh_result->b_size = contig_blocks << blocksize_bits;
  536. bail:
  537. return ret;
  538. }
  539. /*
  540. * ocfs2_dio_end_io is called by the dio core when a dio is finished. We're
  541. * particularly interested in the aio/dio case. Like the core uses
  542. * i_alloc_sem, we use the rw_lock DLM lock to protect io on one node from
  543. * truncation on another.
  544. */
  545. static void ocfs2_dio_end_io(struct kiocb *iocb,
  546. loff_t offset,
  547. ssize_t bytes,
  548. void *private)
  549. {
  550. struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
  551. int level;
  552. /* this io's submitter should not have unlocked this before we could */
  553. BUG_ON(!ocfs2_iocb_is_rw_locked(iocb));
  554. ocfs2_iocb_clear_rw_locked(iocb);
  555. level = ocfs2_iocb_rw_locked_level(iocb);
  556. if (!level)
  557. up_read(&inode->i_alloc_sem);
  558. ocfs2_rw_unlock(inode, level);
  559. }
  560. /*
  561. * ocfs2_invalidatepage() and ocfs2_releasepage() are shamelessly stolen
  562. * from ext3. PageChecked() bits have been removed as OCFS2 does not
  563. * do journalled data.
  564. */
  565. static void ocfs2_invalidatepage(struct page *page, unsigned long offset)
  566. {
  567. journal_t *journal = OCFS2_SB(page->mapping->host->i_sb)->journal->j_journal;
  568. jbd2_journal_invalidatepage(journal, page, offset);
  569. }
  570. static int ocfs2_releasepage(struct page *page, gfp_t wait)
  571. {
  572. journal_t *journal = OCFS2_SB(page->mapping->host->i_sb)->journal->j_journal;
  573. if (!page_has_buffers(page))
  574. return 0;
  575. return jbd2_journal_try_to_free_buffers(journal, page, wait);
  576. }
  577. static ssize_t ocfs2_direct_IO(int rw,
  578. struct kiocb *iocb,
  579. const struct iovec *iov,
  580. loff_t offset,
  581. unsigned long nr_segs)
  582. {
  583. struct file *file = iocb->ki_filp;
  584. struct inode *inode = file->f_path.dentry->d_inode->i_mapping->host;
  585. int ret;
  586. mlog_entry_void();
  587. /*
  588. * Fallback to buffered I/O if we see an inode without
  589. * extents.
  590. */
  591. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  592. return 0;
  593. ret = blockdev_direct_IO_no_locking(rw, iocb, inode,
  594. inode->i_sb->s_bdev, iov, offset,
  595. nr_segs,
  596. ocfs2_direct_IO_get_blocks,
  597. ocfs2_dio_end_io);
  598. mlog_exit(ret);
  599. return ret;
  600. }
  601. static void ocfs2_figure_cluster_boundaries(struct ocfs2_super *osb,
  602. u32 cpos,
  603. unsigned int *start,
  604. unsigned int *end)
  605. {
  606. unsigned int cluster_start = 0, cluster_end = PAGE_CACHE_SIZE;
  607. if (unlikely(PAGE_CACHE_SHIFT > osb->s_clustersize_bits)) {
  608. unsigned int cpp;
  609. cpp = 1 << (PAGE_CACHE_SHIFT - osb->s_clustersize_bits);
  610. cluster_start = cpos % cpp;
  611. cluster_start = cluster_start << osb->s_clustersize_bits;
  612. cluster_end = cluster_start + osb->s_clustersize;
  613. }
  614. BUG_ON(cluster_start > PAGE_SIZE);
  615. BUG_ON(cluster_end > PAGE_SIZE);
  616. if (start)
  617. *start = cluster_start;
  618. if (end)
  619. *end = cluster_end;
  620. }
  621. /*
  622. * 'from' and 'to' are the region in the page to avoid zeroing.
  623. *
  624. * If pagesize > clustersize, this function will avoid zeroing outside
  625. * of the cluster boundary.
  626. *
  627. * from == to == 0 is code for "zero the entire cluster region"
  628. */
  629. static void ocfs2_clear_page_regions(struct page *page,
  630. struct ocfs2_super *osb, u32 cpos,
  631. unsigned from, unsigned to)
  632. {
  633. void *kaddr;
  634. unsigned int cluster_start, cluster_end;
  635. ocfs2_figure_cluster_boundaries(osb, cpos, &cluster_start, &cluster_end);
  636. kaddr = kmap_atomic(page, KM_USER0);
  637. if (from || to) {
  638. if (from > cluster_start)
  639. memset(kaddr + cluster_start, 0, from - cluster_start);
  640. if (to < cluster_end)
  641. memset(kaddr + to, 0, cluster_end - to);
  642. } else {
  643. memset(kaddr + cluster_start, 0, cluster_end - cluster_start);
  644. }
  645. kunmap_atomic(kaddr, KM_USER0);
  646. }
  647. /*
  648. * Nonsparse file systems fully allocate before we get to the write
  649. * code. This prevents ocfs2_write() from tagging the write as an
  650. * allocating one, which means ocfs2_map_page_blocks() might try to
  651. * read-in the blocks at the tail of our file. Avoid reading them by
  652. * testing i_size against each block offset.
  653. */
  654. static int ocfs2_should_read_blk(struct inode *inode, struct page *page,
  655. unsigned int block_start)
  656. {
  657. u64 offset = page_offset(page) + block_start;
  658. if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
  659. return 1;
  660. if (i_size_read(inode) > offset)
  661. return 1;
  662. return 0;
  663. }
  664. /*
  665. * Some of this taken from block_prepare_write(). We already have our
  666. * mapping by now though, and the entire write will be allocating or
  667. * it won't, so not much need to use BH_New.
  668. *
  669. * This will also skip zeroing, which is handled externally.
  670. */
  671. int ocfs2_map_page_blocks(struct page *page, u64 *p_blkno,
  672. struct inode *inode, unsigned int from,
  673. unsigned int to, int new)
  674. {
  675. int ret = 0;
  676. struct buffer_head *head, *bh, *wait[2], **wait_bh = wait;
  677. unsigned int block_end, block_start;
  678. unsigned int bsize = 1 << inode->i_blkbits;
  679. if (!page_has_buffers(page))
  680. create_empty_buffers(page, bsize, 0);
  681. head = page_buffers(page);
  682. for (bh = head, block_start = 0; bh != head || !block_start;
  683. bh = bh->b_this_page, block_start += bsize) {
  684. block_end = block_start + bsize;
  685. clear_buffer_new(bh);
  686. /*
  687. * Ignore blocks outside of our i/o range -
  688. * they may belong to unallocated clusters.
  689. */
  690. if (block_start >= to || block_end <= from) {
  691. if (PageUptodate(page))
  692. set_buffer_uptodate(bh);
  693. continue;
  694. }
  695. /*
  696. * For an allocating write with cluster size >= page
  697. * size, we always write the entire page.
  698. */
  699. if (new)
  700. set_buffer_new(bh);
  701. if (!buffer_mapped(bh)) {
  702. map_bh(bh, inode->i_sb, *p_blkno);
  703. unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
  704. }
  705. if (PageUptodate(page)) {
  706. if (!buffer_uptodate(bh))
  707. set_buffer_uptodate(bh);
  708. } else if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
  709. !buffer_new(bh) &&
  710. ocfs2_should_read_blk(inode, page, block_start) &&
  711. (block_start < from || block_end > to)) {
  712. ll_rw_block(READ, 1, &bh);
  713. *wait_bh++=bh;
  714. }
  715. *p_blkno = *p_blkno + 1;
  716. }
  717. /*
  718. * If we issued read requests - let them complete.
  719. */
  720. while(wait_bh > wait) {
  721. wait_on_buffer(*--wait_bh);
  722. if (!buffer_uptodate(*wait_bh))
  723. ret = -EIO;
  724. }
  725. if (ret == 0 || !new)
  726. return ret;
  727. /*
  728. * If we get -EIO above, zero out any newly allocated blocks
  729. * to avoid exposing stale data.
  730. */
  731. bh = head;
  732. block_start = 0;
  733. do {
  734. block_end = block_start + bsize;
  735. if (block_end <= from)
  736. goto next_bh;
  737. if (block_start >= to)
  738. break;
  739. zero_user(page, block_start, bh->b_size);
  740. set_buffer_uptodate(bh);
  741. mark_buffer_dirty(bh);
  742. next_bh:
  743. block_start = block_end;
  744. bh = bh->b_this_page;
  745. } while (bh != head);
  746. return ret;
  747. }
  748. #if (PAGE_CACHE_SIZE >= OCFS2_MAX_CLUSTERSIZE)
  749. #define OCFS2_MAX_CTXT_PAGES 1
  750. #else
  751. #define OCFS2_MAX_CTXT_PAGES (OCFS2_MAX_CLUSTERSIZE / PAGE_CACHE_SIZE)
  752. #endif
  753. #define OCFS2_MAX_CLUSTERS_PER_PAGE (PAGE_CACHE_SIZE / OCFS2_MIN_CLUSTERSIZE)
  754. /*
  755. * Describe the state of a single cluster to be written to.
  756. */
  757. struct ocfs2_write_cluster_desc {
  758. u32 c_cpos;
  759. u32 c_phys;
  760. /*
  761. * Give this a unique field because c_phys eventually gets
  762. * filled.
  763. */
  764. unsigned c_new;
  765. unsigned c_unwritten;
  766. };
  767. static inline int ocfs2_should_zero_cluster(struct ocfs2_write_cluster_desc *d)
  768. {
  769. return d->c_new || d->c_unwritten;
  770. }
  771. struct ocfs2_write_ctxt {
  772. /* Logical cluster position / len of write */
  773. u32 w_cpos;
  774. u32 w_clen;
  775. struct ocfs2_write_cluster_desc w_desc[OCFS2_MAX_CLUSTERS_PER_PAGE];
  776. /*
  777. * This is true if page_size > cluster_size.
  778. *
  779. * It triggers a set of special cases during write which might
  780. * have to deal with allocating writes to partial pages.
  781. */
  782. unsigned int w_large_pages;
  783. /*
  784. * Pages involved in this write.
  785. *
  786. * w_target_page is the page being written to by the user.
  787. *
  788. * w_pages is an array of pages which always contains
  789. * w_target_page, and in the case of an allocating write with
  790. * page_size < cluster size, it will contain zero'd and mapped
  791. * pages adjacent to w_target_page which need to be written
  792. * out in so that future reads from that region will get
  793. * zero's.
  794. */
  795. struct page *w_pages[OCFS2_MAX_CTXT_PAGES];
  796. unsigned int w_num_pages;
  797. struct page *w_target_page;
  798. /*
  799. * ocfs2_write_end() uses this to know what the real range to
  800. * write in the target should be.
  801. */
  802. unsigned int w_target_from;
  803. unsigned int w_target_to;
  804. /*
  805. * We could use journal_current_handle() but this is cleaner,
  806. * IMHO -Mark
  807. */
  808. handle_t *w_handle;
  809. struct buffer_head *w_di_bh;
  810. struct ocfs2_cached_dealloc_ctxt w_dealloc;
  811. };
  812. void ocfs2_unlock_and_free_pages(struct page **pages, int num_pages)
  813. {
  814. int i;
  815. for(i = 0; i < num_pages; i++) {
  816. if (pages[i]) {
  817. unlock_page(pages[i]);
  818. mark_page_accessed(pages[i]);
  819. page_cache_release(pages[i]);
  820. }
  821. }
  822. }
  823. static void ocfs2_free_write_ctxt(struct ocfs2_write_ctxt *wc)
  824. {
  825. ocfs2_unlock_and_free_pages(wc->w_pages, wc->w_num_pages);
  826. brelse(wc->w_di_bh);
  827. kfree(wc);
  828. }
  829. static int ocfs2_alloc_write_ctxt(struct ocfs2_write_ctxt **wcp,
  830. struct ocfs2_super *osb, loff_t pos,
  831. unsigned len, struct buffer_head *di_bh)
  832. {
  833. u32 cend;
  834. struct ocfs2_write_ctxt *wc;
  835. wc = kzalloc(sizeof(struct ocfs2_write_ctxt), GFP_NOFS);
  836. if (!wc)
  837. return -ENOMEM;
  838. wc->w_cpos = pos >> osb->s_clustersize_bits;
  839. cend = (pos + len - 1) >> osb->s_clustersize_bits;
  840. wc->w_clen = cend - wc->w_cpos + 1;
  841. get_bh(di_bh);
  842. wc->w_di_bh = di_bh;
  843. if (unlikely(PAGE_CACHE_SHIFT > osb->s_clustersize_bits))
  844. wc->w_large_pages = 1;
  845. else
  846. wc->w_large_pages = 0;
  847. ocfs2_init_dealloc_ctxt(&wc->w_dealloc);
  848. *wcp = wc;
  849. return 0;
  850. }
  851. /*
  852. * If a page has any new buffers, zero them out here, and mark them uptodate
  853. * and dirty so they'll be written out (in order to prevent uninitialised
  854. * block data from leaking). And clear the new bit.
  855. */
  856. static void ocfs2_zero_new_buffers(struct page *page, unsigned from, unsigned to)
  857. {
  858. unsigned int block_start, block_end;
  859. struct buffer_head *head, *bh;
  860. BUG_ON(!PageLocked(page));
  861. if (!page_has_buffers(page))
  862. return;
  863. bh = head = page_buffers(page);
  864. block_start = 0;
  865. do {
  866. block_end = block_start + bh->b_size;
  867. if (buffer_new(bh)) {
  868. if (block_end > from && block_start < to) {
  869. if (!PageUptodate(page)) {
  870. unsigned start, end;
  871. start = max(from, block_start);
  872. end = min(to, block_end);
  873. zero_user_segment(page, start, end);
  874. set_buffer_uptodate(bh);
  875. }
  876. clear_buffer_new(bh);
  877. mark_buffer_dirty(bh);
  878. }
  879. }
  880. block_start = block_end;
  881. bh = bh->b_this_page;
  882. } while (bh != head);
  883. }
  884. /*
  885. * Only called when we have a failure during allocating write to write
  886. * zero's to the newly allocated region.
  887. */
  888. static void ocfs2_write_failure(struct inode *inode,
  889. struct ocfs2_write_ctxt *wc,
  890. loff_t user_pos, unsigned user_len)
  891. {
  892. int i;
  893. unsigned from = user_pos & (PAGE_CACHE_SIZE - 1),
  894. to = user_pos + user_len;
  895. struct page *tmppage;
  896. ocfs2_zero_new_buffers(wc->w_target_page, from, to);
  897. for(i = 0; i < wc->w_num_pages; i++) {
  898. tmppage = wc->w_pages[i];
  899. if (page_has_buffers(tmppage)) {
  900. if (ocfs2_should_order_data(inode)) {
  901. ocfs2_jbd2_file_inode(wc->w_handle, inode);
  902. #ifdef CONFIG_OCFS2_COMPAT_JBD
  903. walk_page_buffers(wc->w_handle,
  904. page_buffers(tmppage),
  905. from, to, NULL,
  906. ocfs2_journal_dirty_data);
  907. #endif
  908. }
  909. block_commit_write(tmppage, from, to);
  910. }
  911. }
  912. }
  913. static int ocfs2_prepare_page_for_write(struct inode *inode, u64 *p_blkno,
  914. struct ocfs2_write_ctxt *wc,
  915. struct page *page, u32 cpos,
  916. loff_t user_pos, unsigned user_len,
  917. int new)
  918. {
  919. int ret;
  920. unsigned int map_from = 0, map_to = 0;
  921. unsigned int cluster_start, cluster_end;
  922. unsigned int user_data_from = 0, user_data_to = 0;
  923. ocfs2_figure_cluster_boundaries(OCFS2_SB(inode->i_sb), cpos,
  924. &cluster_start, &cluster_end);
  925. if (page == wc->w_target_page) {
  926. map_from = user_pos & (PAGE_CACHE_SIZE - 1);
  927. map_to = map_from + user_len;
  928. if (new)
  929. ret = ocfs2_map_page_blocks(page, p_blkno, inode,
  930. cluster_start, cluster_end,
  931. new);
  932. else
  933. ret = ocfs2_map_page_blocks(page, p_blkno, inode,
  934. map_from, map_to, new);
  935. if (ret) {
  936. mlog_errno(ret);
  937. goto out;
  938. }
  939. user_data_from = map_from;
  940. user_data_to = map_to;
  941. if (new) {
  942. map_from = cluster_start;
  943. map_to = cluster_end;
  944. }
  945. } else {
  946. /*
  947. * If we haven't allocated the new page yet, we
  948. * shouldn't be writing it out without copying user
  949. * data. This is likely a math error from the caller.
  950. */
  951. BUG_ON(!new);
  952. map_from = cluster_start;
  953. map_to = cluster_end;
  954. ret = ocfs2_map_page_blocks(page, p_blkno, inode,
  955. cluster_start, cluster_end, new);
  956. if (ret) {
  957. mlog_errno(ret);
  958. goto out;
  959. }
  960. }
  961. /*
  962. * Parts of newly allocated pages need to be zero'd.
  963. *
  964. * Above, we have also rewritten 'to' and 'from' - as far as
  965. * the rest of the function is concerned, the entire cluster
  966. * range inside of a page needs to be written.
  967. *
  968. * We can skip this if the page is up to date - it's already
  969. * been zero'd from being read in as a hole.
  970. */
  971. if (new && !PageUptodate(page))
  972. ocfs2_clear_page_regions(page, OCFS2_SB(inode->i_sb),
  973. cpos, user_data_from, user_data_to);
  974. flush_dcache_page(page);
  975. out:
  976. return ret;
  977. }
  978. /*
  979. * This function will only grab one clusters worth of pages.
  980. */
  981. static int ocfs2_grab_pages_for_write(struct address_space *mapping,
  982. struct ocfs2_write_ctxt *wc,
  983. u32 cpos, loff_t user_pos, int new,
  984. struct page *mmap_page)
  985. {
  986. int ret = 0, i;
  987. unsigned long start, target_index, index;
  988. struct inode *inode = mapping->host;
  989. target_index = user_pos >> PAGE_CACHE_SHIFT;
  990. /*
  991. * Figure out how many pages we'll be manipulating here. For
  992. * non allocating write, we just change the one
  993. * page. Otherwise, we'll need a whole clusters worth.
  994. */
  995. if (new) {
  996. wc->w_num_pages = ocfs2_pages_per_cluster(inode->i_sb);
  997. start = ocfs2_align_clusters_to_page_index(inode->i_sb, cpos);
  998. } else {
  999. wc->w_num_pages = 1;
  1000. start = target_index;
  1001. }
  1002. for(i = 0; i < wc->w_num_pages; i++) {
  1003. index = start + i;
  1004. if (index == target_index && mmap_page) {
  1005. /*
  1006. * ocfs2_pagemkwrite() is a little different
  1007. * and wants us to directly use the page
  1008. * passed in.
  1009. */
  1010. lock_page(mmap_page);
  1011. if (mmap_page->mapping != mapping) {
  1012. unlock_page(mmap_page);
  1013. /*
  1014. * Sanity check - the locking in
  1015. * ocfs2_pagemkwrite() should ensure
  1016. * that this code doesn't trigger.
  1017. */
  1018. ret = -EINVAL;
  1019. mlog_errno(ret);
  1020. goto out;
  1021. }
  1022. page_cache_get(mmap_page);
  1023. wc->w_pages[i] = mmap_page;
  1024. } else {
  1025. wc->w_pages[i] = find_or_create_page(mapping, index,
  1026. GFP_NOFS);
  1027. if (!wc->w_pages[i]) {
  1028. ret = -ENOMEM;
  1029. mlog_errno(ret);
  1030. goto out;
  1031. }
  1032. }
  1033. if (index == target_index)
  1034. wc->w_target_page = wc->w_pages[i];
  1035. }
  1036. out:
  1037. return ret;
  1038. }
  1039. /*
  1040. * Prepare a single cluster for write one cluster into the file.
  1041. */
  1042. static int ocfs2_write_cluster(struct address_space *mapping,
  1043. u32 phys, unsigned int unwritten,
  1044. struct ocfs2_alloc_context *data_ac,
  1045. struct ocfs2_alloc_context *meta_ac,
  1046. struct ocfs2_write_ctxt *wc, u32 cpos,
  1047. loff_t user_pos, unsigned user_len)
  1048. {
  1049. int ret, i, new, should_zero = 0;
  1050. u64 v_blkno, p_blkno;
  1051. struct inode *inode = mapping->host;
  1052. struct ocfs2_extent_tree et;
  1053. new = phys == 0 ? 1 : 0;
  1054. if (new || unwritten)
  1055. should_zero = 1;
  1056. if (new) {
  1057. u32 tmp_pos;
  1058. /*
  1059. * This is safe to call with the page locks - it won't take
  1060. * any additional semaphores or cluster locks.
  1061. */
  1062. tmp_pos = cpos;
  1063. ret = ocfs2_add_inode_data(OCFS2_SB(inode->i_sb), inode,
  1064. &tmp_pos, 1, 0, wc->w_di_bh,
  1065. wc->w_handle, data_ac,
  1066. meta_ac, NULL);
  1067. /*
  1068. * This shouldn't happen because we must have already
  1069. * calculated the correct meta data allocation required. The
  1070. * internal tree allocation code should know how to increase
  1071. * transaction credits itself.
  1072. *
  1073. * If need be, we could handle -EAGAIN for a
  1074. * RESTART_TRANS here.
  1075. */
  1076. mlog_bug_on_msg(ret == -EAGAIN,
  1077. "Inode %llu: EAGAIN return during allocation.\n",
  1078. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  1079. if (ret < 0) {
  1080. mlog_errno(ret);
  1081. goto out;
  1082. }
  1083. } else if (unwritten) {
  1084. ocfs2_init_dinode_extent_tree(&et, inode, wc->w_di_bh);
  1085. ret = ocfs2_mark_extent_written(inode, &et,
  1086. wc->w_handle, cpos, 1, phys,
  1087. meta_ac, &wc->w_dealloc);
  1088. if (ret < 0) {
  1089. mlog_errno(ret);
  1090. goto out;
  1091. }
  1092. }
  1093. if (should_zero)
  1094. v_blkno = ocfs2_clusters_to_blocks(inode->i_sb, cpos);
  1095. else
  1096. v_blkno = user_pos >> inode->i_sb->s_blocksize_bits;
  1097. /*
  1098. * The only reason this should fail is due to an inability to
  1099. * find the extent added.
  1100. */
  1101. ret = ocfs2_extent_map_get_blocks(inode, v_blkno, &p_blkno, NULL,
  1102. NULL);
  1103. if (ret < 0) {
  1104. ocfs2_error(inode->i_sb, "Corrupting extend for inode %llu, "
  1105. "at logical block %llu",
  1106. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1107. (unsigned long long)v_blkno);
  1108. goto out;
  1109. }
  1110. BUG_ON(p_blkno == 0);
  1111. for(i = 0; i < wc->w_num_pages; i++) {
  1112. int tmpret;
  1113. tmpret = ocfs2_prepare_page_for_write(inode, &p_blkno, wc,
  1114. wc->w_pages[i], cpos,
  1115. user_pos, user_len,
  1116. should_zero);
  1117. if (tmpret) {
  1118. mlog_errno(tmpret);
  1119. if (ret == 0)
  1120. tmpret = ret;
  1121. }
  1122. }
  1123. /*
  1124. * We only have cleanup to do in case of allocating write.
  1125. */
  1126. if (ret && new)
  1127. ocfs2_write_failure(inode, wc, user_pos, user_len);
  1128. out:
  1129. return ret;
  1130. }
  1131. static int ocfs2_write_cluster_by_desc(struct address_space *mapping,
  1132. struct ocfs2_alloc_context *data_ac,
  1133. struct ocfs2_alloc_context *meta_ac,
  1134. struct ocfs2_write_ctxt *wc,
  1135. loff_t pos, unsigned len)
  1136. {
  1137. int ret, i;
  1138. loff_t cluster_off;
  1139. unsigned int local_len = len;
  1140. struct ocfs2_write_cluster_desc *desc;
  1141. struct ocfs2_super *osb = OCFS2_SB(mapping->host->i_sb);
  1142. for (i = 0; i < wc->w_clen; i++) {
  1143. desc = &wc->w_desc[i];
  1144. /*
  1145. * We have to make sure that the total write passed in
  1146. * doesn't extend past a single cluster.
  1147. */
  1148. local_len = len;
  1149. cluster_off = pos & (osb->s_clustersize - 1);
  1150. if ((cluster_off + local_len) > osb->s_clustersize)
  1151. local_len = osb->s_clustersize - cluster_off;
  1152. ret = ocfs2_write_cluster(mapping, desc->c_phys,
  1153. desc->c_unwritten, data_ac, meta_ac,
  1154. wc, desc->c_cpos, pos, local_len);
  1155. if (ret) {
  1156. mlog_errno(ret);
  1157. goto out;
  1158. }
  1159. len -= local_len;
  1160. pos += local_len;
  1161. }
  1162. ret = 0;
  1163. out:
  1164. return ret;
  1165. }
  1166. /*
  1167. * ocfs2_write_end() wants to know which parts of the target page it
  1168. * should complete the write on. It's easiest to compute them ahead of
  1169. * time when a more complete view of the write is available.
  1170. */
  1171. static void ocfs2_set_target_boundaries(struct ocfs2_super *osb,
  1172. struct ocfs2_write_ctxt *wc,
  1173. loff_t pos, unsigned len, int alloc)
  1174. {
  1175. struct ocfs2_write_cluster_desc *desc;
  1176. wc->w_target_from = pos & (PAGE_CACHE_SIZE - 1);
  1177. wc->w_target_to = wc->w_target_from + len;
  1178. if (alloc == 0)
  1179. return;
  1180. /*
  1181. * Allocating write - we may have different boundaries based
  1182. * on page size and cluster size.
  1183. *
  1184. * NOTE: We can no longer compute one value from the other as
  1185. * the actual write length and user provided length may be
  1186. * different.
  1187. */
  1188. if (wc->w_large_pages) {
  1189. /*
  1190. * We only care about the 1st and last cluster within
  1191. * our range and whether they should be zero'd or not. Either
  1192. * value may be extended out to the start/end of a
  1193. * newly allocated cluster.
  1194. */
  1195. desc = &wc->w_desc[0];
  1196. if (ocfs2_should_zero_cluster(desc))
  1197. ocfs2_figure_cluster_boundaries(osb,
  1198. desc->c_cpos,
  1199. &wc->w_target_from,
  1200. NULL);
  1201. desc = &wc->w_desc[wc->w_clen - 1];
  1202. if (ocfs2_should_zero_cluster(desc))
  1203. ocfs2_figure_cluster_boundaries(osb,
  1204. desc->c_cpos,
  1205. NULL,
  1206. &wc->w_target_to);
  1207. } else {
  1208. wc->w_target_from = 0;
  1209. wc->w_target_to = PAGE_CACHE_SIZE;
  1210. }
  1211. }
  1212. /*
  1213. * Populate each single-cluster write descriptor in the write context
  1214. * with information about the i/o to be done.
  1215. *
  1216. * Returns the number of clusters that will have to be allocated, as
  1217. * well as a worst case estimate of the number of extent records that
  1218. * would have to be created during a write to an unwritten region.
  1219. */
  1220. static int ocfs2_populate_write_desc(struct inode *inode,
  1221. struct ocfs2_write_ctxt *wc,
  1222. unsigned int *clusters_to_alloc,
  1223. unsigned int *extents_to_split)
  1224. {
  1225. int ret;
  1226. struct ocfs2_write_cluster_desc *desc;
  1227. unsigned int num_clusters = 0;
  1228. unsigned int ext_flags = 0;
  1229. u32 phys = 0;
  1230. int i;
  1231. *clusters_to_alloc = 0;
  1232. *extents_to_split = 0;
  1233. for (i = 0; i < wc->w_clen; i++) {
  1234. desc = &wc->w_desc[i];
  1235. desc->c_cpos = wc->w_cpos + i;
  1236. if (num_clusters == 0) {
  1237. /*
  1238. * Need to look up the next extent record.
  1239. */
  1240. ret = ocfs2_get_clusters(inode, desc->c_cpos, &phys,
  1241. &num_clusters, &ext_flags);
  1242. if (ret) {
  1243. mlog_errno(ret);
  1244. goto out;
  1245. }
  1246. /*
  1247. * Assume worst case - that we're writing in
  1248. * the middle of the extent.
  1249. *
  1250. * We can assume that the write proceeds from
  1251. * left to right, in which case the extent
  1252. * insert code is smart enough to coalesce the
  1253. * next splits into the previous records created.
  1254. */
  1255. if (ext_flags & OCFS2_EXT_UNWRITTEN)
  1256. *extents_to_split = *extents_to_split + 2;
  1257. } else if (phys) {
  1258. /*
  1259. * Only increment phys if it doesn't describe
  1260. * a hole.
  1261. */
  1262. phys++;
  1263. }
  1264. desc->c_phys = phys;
  1265. if (phys == 0) {
  1266. desc->c_new = 1;
  1267. *clusters_to_alloc = *clusters_to_alloc + 1;
  1268. }
  1269. if (ext_flags & OCFS2_EXT_UNWRITTEN)
  1270. desc->c_unwritten = 1;
  1271. num_clusters--;
  1272. }
  1273. ret = 0;
  1274. out:
  1275. return ret;
  1276. }
  1277. static int ocfs2_write_begin_inline(struct address_space *mapping,
  1278. struct inode *inode,
  1279. struct ocfs2_write_ctxt *wc)
  1280. {
  1281. int ret;
  1282. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1283. struct page *page;
  1284. handle_t *handle;
  1285. struct ocfs2_dinode *di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
  1286. page = find_or_create_page(mapping, 0, GFP_NOFS);
  1287. if (!page) {
  1288. ret = -ENOMEM;
  1289. mlog_errno(ret);
  1290. goto out;
  1291. }
  1292. /*
  1293. * If we don't set w_num_pages then this page won't get unlocked
  1294. * and freed on cleanup of the write context.
  1295. */
  1296. wc->w_pages[0] = wc->w_target_page = page;
  1297. wc->w_num_pages = 1;
  1298. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1299. if (IS_ERR(handle)) {
  1300. ret = PTR_ERR(handle);
  1301. mlog_errno(ret);
  1302. goto out;
  1303. }
  1304. ret = ocfs2_journal_access(handle, inode, wc->w_di_bh,
  1305. OCFS2_JOURNAL_ACCESS_WRITE);
  1306. if (ret) {
  1307. ocfs2_commit_trans(osb, handle);
  1308. mlog_errno(ret);
  1309. goto out;
  1310. }
  1311. if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL))
  1312. ocfs2_set_inode_data_inline(inode, di);
  1313. if (!PageUptodate(page)) {
  1314. ret = ocfs2_read_inline_data(inode, page, wc->w_di_bh);
  1315. if (ret) {
  1316. ocfs2_commit_trans(osb, handle);
  1317. goto out;
  1318. }
  1319. }
  1320. wc->w_handle = handle;
  1321. out:
  1322. return ret;
  1323. }
  1324. int ocfs2_size_fits_inline_data(struct buffer_head *di_bh, u64 new_size)
  1325. {
  1326. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1327. if (new_size <= le16_to_cpu(di->id2.i_data.id_count))
  1328. return 1;
  1329. return 0;
  1330. }
  1331. static int ocfs2_try_to_write_inline_data(struct address_space *mapping,
  1332. struct inode *inode, loff_t pos,
  1333. unsigned len, struct page *mmap_page,
  1334. struct ocfs2_write_ctxt *wc)
  1335. {
  1336. int ret, written = 0;
  1337. loff_t end = pos + len;
  1338. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1339. mlog(0, "Inode %llu, write of %u bytes at off %llu. features: 0x%x\n",
  1340. (unsigned long long)oi->ip_blkno, len, (unsigned long long)pos,
  1341. oi->ip_dyn_features);
  1342. /*
  1343. * Handle inodes which already have inline data 1st.
  1344. */
  1345. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1346. if (mmap_page == NULL &&
  1347. ocfs2_size_fits_inline_data(wc->w_di_bh, end))
  1348. goto do_inline_write;
  1349. /*
  1350. * The write won't fit - we have to give this inode an
  1351. * inline extent list now.
  1352. */
  1353. ret = ocfs2_convert_inline_data_to_extents(inode, wc->w_di_bh);
  1354. if (ret)
  1355. mlog_errno(ret);
  1356. goto out;
  1357. }
  1358. /*
  1359. * Check whether the inode can accept inline data.
  1360. */
  1361. if (oi->ip_clusters != 0 || i_size_read(inode) != 0)
  1362. return 0;
  1363. /*
  1364. * Check whether the write can fit.
  1365. */
  1366. if (mmap_page || end > ocfs2_max_inline_data(inode->i_sb))
  1367. return 0;
  1368. do_inline_write:
  1369. ret = ocfs2_write_begin_inline(mapping, inode, wc);
  1370. if (ret) {
  1371. mlog_errno(ret);
  1372. goto out;
  1373. }
  1374. /*
  1375. * This signals to the caller that the data can be written
  1376. * inline.
  1377. */
  1378. written = 1;
  1379. out:
  1380. return written ? written : ret;
  1381. }
  1382. /*
  1383. * This function only does anything for file systems which can't
  1384. * handle sparse files.
  1385. *
  1386. * What we want to do here is fill in any hole between the current end
  1387. * of allocation and the end of our write. That way the rest of the
  1388. * write path can treat it as an non-allocating write, which has no
  1389. * special case code for sparse/nonsparse files.
  1390. */
  1391. static int ocfs2_expand_nonsparse_inode(struct inode *inode, loff_t pos,
  1392. unsigned len,
  1393. struct ocfs2_write_ctxt *wc)
  1394. {
  1395. int ret;
  1396. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1397. loff_t newsize = pos + len;
  1398. if (ocfs2_sparse_alloc(osb))
  1399. return 0;
  1400. if (newsize <= i_size_read(inode))
  1401. return 0;
  1402. ret = ocfs2_extend_no_holes(inode, newsize, newsize - len);
  1403. if (ret)
  1404. mlog_errno(ret);
  1405. return ret;
  1406. }
  1407. int ocfs2_write_begin_nolock(struct address_space *mapping,
  1408. loff_t pos, unsigned len, unsigned flags,
  1409. struct page **pagep, void **fsdata,
  1410. struct buffer_head *di_bh, struct page *mmap_page)
  1411. {
  1412. int ret, credits = OCFS2_INODE_UPDATE_CREDITS;
  1413. unsigned int clusters_to_alloc, extents_to_split;
  1414. struct ocfs2_write_ctxt *wc;
  1415. struct inode *inode = mapping->host;
  1416. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1417. struct ocfs2_dinode *di;
  1418. struct ocfs2_alloc_context *data_ac = NULL;
  1419. struct ocfs2_alloc_context *meta_ac = NULL;
  1420. handle_t *handle;
  1421. struct ocfs2_extent_tree et;
  1422. ret = ocfs2_alloc_write_ctxt(&wc, osb, pos, len, di_bh);
  1423. if (ret) {
  1424. mlog_errno(ret);
  1425. return ret;
  1426. }
  1427. if (ocfs2_supports_inline_data(osb)) {
  1428. ret = ocfs2_try_to_write_inline_data(mapping, inode, pos, len,
  1429. mmap_page, wc);
  1430. if (ret == 1) {
  1431. ret = 0;
  1432. goto success;
  1433. }
  1434. if (ret < 0) {
  1435. mlog_errno(ret);
  1436. goto out;
  1437. }
  1438. }
  1439. ret = ocfs2_expand_nonsparse_inode(inode, pos, len, wc);
  1440. if (ret) {
  1441. mlog_errno(ret);
  1442. goto out;
  1443. }
  1444. ret = ocfs2_populate_write_desc(inode, wc, &clusters_to_alloc,
  1445. &extents_to_split);
  1446. if (ret) {
  1447. mlog_errno(ret);
  1448. goto out;
  1449. }
  1450. di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
  1451. /*
  1452. * We set w_target_from, w_target_to here so that
  1453. * ocfs2_write_end() knows which range in the target page to
  1454. * write out. An allocation requires that we write the entire
  1455. * cluster range.
  1456. */
  1457. if (clusters_to_alloc || extents_to_split) {
  1458. /*
  1459. * XXX: We are stretching the limits of
  1460. * ocfs2_lock_allocators(). It greatly over-estimates
  1461. * the work to be done.
  1462. */
  1463. mlog(0, "extend inode %llu, i_size = %lld, di->i_clusters = %u,"
  1464. " clusters_to_add = %u, extents_to_split = %u\n",
  1465. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1466. (long long)i_size_read(inode), le32_to_cpu(di->i_clusters),
  1467. clusters_to_alloc, extents_to_split);
  1468. ocfs2_init_dinode_extent_tree(&et, inode, wc->w_di_bh);
  1469. ret = ocfs2_lock_allocators(inode, &et,
  1470. clusters_to_alloc, extents_to_split,
  1471. &data_ac, &meta_ac);
  1472. if (ret) {
  1473. mlog_errno(ret);
  1474. goto out;
  1475. }
  1476. credits = ocfs2_calc_extend_credits(inode->i_sb,
  1477. &di->id2.i_list,
  1478. clusters_to_alloc);
  1479. }
  1480. ocfs2_set_target_boundaries(osb, wc, pos, len,
  1481. clusters_to_alloc + extents_to_split);
  1482. handle = ocfs2_start_trans(osb, credits);
  1483. if (IS_ERR(handle)) {
  1484. ret = PTR_ERR(handle);
  1485. mlog_errno(ret);
  1486. goto out;
  1487. }
  1488. wc->w_handle = handle;
  1489. /*
  1490. * We don't want this to fail in ocfs2_write_end(), so do it
  1491. * here.
  1492. */
  1493. ret = ocfs2_journal_access(handle, inode, wc->w_di_bh,
  1494. OCFS2_JOURNAL_ACCESS_WRITE);
  1495. if (ret) {
  1496. mlog_errno(ret);
  1497. goto out_commit;
  1498. }
  1499. /*
  1500. * Fill our page array first. That way we've grabbed enough so
  1501. * that we can zero and flush if we error after adding the
  1502. * extent.
  1503. */
  1504. ret = ocfs2_grab_pages_for_write(mapping, wc, wc->w_cpos, pos,
  1505. clusters_to_alloc + extents_to_split,
  1506. mmap_page);
  1507. if (ret) {
  1508. mlog_errno(ret);
  1509. goto out_commit;
  1510. }
  1511. ret = ocfs2_write_cluster_by_desc(mapping, data_ac, meta_ac, wc, pos,
  1512. len);
  1513. if (ret) {
  1514. mlog_errno(ret);
  1515. goto out_commit;
  1516. }
  1517. if (data_ac)
  1518. ocfs2_free_alloc_context(data_ac);
  1519. if (meta_ac)
  1520. ocfs2_free_alloc_context(meta_ac);
  1521. success:
  1522. *pagep = wc->w_target_page;
  1523. *fsdata = wc;
  1524. return 0;
  1525. out_commit:
  1526. ocfs2_commit_trans(osb, handle);
  1527. out:
  1528. ocfs2_free_write_ctxt(wc);
  1529. if (data_ac)
  1530. ocfs2_free_alloc_context(data_ac);
  1531. if (meta_ac)
  1532. ocfs2_free_alloc_context(meta_ac);
  1533. return ret;
  1534. }
  1535. static int ocfs2_write_begin(struct file *file, struct address_space *mapping,
  1536. loff_t pos, unsigned len, unsigned flags,
  1537. struct page **pagep, void **fsdata)
  1538. {
  1539. int ret;
  1540. struct buffer_head *di_bh = NULL;
  1541. struct inode *inode = mapping->host;
  1542. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  1543. if (ret) {
  1544. mlog_errno(ret);
  1545. return ret;
  1546. }
  1547. /*
  1548. * Take alloc sem here to prevent concurrent lookups. That way
  1549. * the mapping, zeroing and tree manipulation within
  1550. * ocfs2_write() will be safe against ->readpage(). This
  1551. * should also serve to lock out allocation from a shared
  1552. * writeable region.
  1553. */
  1554. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  1555. ret = ocfs2_write_begin_nolock(mapping, pos, len, flags, pagep,
  1556. fsdata, di_bh, NULL);
  1557. if (ret) {
  1558. mlog_errno(ret);
  1559. goto out_fail;
  1560. }
  1561. brelse(di_bh);
  1562. return 0;
  1563. out_fail:
  1564. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  1565. brelse(di_bh);
  1566. ocfs2_inode_unlock(inode, 1);
  1567. return ret;
  1568. }
  1569. static void ocfs2_write_end_inline(struct inode *inode, loff_t pos,
  1570. unsigned len, unsigned *copied,
  1571. struct ocfs2_dinode *di,
  1572. struct ocfs2_write_ctxt *wc)
  1573. {
  1574. void *kaddr;
  1575. if (unlikely(*copied < len)) {
  1576. if (!PageUptodate(wc->w_target_page)) {
  1577. *copied = 0;
  1578. return;
  1579. }
  1580. }
  1581. kaddr = kmap_atomic(wc->w_target_page, KM_USER0);
  1582. memcpy(di->id2.i_data.id_data + pos, kaddr + pos, *copied);
  1583. kunmap_atomic(kaddr, KM_USER0);
  1584. mlog(0, "Data written to inode at offset %llu. "
  1585. "id_count = %u, copied = %u, i_dyn_features = 0x%x\n",
  1586. (unsigned long long)pos, *copied,
  1587. le16_to_cpu(di->id2.i_data.id_count),
  1588. le16_to_cpu(di->i_dyn_features));
  1589. }
  1590. int ocfs2_write_end_nolock(struct address_space *mapping,
  1591. loff_t pos, unsigned len, unsigned copied,
  1592. struct page *page, void *fsdata)
  1593. {
  1594. int i;
  1595. unsigned from, to, start = pos & (PAGE_CACHE_SIZE - 1);
  1596. struct inode *inode = mapping->host;
  1597. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1598. struct ocfs2_write_ctxt *wc = fsdata;
  1599. struct ocfs2_dinode *di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
  1600. handle_t *handle = wc->w_handle;
  1601. struct page *tmppage;
  1602. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1603. ocfs2_write_end_inline(inode, pos, len, &copied, di, wc);
  1604. goto out_write_size;
  1605. }
  1606. if (unlikely(copied < len)) {
  1607. if (!PageUptodate(wc->w_target_page))
  1608. copied = 0;
  1609. ocfs2_zero_new_buffers(wc->w_target_page, start+copied,
  1610. start+len);
  1611. }
  1612. flush_dcache_page(wc->w_target_page);
  1613. for(i = 0; i < wc->w_num_pages; i++) {
  1614. tmppage = wc->w_pages[i];
  1615. if (tmppage == wc->w_target_page) {
  1616. from = wc->w_target_from;
  1617. to = wc->w_target_to;
  1618. BUG_ON(from > PAGE_CACHE_SIZE ||
  1619. to > PAGE_CACHE_SIZE ||
  1620. to < from);
  1621. } else {
  1622. /*
  1623. * Pages adjacent to the target (if any) imply
  1624. * a hole-filling write in which case we want
  1625. * to flush their entire range.
  1626. */
  1627. from = 0;
  1628. to = PAGE_CACHE_SIZE;
  1629. }
  1630. if (page_has_buffers(tmppage)) {
  1631. if (ocfs2_should_order_data(inode)) {
  1632. ocfs2_jbd2_file_inode(wc->w_handle, inode);
  1633. #ifdef CONFIG_OCFS2_COMPAT_JBD
  1634. walk_page_buffers(wc->w_handle,
  1635. page_buffers(tmppage),
  1636. from, to, NULL,
  1637. ocfs2_journal_dirty_data);
  1638. #endif
  1639. }
  1640. block_commit_write(tmppage, from, to);
  1641. }
  1642. }
  1643. out_write_size:
  1644. pos += copied;
  1645. if (pos > inode->i_size) {
  1646. i_size_write(inode, pos);
  1647. mark_inode_dirty(inode);
  1648. }
  1649. inode->i_blocks = ocfs2_inode_sector_count(inode);
  1650. di->i_size = cpu_to_le64((u64)i_size_read(inode));
  1651. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1652. di->i_mtime = di->i_ctime = cpu_to_le64(inode->i_mtime.tv_sec);
  1653. di->i_mtime_nsec = di->i_ctime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
  1654. ocfs2_journal_dirty(handle, wc->w_di_bh);
  1655. ocfs2_commit_trans(osb, handle);
  1656. ocfs2_run_deallocs(osb, &wc->w_dealloc);
  1657. ocfs2_free_write_ctxt(wc);
  1658. return copied;
  1659. }
  1660. static int ocfs2_write_end(struct file *file, struct address_space *mapping,
  1661. loff_t pos, unsigned len, unsigned copied,
  1662. struct page *page, void *fsdata)
  1663. {
  1664. int ret;
  1665. struct inode *inode = mapping->host;
  1666. ret = ocfs2_write_end_nolock(mapping, pos, len, copied, page, fsdata);
  1667. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  1668. ocfs2_inode_unlock(inode, 1);
  1669. return ret;
  1670. }
  1671. const struct address_space_operations ocfs2_aops = {
  1672. .readpage = ocfs2_readpage,
  1673. .readpages = ocfs2_readpages,
  1674. .writepage = ocfs2_writepage,
  1675. .write_begin = ocfs2_write_begin,
  1676. .write_end = ocfs2_write_end,
  1677. .bmap = ocfs2_bmap,
  1678. .sync_page = block_sync_page,
  1679. .direct_IO = ocfs2_direct_IO,
  1680. .invalidatepage = ocfs2_invalidatepage,
  1681. .releasepage = ocfs2_releasepage,
  1682. .migratepage = buffer_migrate_page,
  1683. };