aops.c 53 KB

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