aops.c 49 KB

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