aops.c 52 KB

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