aops.c 51 KB

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