aops.c 62 KB

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