aops.c 52 KB

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