aops.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101
  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 int offset,
  521. unsigned int length)
  522. {
  523. journal_t *journal = OCFS2_SB(page->mapping->host->i_sb)->journal->j_journal;
  524. jbd2_journal_invalidatepage(journal, page, offset, length);
  525. }
  526. static int ocfs2_releasepage(struct page *page, gfp_t wait)
  527. {
  528. journal_t *journal = OCFS2_SB(page->mapping->host->i_sb)->journal->j_journal;
  529. if (!page_has_buffers(page))
  530. return 0;
  531. return jbd2_journal_try_to_free_buffers(journal, page, wait);
  532. }
  533. static ssize_t ocfs2_direct_IO(int rw,
  534. struct kiocb *iocb,
  535. const struct iovec *iov,
  536. loff_t offset,
  537. unsigned long nr_segs)
  538. {
  539. struct file *file = iocb->ki_filp;
  540. struct inode *inode = file_inode(file)->i_mapping->host;
  541. /*
  542. * Fallback to buffered I/O if we see an inode without
  543. * extents.
  544. */
  545. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  546. return 0;
  547. /* Fallback to buffered I/O if we are appending. */
  548. if (i_size_read(inode) <= offset)
  549. return 0;
  550. return __blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev,
  551. iov, offset, nr_segs,
  552. ocfs2_direct_IO_get_blocks,
  553. ocfs2_dio_end_io, NULL, 0);
  554. }
  555. static void ocfs2_figure_cluster_boundaries(struct ocfs2_super *osb,
  556. u32 cpos,
  557. unsigned int *start,
  558. unsigned int *end)
  559. {
  560. unsigned int cluster_start = 0, cluster_end = PAGE_CACHE_SIZE;
  561. if (unlikely(PAGE_CACHE_SHIFT > osb->s_clustersize_bits)) {
  562. unsigned int cpp;
  563. cpp = 1 << (PAGE_CACHE_SHIFT - osb->s_clustersize_bits);
  564. cluster_start = cpos % cpp;
  565. cluster_start = cluster_start << osb->s_clustersize_bits;
  566. cluster_end = cluster_start + osb->s_clustersize;
  567. }
  568. BUG_ON(cluster_start > PAGE_SIZE);
  569. BUG_ON(cluster_end > PAGE_SIZE);
  570. if (start)
  571. *start = cluster_start;
  572. if (end)
  573. *end = cluster_end;
  574. }
  575. /*
  576. * 'from' and 'to' are the region in the page to avoid zeroing.
  577. *
  578. * If pagesize > clustersize, this function will avoid zeroing outside
  579. * of the cluster boundary.
  580. *
  581. * from == to == 0 is code for "zero the entire cluster region"
  582. */
  583. static void ocfs2_clear_page_regions(struct page *page,
  584. struct ocfs2_super *osb, u32 cpos,
  585. unsigned from, unsigned to)
  586. {
  587. void *kaddr;
  588. unsigned int cluster_start, cluster_end;
  589. ocfs2_figure_cluster_boundaries(osb, cpos, &cluster_start, &cluster_end);
  590. kaddr = kmap_atomic(page);
  591. if (from || to) {
  592. if (from > cluster_start)
  593. memset(kaddr + cluster_start, 0, from - cluster_start);
  594. if (to < cluster_end)
  595. memset(kaddr + to, 0, cluster_end - to);
  596. } else {
  597. memset(kaddr + cluster_start, 0, cluster_end - cluster_start);
  598. }
  599. kunmap_atomic(kaddr);
  600. }
  601. /*
  602. * Nonsparse file systems fully allocate before we get to the write
  603. * code. This prevents ocfs2_write() from tagging the write as an
  604. * allocating one, which means ocfs2_map_page_blocks() might try to
  605. * read-in the blocks at the tail of our file. Avoid reading them by
  606. * testing i_size against each block offset.
  607. */
  608. static int ocfs2_should_read_blk(struct inode *inode, struct page *page,
  609. unsigned int block_start)
  610. {
  611. u64 offset = page_offset(page) + block_start;
  612. if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
  613. return 1;
  614. if (i_size_read(inode) > offset)
  615. return 1;
  616. return 0;
  617. }
  618. /*
  619. * Some of this taken from __block_write_begin(). We already have our
  620. * mapping by now though, and the entire write will be allocating or
  621. * it won't, so not much need to use BH_New.
  622. *
  623. * This will also skip zeroing, which is handled externally.
  624. */
  625. int ocfs2_map_page_blocks(struct page *page, u64 *p_blkno,
  626. struct inode *inode, unsigned int from,
  627. unsigned int to, int new)
  628. {
  629. int ret = 0;
  630. struct buffer_head *head, *bh, *wait[2], **wait_bh = wait;
  631. unsigned int block_end, block_start;
  632. unsigned int bsize = 1 << inode->i_blkbits;
  633. if (!page_has_buffers(page))
  634. create_empty_buffers(page, bsize, 0);
  635. head = page_buffers(page);
  636. for (bh = head, block_start = 0; bh != head || !block_start;
  637. bh = bh->b_this_page, block_start += bsize) {
  638. block_end = block_start + bsize;
  639. clear_buffer_new(bh);
  640. /*
  641. * Ignore blocks outside of our i/o range -
  642. * they may belong to unallocated clusters.
  643. */
  644. if (block_start >= to || block_end <= from) {
  645. if (PageUptodate(page))
  646. set_buffer_uptodate(bh);
  647. continue;
  648. }
  649. /*
  650. * For an allocating write with cluster size >= page
  651. * size, we always write the entire page.
  652. */
  653. if (new)
  654. set_buffer_new(bh);
  655. if (!buffer_mapped(bh)) {
  656. map_bh(bh, inode->i_sb, *p_blkno);
  657. unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
  658. }
  659. if (PageUptodate(page)) {
  660. if (!buffer_uptodate(bh))
  661. set_buffer_uptodate(bh);
  662. } else if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
  663. !buffer_new(bh) &&
  664. ocfs2_should_read_blk(inode, page, block_start) &&
  665. (block_start < from || block_end > to)) {
  666. ll_rw_block(READ, 1, &bh);
  667. *wait_bh++=bh;
  668. }
  669. *p_blkno = *p_blkno + 1;
  670. }
  671. /*
  672. * If we issued read requests - let them complete.
  673. */
  674. while(wait_bh > wait) {
  675. wait_on_buffer(*--wait_bh);
  676. if (!buffer_uptodate(*wait_bh))
  677. ret = -EIO;
  678. }
  679. if (ret == 0 || !new)
  680. return ret;
  681. /*
  682. * If we get -EIO above, zero out any newly allocated blocks
  683. * to avoid exposing stale data.
  684. */
  685. bh = head;
  686. block_start = 0;
  687. do {
  688. block_end = block_start + bsize;
  689. if (block_end <= from)
  690. goto next_bh;
  691. if (block_start >= to)
  692. break;
  693. zero_user(page, block_start, bh->b_size);
  694. set_buffer_uptodate(bh);
  695. mark_buffer_dirty(bh);
  696. next_bh:
  697. block_start = block_end;
  698. bh = bh->b_this_page;
  699. } while (bh != head);
  700. return ret;
  701. }
  702. #if (PAGE_CACHE_SIZE >= OCFS2_MAX_CLUSTERSIZE)
  703. #define OCFS2_MAX_CTXT_PAGES 1
  704. #else
  705. #define OCFS2_MAX_CTXT_PAGES (OCFS2_MAX_CLUSTERSIZE / PAGE_CACHE_SIZE)
  706. #endif
  707. #define OCFS2_MAX_CLUSTERS_PER_PAGE (PAGE_CACHE_SIZE / OCFS2_MIN_CLUSTERSIZE)
  708. /*
  709. * Describe the state of a single cluster to be written to.
  710. */
  711. struct ocfs2_write_cluster_desc {
  712. u32 c_cpos;
  713. u32 c_phys;
  714. /*
  715. * Give this a unique field because c_phys eventually gets
  716. * filled.
  717. */
  718. unsigned c_new;
  719. unsigned c_unwritten;
  720. unsigned c_needs_zero;
  721. };
  722. struct ocfs2_write_ctxt {
  723. /* Logical cluster position / len of write */
  724. u32 w_cpos;
  725. u32 w_clen;
  726. /* First cluster allocated in a nonsparse extend */
  727. u32 w_first_new_cpos;
  728. struct ocfs2_write_cluster_desc w_desc[OCFS2_MAX_CLUSTERS_PER_PAGE];
  729. /*
  730. * This is true if page_size > cluster_size.
  731. *
  732. * It triggers a set of special cases during write which might
  733. * have to deal with allocating writes to partial pages.
  734. */
  735. unsigned int w_large_pages;
  736. /*
  737. * Pages involved in this write.
  738. *
  739. * w_target_page is the page being written to by the user.
  740. *
  741. * w_pages is an array of pages which always contains
  742. * w_target_page, and in the case of an allocating write with
  743. * page_size < cluster size, it will contain zero'd and mapped
  744. * pages adjacent to w_target_page which need to be written
  745. * out in so that future reads from that region will get
  746. * zero's.
  747. */
  748. unsigned int w_num_pages;
  749. struct page *w_pages[OCFS2_MAX_CTXT_PAGES];
  750. struct page *w_target_page;
  751. /*
  752. * w_target_locked is used for page_mkwrite path indicating no unlocking
  753. * against w_target_page in ocfs2_write_end_nolock.
  754. */
  755. unsigned int w_target_locked:1;
  756. /*
  757. * ocfs2_write_end() uses this to know what the real range to
  758. * write in the target should be.
  759. */
  760. unsigned int w_target_from;
  761. unsigned int w_target_to;
  762. /*
  763. * We could use journal_current_handle() but this is cleaner,
  764. * IMHO -Mark
  765. */
  766. handle_t *w_handle;
  767. struct buffer_head *w_di_bh;
  768. struct ocfs2_cached_dealloc_ctxt w_dealloc;
  769. };
  770. void ocfs2_unlock_and_free_pages(struct page **pages, int num_pages)
  771. {
  772. int i;
  773. for(i = 0; i < num_pages; i++) {
  774. if (pages[i]) {
  775. unlock_page(pages[i]);
  776. mark_page_accessed(pages[i]);
  777. page_cache_release(pages[i]);
  778. }
  779. }
  780. }
  781. static void ocfs2_free_write_ctxt(struct ocfs2_write_ctxt *wc)
  782. {
  783. int i;
  784. /*
  785. * w_target_locked is only set to true in the page_mkwrite() case.
  786. * The intent is to allow us to lock the target page from write_begin()
  787. * to write_end(). The caller must hold a ref on w_target_page.
  788. */
  789. if (wc->w_target_locked) {
  790. BUG_ON(!wc->w_target_page);
  791. for (i = 0; i < wc->w_num_pages; i++) {
  792. if (wc->w_target_page == wc->w_pages[i]) {
  793. wc->w_pages[i] = NULL;
  794. break;
  795. }
  796. }
  797. mark_page_accessed(wc->w_target_page);
  798. page_cache_release(wc->w_target_page);
  799. }
  800. ocfs2_unlock_and_free_pages(wc->w_pages, wc->w_num_pages);
  801. brelse(wc->w_di_bh);
  802. kfree(wc);
  803. }
  804. static int ocfs2_alloc_write_ctxt(struct ocfs2_write_ctxt **wcp,
  805. struct ocfs2_super *osb, loff_t pos,
  806. unsigned len, struct buffer_head *di_bh)
  807. {
  808. u32 cend;
  809. struct ocfs2_write_ctxt *wc;
  810. wc = kzalloc(sizeof(struct ocfs2_write_ctxt), GFP_NOFS);
  811. if (!wc)
  812. return -ENOMEM;
  813. wc->w_cpos = pos >> osb->s_clustersize_bits;
  814. wc->w_first_new_cpos = UINT_MAX;
  815. cend = (pos + len - 1) >> osb->s_clustersize_bits;
  816. wc->w_clen = cend - wc->w_cpos + 1;
  817. get_bh(di_bh);
  818. wc->w_di_bh = di_bh;
  819. if (unlikely(PAGE_CACHE_SHIFT > osb->s_clustersize_bits))
  820. wc->w_large_pages = 1;
  821. else
  822. wc->w_large_pages = 0;
  823. ocfs2_init_dealloc_ctxt(&wc->w_dealloc);
  824. *wcp = wc;
  825. return 0;
  826. }
  827. /*
  828. * If a page has any new buffers, zero them out here, and mark them uptodate
  829. * and dirty so they'll be written out (in order to prevent uninitialised
  830. * block data from leaking). And clear the new bit.
  831. */
  832. static void ocfs2_zero_new_buffers(struct page *page, unsigned from, unsigned to)
  833. {
  834. unsigned int block_start, block_end;
  835. struct buffer_head *head, *bh;
  836. BUG_ON(!PageLocked(page));
  837. if (!page_has_buffers(page))
  838. return;
  839. bh = head = page_buffers(page);
  840. block_start = 0;
  841. do {
  842. block_end = block_start + bh->b_size;
  843. if (buffer_new(bh)) {
  844. if (block_end > from && block_start < to) {
  845. if (!PageUptodate(page)) {
  846. unsigned start, end;
  847. start = max(from, block_start);
  848. end = min(to, block_end);
  849. zero_user_segment(page, start, end);
  850. set_buffer_uptodate(bh);
  851. }
  852. clear_buffer_new(bh);
  853. mark_buffer_dirty(bh);
  854. }
  855. }
  856. block_start = block_end;
  857. bh = bh->b_this_page;
  858. } while (bh != head);
  859. }
  860. /*
  861. * Only called when we have a failure during allocating write to write
  862. * zero's to the newly allocated region.
  863. */
  864. static void ocfs2_write_failure(struct inode *inode,
  865. struct ocfs2_write_ctxt *wc,
  866. loff_t user_pos, unsigned user_len)
  867. {
  868. int i;
  869. unsigned from = user_pos & (PAGE_CACHE_SIZE - 1),
  870. to = user_pos + user_len;
  871. struct page *tmppage;
  872. ocfs2_zero_new_buffers(wc->w_target_page, from, to);
  873. for(i = 0; i < wc->w_num_pages; i++) {
  874. tmppage = wc->w_pages[i];
  875. if (page_has_buffers(tmppage)) {
  876. if (ocfs2_should_order_data(inode))
  877. ocfs2_jbd2_file_inode(wc->w_handle, inode);
  878. block_commit_write(tmppage, from, to);
  879. }
  880. }
  881. }
  882. static int ocfs2_prepare_page_for_write(struct inode *inode, u64 *p_blkno,
  883. struct ocfs2_write_ctxt *wc,
  884. struct page *page, u32 cpos,
  885. loff_t user_pos, unsigned user_len,
  886. int new)
  887. {
  888. int ret;
  889. unsigned int map_from = 0, map_to = 0;
  890. unsigned int cluster_start, cluster_end;
  891. unsigned int user_data_from = 0, user_data_to = 0;
  892. ocfs2_figure_cluster_boundaries(OCFS2_SB(inode->i_sb), cpos,
  893. &cluster_start, &cluster_end);
  894. /* treat the write as new if the a hole/lseek spanned across
  895. * the page boundary.
  896. */
  897. new = new | ((i_size_read(inode) <= page_offset(page)) &&
  898. (page_offset(page) <= user_pos));
  899. if (page == wc->w_target_page) {
  900. map_from = user_pos & (PAGE_CACHE_SIZE - 1);
  901. map_to = map_from + user_len;
  902. if (new)
  903. ret = ocfs2_map_page_blocks(page, p_blkno, inode,
  904. cluster_start, cluster_end,
  905. new);
  906. else
  907. ret = ocfs2_map_page_blocks(page, p_blkno, inode,
  908. map_from, map_to, new);
  909. if (ret) {
  910. mlog_errno(ret);
  911. goto out;
  912. }
  913. user_data_from = map_from;
  914. user_data_to = map_to;
  915. if (new) {
  916. map_from = cluster_start;
  917. map_to = cluster_end;
  918. }
  919. } else {
  920. /*
  921. * If we haven't allocated the new page yet, we
  922. * shouldn't be writing it out without copying user
  923. * data. This is likely a math error from the caller.
  924. */
  925. BUG_ON(!new);
  926. map_from = cluster_start;
  927. map_to = cluster_end;
  928. ret = ocfs2_map_page_blocks(page, p_blkno, inode,
  929. cluster_start, cluster_end, new);
  930. if (ret) {
  931. mlog_errno(ret);
  932. goto out;
  933. }
  934. }
  935. /*
  936. * Parts of newly allocated pages need to be zero'd.
  937. *
  938. * Above, we have also rewritten 'to' and 'from' - as far as
  939. * the rest of the function is concerned, the entire cluster
  940. * range inside of a page needs to be written.
  941. *
  942. * We can skip this if the page is up to date - it's already
  943. * been zero'd from being read in as a hole.
  944. */
  945. if (new && !PageUptodate(page))
  946. ocfs2_clear_page_regions(page, OCFS2_SB(inode->i_sb),
  947. cpos, user_data_from, user_data_to);
  948. flush_dcache_page(page);
  949. out:
  950. return ret;
  951. }
  952. /*
  953. * This function will only grab one clusters worth of pages.
  954. */
  955. static int ocfs2_grab_pages_for_write(struct address_space *mapping,
  956. struct ocfs2_write_ctxt *wc,
  957. u32 cpos, loff_t user_pos,
  958. unsigned user_len, int new,
  959. struct page *mmap_page)
  960. {
  961. int ret = 0, i;
  962. unsigned long start, target_index, end_index, index;
  963. struct inode *inode = mapping->host;
  964. loff_t last_byte;
  965. target_index = user_pos >> PAGE_CACHE_SHIFT;
  966. /*
  967. * Figure out how many pages we'll be manipulating here. For
  968. * non allocating write, we just change the one
  969. * page. Otherwise, we'll need a whole clusters worth. If we're
  970. * writing past i_size, we only need enough pages to cover the
  971. * last page of the write.
  972. */
  973. if (new) {
  974. wc->w_num_pages = ocfs2_pages_per_cluster(inode->i_sb);
  975. start = ocfs2_align_clusters_to_page_index(inode->i_sb, cpos);
  976. /*
  977. * We need the index *past* the last page we could possibly
  978. * touch. This is the page past the end of the write or
  979. * i_size, whichever is greater.
  980. */
  981. last_byte = max(user_pos + user_len, i_size_read(inode));
  982. BUG_ON(last_byte < 1);
  983. end_index = ((last_byte - 1) >> PAGE_CACHE_SHIFT) + 1;
  984. if ((start + wc->w_num_pages) > end_index)
  985. wc->w_num_pages = end_index - start;
  986. } else {
  987. wc->w_num_pages = 1;
  988. start = target_index;
  989. }
  990. for(i = 0; i < wc->w_num_pages; i++) {
  991. index = start + i;
  992. if (index == target_index && mmap_page) {
  993. /*
  994. * ocfs2_pagemkwrite() is a little different
  995. * and wants us to directly use the page
  996. * passed in.
  997. */
  998. lock_page(mmap_page);
  999. /* Exit and let the caller retry */
  1000. if (mmap_page->mapping != mapping) {
  1001. WARN_ON(mmap_page->mapping);
  1002. unlock_page(mmap_page);
  1003. ret = -EAGAIN;
  1004. goto out;
  1005. }
  1006. page_cache_get(mmap_page);
  1007. wc->w_pages[i] = mmap_page;
  1008. wc->w_target_locked = true;
  1009. } else {
  1010. wc->w_pages[i] = find_or_create_page(mapping, index,
  1011. GFP_NOFS);
  1012. if (!wc->w_pages[i]) {
  1013. ret = -ENOMEM;
  1014. mlog_errno(ret);
  1015. goto out;
  1016. }
  1017. }
  1018. wait_for_stable_page(wc->w_pages[i]);
  1019. if (index == target_index)
  1020. wc->w_target_page = wc->w_pages[i];
  1021. }
  1022. out:
  1023. if (ret)
  1024. wc->w_target_locked = false;
  1025. return ret;
  1026. }
  1027. /*
  1028. * Prepare a single cluster for write one cluster into the file.
  1029. */
  1030. static int ocfs2_write_cluster(struct address_space *mapping,
  1031. u32 phys, unsigned int unwritten,
  1032. unsigned int should_zero,
  1033. struct ocfs2_alloc_context *data_ac,
  1034. struct ocfs2_alloc_context *meta_ac,
  1035. struct ocfs2_write_ctxt *wc, u32 cpos,
  1036. loff_t user_pos, unsigned user_len)
  1037. {
  1038. int ret, i, new;
  1039. u64 v_blkno, p_blkno;
  1040. struct inode *inode = mapping->host;
  1041. struct ocfs2_extent_tree et;
  1042. new = phys == 0 ? 1 : 0;
  1043. if (new) {
  1044. u32 tmp_pos;
  1045. /*
  1046. * This is safe to call with the page locks - it won't take
  1047. * any additional semaphores or cluster locks.
  1048. */
  1049. tmp_pos = cpos;
  1050. ret = ocfs2_add_inode_data(OCFS2_SB(inode->i_sb), inode,
  1051. &tmp_pos, 1, 0, wc->w_di_bh,
  1052. wc->w_handle, data_ac,
  1053. meta_ac, NULL);
  1054. /*
  1055. * This shouldn't happen because we must have already
  1056. * calculated the correct meta data allocation required. The
  1057. * internal tree allocation code should know how to increase
  1058. * transaction credits itself.
  1059. *
  1060. * If need be, we could handle -EAGAIN for a
  1061. * RESTART_TRANS here.
  1062. */
  1063. mlog_bug_on_msg(ret == -EAGAIN,
  1064. "Inode %llu: EAGAIN return during allocation.\n",
  1065. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  1066. if (ret < 0) {
  1067. mlog_errno(ret);
  1068. goto out;
  1069. }
  1070. } else if (unwritten) {
  1071. ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode),
  1072. wc->w_di_bh);
  1073. ret = ocfs2_mark_extent_written(inode, &et,
  1074. wc->w_handle, cpos, 1, phys,
  1075. meta_ac, &wc->w_dealloc);
  1076. if (ret < 0) {
  1077. mlog_errno(ret);
  1078. goto out;
  1079. }
  1080. }
  1081. if (should_zero)
  1082. v_blkno = ocfs2_clusters_to_blocks(inode->i_sb, cpos);
  1083. else
  1084. v_blkno = user_pos >> inode->i_sb->s_blocksize_bits;
  1085. /*
  1086. * The only reason this should fail is due to an inability to
  1087. * find the extent added.
  1088. */
  1089. ret = ocfs2_extent_map_get_blocks(inode, v_blkno, &p_blkno, NULL,
  1090. NULL);
  1091. if (ret < 0) {
  1092. ocfs2_error(inode->i_sb, "Corrupting extend for inode %llu, "
  1093. "at logical block %llu",
  1094. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1095. (unsigned long long)v_blkno);
  1096. goto out;
  1097. }
  1098. BUG_ON(p_blkno == 0);
  1099. for(i = 0; i < wc->w_num_pages; i++) {
  1100. int tmpret;
  1101. tmpret = ocfs2_prepare_page_for_write(inode, &p_blkno, wc,
  1102. wc->w_pages[i], cpos,
  1103. user_pos, user_len,
  1104. should_zero);
  1105. if (tmpret) {
  1106. mlog_errno(tmpret);
  1107. if (ret == 0)
  1108. ret = tmpret;
  1109. }
  1110. }
  1111. /*
  1112. * We only have cleanup to do in case of allocating write.
  1113. */
  1114. if (ret && new)
  1115. ocfs2_write_failure(inode, wc, user_pos, user_len);
  1116. out:
  1117. return ret;
  1118. }
  1119. static int ocfs2_write_cluster_by_desc(struct address_space *mapping,
  1120. struct ocfs2_alloc_context *data_ac,
  1121. struct ocfs2_alloc_context *meta_ac,
  1122. struct ocfs2_write_ctxt *wc,
  1123. loff_t pos, unsigned len)
  1124. {
  1125. int ret, i;
  1126. loff_t cluster_off;
  1127. unsigned int local_len = len;
  1128. struct ocfs2_write_cluster_desc *desc;
  1129. struct ocfs2_super *osb = OCFS2_SB(mapping->host->i_sb);
  1130. for (i = 0; i < wc->w_clen; i++) {
  1131. desc = &wc->w_desc[i];
  1132. /*
  1133. * We have to make sure that the total write passed in
  1134. * doesn't extend past a single cluster.
  1135. */
  1136. local_len = len;
  1137. cluster_off = pos & (osb->s_clustersize - 1);
  1138. if ((cluster_off + local_len) > osb->s_clustersize)
  1139. local_len = osb->s_clustersize - cluster_off;
  1140. ret = ocfs2_write_cluster(mapping, desc->c_phys,
  1141. desc->c_unwritten,
  1142. desc->c_needs_zero,
  1143. data_ac, meta_ac,
  1144. wc, desc->c_cpos, pos, local_len);
  1145. if (ret) {
  1146. mlog_errno(ret);
  1147. goto out;
  1148. }
  1149. len -= local_len;
  1150. pos += local_len;
  1151. }
  1152. ret = 0;
  1153. out:
  1154. return ret;
  1155. }
  1156. /*
  1157. * ocfs2_write_end() wants to know which parts of the target page it
  1158. * should complete the write on. It's easiest to compute them ahead of
  1159. * time when a more complete view of the write is available.
  1160. */
  1161. static void ocfs2_set_target_boundaries(struct ocfs2_super *osb,
  1162. struct ocfs2_write_ctxt *wc,
  1163. loff_t pos, unsigned len, int alloc)
  1164. {
  1165. struct ocfs2_write_cluster_desc *desc;
  1166. wc->w_target_from = pos & (PAGE_CACHE_SIZE - 1);
  1167. wc->w_target_to = wc->w_target_from + len;
  1168. if (alloc == 0)
  1169. return;
  1170. /*
  1171. * Allocating write - we may have different boundaries based
  1172. * on page size and cluster size.
  1173. *
  1174. * NOTE: We can no longer compute one value from the other as
  1175. * the actual write length and user provided length may be
  1176. * different.
  1177. */
  1178. if (wc->w_large_pages) {
  1179. /*
  1180. * We only care about the 1st and last cluster within
  1181. * our range and whether they should be zero'd or not. Either
  1182. * value may be extended out to the start/end of a
  1183. * newly allocated cluster.
  1184. */
  1185. desc = &wc->w_desc[0];
  1186. if (desc->c_needs_zero)
  1187. ocfs2_figure_cluster_boundaries(osb,
  1188. desc->c_cpos,
  1189. &wc->w_target_from,
  1190. NULL);
  1191. desc = &wc->w_desc[wc->w_clen - 1];
  1192. if (desc->c_needs_zero)
  1193. ocfs2_figure_cluster_boundaries(osb,
  1194. desc->c_cpos,
  1195. NULL,
  1196. &wc->w_target_to);
  1197. } else {
  1198. wc->w_target_from = 0;
  1199. wc->w_target_to = PAGE_CACHE_SIZE;
  1200. }
  1201. }
  1202. /*
  1203. * Populate each single-cluster write descriptor in the write context
  1204. * with information about the i/o to be done.
  1205. *
  1206. * Returns the number of clusters that will have to be allocated, as
  1207. * well as a worst case estimate of the number of extent records that
  1208. * would have to be created during a write to an unwritten region.
  1209. */
  1210. static int ocfs2_populate_write_desc(struct inode *inode,
  1211. struct ocfs2_write_ctxt *wc,
  1212. unsigned int *clusters_to_alloc,
  1213. unsigned int *extents_to_split)
  1214. {
  1215. int ret;
  1216. struct ocfs2_write_cluster_desc *desc;
  1217. unsigned int num_clusters = 0;
  1218. unsigned int ext_flags = 0;
  1219. u32 phys = 0;
  1220. int i;
  1221. *clusters_to_alloc = 0;
  1222. *extents_to_split = 0;
  1223. for (i = 0; i < wc->w_clen; i++) {
  1224. desc = &wc->w_desc[i];
  1225. desc->c_cpos = wc->w_cpos + i;
  1226. if (num_clusters == 0) {
  1227. /*
  1228. * Need to look up the next extent record.
  1229. */
  1230. ret = ocfs2_get_clusters(inode, desc->c_cpos, &phys,
  1231. &num_clusters, &ext_flags);
  1232. if (ret) {
  1233. mlog_errno(ret);
  1234. goto out;
  1235. }
  1236. /* We should already CoW the refcountd extent. */
  1237. BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
  1238. /*
  1239. * Assume worst case - that we're writing in
  1240. * the middle of the extent.
  1241. *
  1242. * We can assume that the write proceeds from
  1243. * left to right, in which case the extent
  1244. * insert code is smart enough to coalesce the
  1245. * next splits into the previous records created.
  1246. */
  1247. if (ext_flags & OCFS2_EXT_UNWRITTEN)
  1248. *extents_to_split = *extents_to_split + 2;
  1249. } else if (phys) {
  1250. /*
  1251. * Only increment phys if it doesn't describe
  1252. * a hole.
  1253. */
  1254. phys++;
  1255. }
  1256. /*
  1257. * If w_first_new_cpos is < UINT_MAX, we have a non-sparse
  1258. * file that got extended. w_first_new_cpos tells us
  1259. * where the newly allocated clusters are so we can
  1260. * zero them.
  1261. */
  1262. if (desc->c_cpos >= wc->w_first_new_cpos) {
  1263. BUG_ON(phys == 0);
  1264. desc->c_needs_zero = 1;
  1265. }
  1266. desc->c_phys = phys;
  1267. if (phys == 0) {
  1268. desc->c_new = 1;
  1269. desc->c_needs_zero = 1;
  1270. *clusters_to_alloc = *clusters_to_alloc + 1;
  1271. }
  1272. if (ext_flags & OCFS2_EXT_UNWRITTEN) {
  1273. desc->c_unwritten = 1;
  1274. desc->c_needs_zero = 1;
  1275. }
  1276. num_clusters--;
  1277. }
  1278. ret = 0;
  1279. out:
  1280. return ret;
  1281. }
  1282. static int ocfs2_write_begin_inline(struct address_space *mapping,
  1283. struct inode *inode,
  1284. struct ocfs2_write_ctxt *wc)
  1285. {
  1286. int ret;
  1287. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1288. struct page *page;
  1289. handle_t *handle;
  1290. struct ocfs2_dinode *di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
  1291. page = find_or_create_page(mapping, 0, GFP_NOFS);
  1292. if (!page) {
  1293. ret = -ENOMEM;
  1294. mlog_errno(ret);
  1295. goto out;
  1296. }
  1297. /*
  1298. * If we don't set w_num_pages then this page won't get unlocked
  1299. * and freed on cleanup of the write context.
  1300. */
  1301. wc->w_pages[0] = wc->w_target_page = page;
  1302. wc->w_num_pages = 1;
  1303. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1304. if (IS_ERR(handle)) {
  1305. ret = PTR_ERR(handle);
  1306. mlog_errno(ret);
  1307. goto out;
  1308. }
  1309. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), wc->w_di_bh,
  1310. OCFS2_JOURNAL_ACCESS_WRITE);
  1311. if (ret) {
  1312. ocfs2_commit_trans(osb, handle);
  1313. mlog_errno(ret);
  1314. goto out;
  1315. }
  1316. if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL))
  1317. ocfs2_set_inode_data_inline(inode, di);
  1318. if (!PageUptodate(page)) {
  1319. ret = ocfs2_read_inline_data(inode, page, wc->w_di_bh);
  1320. if (ret) {
  1321. ocfs2_commit_trans(osb, handle);
  1322. goto out;
  1323. }
  1324. }
  1325. wc->w_handle = handle;
  1326. out:
  1327. return ret;
  1328. }
  1329. int ocfs2_size_fits_inline_data(struct buffer_head *di_bh, u64 new_size)
  1330. {
  1331. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1332. if (new_size <= le16_to_cpu(di->id2.i_data.id_count))
  1333. return 1;
  1334. return 0;
  1335. }
  1336. static int ocfs2_try_to_write_inline_data(struct address_space *mapping,
  1337. struct inode *inode, loff_t pos,
  1338. unsigned len, struct page *mmap_page,
  1339. struct ocfs2_write_ctxt *wc)
  1340. {
  1341. int ret, written = 0;
  1342. loff_t end = pos + len;
  1343. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1344. struct ocfs2_dinode *di = NULL;
  1345. trace_ocfs2_try_to_write_inline_data((unsigned long long)oi->ip_blkno,
  1346. len, (unsigned long long)pos,
  1347. oi->ip_dyn_features);
  1348. /*
  1349. * Handle inodes which already have inline data 1st.
  1350. */
  1351. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1352. if (mmap_page == NULL &&
  1353. ocfs2_size_fits_inline_data(wc->w_di_bh, end))
  1354. goto do_inline_write;
  1355. /*
  1356. * The write won't fit - we have to give this inode an
  1357. * inline extent list now.
  1358. */
  1359. ret = ocfs2_convert_inline_data_to_extents(inode, wc->w_di_bh);
  1360. if (ret)
  1361. mlog_errno(ret);
  1362. goto out;
  1363. }
  1364. /*
  1365. * Check whether the inode can accept inline data.
  1366. */
  1367. if (oi->ip_clusters != 0 || i_size_read(inode) != 0)
  1368. return 0;
  1369. /*
  1370. * Check whether the write can fit.
  1371. */
  1372. di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
  1373. if (mmap_page ||
  1374. end > ocfs2_max_inline_data_with_xattr(inode->i_sb, di))
  1375. return 0;
  1376. do_inline_write:
  1377. ret = ocfs2_write_begin_inline(mapping, inode, wc);
  1378. if (ret) {
  1379. mlog_errno(ret);
  1380. goto out;
  1381. }
  1382. /*
  1383. * This signals to the caller that the data can be written
  1384. * inline.
  1385. */
  1386. written = 1;
  1387. out:
  1388. return written ? written : ret;
  1389. }
  1390. /*
  1391. * This function only does anything for file systems which can't
  1392. * handle sparse files.
  1393. *
  1394. * What we want to do here is fill in any hole between the current end
  1395. * of allocation and the end of our write. That way the rest of the
  1396. * write path can treat it as an non-allocating write, which has no
  1397. * special case code for sparse/nonsparse files.
  1398. */
  1399. static int ocfs2_expand_nonsparse_inode(struct inode *inode,
  1400. struct buffer_head *di_bh,
  1401. loff_t pos, unsigned len,
  1402. struct ocfs2_write_ctxt *wc)
  1403. {
  1404. int ret;
  1405. loff_t newsize = pos + len;
  1406. BUG_ON(ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)));
  1407. if (newsize <= i_size_read(inode))
  1408. return 0;
  1409. ret = ocfs2_extend_no_holes(inode, di_bh, newsize, pos);
  1410. if (ret)
  1411. mlog_errno(ret);
  1412. wc->w_first_new_cpos =
  1413. ocfs2_clusters_for_bytes(inode->i_sb, i_size_read(inode));
  1414. return ret;
  1415. }
  1416. static int ocfs2_zero_tail(struct inode *inode, struct buffer_head *di_bh,
  1417. loff_t pos)
  1418. {
  1419. int ret = 0;
  1420. BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)));
  1421. if (pos > i_size_read(inode))
  1422. ret = ocfs2_zero_extend(inode, di_bh, pos);
  1423. return ret;
  1424. }
  1425. /*
  1426. * Try to flush truncate logs if we can free enough clusters from it.
  1427. * As for return value, "< 0" means error, "0" no space and "1" means
  1428. * we have freed enough spaces and let the caller try to allocate again.
  1429. */
  1430. static int ocfs2_try_to_free_truncate_log(struct ocfs2_super *osb,
  1431. unsigned int needed)
  1432. {
  1433. tid_t target;
  1434. int ret = 0;
  1435. unsigned int truncated_clusters;
  1436. mutex_lock(&osb->osb_tl_inode->i_mutex);
  1437. truncated_clusters = osb->truncated_clusters;
  1438. mutex_unlock(&osb->osb_tl_inode->i_mutex);
  1439. /*
  1440. * Check whether we can succeed in allocating if we free
  1441. * the truncate log.
  1442. */
  1443. if (truncated_clusters < needed)
  1444. goto out;
  1445. ret = ocfs2_flush_truncate_log(osb);
  1446. if (ret) {
  1447. mlog_errno(ret);
  1448. goto out;
  1449. }
  1450. if (jbd2_journal_start_commit(osb->journal->j_journal, &target)) {
  1451. jbd2_log_wait_commit(osb->journal->j_journal, target);
  1452. ret = 1;
  1453. }
  1454. out:
  1455. return ret;
  1456. }
  1457. int ocfs2_write_begin_nolock(struct file *filp,
  1458. struct address_space *mapping,
  1459. loff_t pos, unsigned len, unsigned flags,
  1460. struct page **pagep, void **fsdata,
  1461. struct buffer_head *di_bh, struct page *mmap_page)
  1462. {
  1463. int ret, cluster_of_pages, credits = OCFS2_INODE_UPDATE_CREDITS;
  1464. unsigned int clusters_to_alloc, extents_to_split, clusters_need = 0;
  1465. struct ocfs2_write_ctxt *wc;
  1466. struct inode *inode = mapping->host;
  1467. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1468. struct ocfs2_dinode *di;
  1469. struct ocfs2_alloc_context *data_ac = NULL;
  1470. struct ocfs2_alloc_context *meta_ac = NULL;
  1471. handle_t *handle;
  1472. struct ocfs2_extent_tree et;
  1473. int try_free = 1, ret1;
  1474. try_again:
  1475. ret = ocfs2_alloc_write_ctxt(&wc, osb, pos, len, di_bh);
  1476. if (ret) {
  1477. mlog_errno(ret);
  1478. return ret;
  1479. }
  1480. if (ocfs2_supports_inline_data(osb)) {
  1481. ret = ocfs2_try_to_write_inline_data(mapping, inode, pos, len,
  1482. mmap_page, wc);
  1483. if (ret == 1) {
  1484. ret = 0;
  1485. goto success;
  1486. }
  1487. if (ret < 0) {
  1488. mlog_errno(ret);
  1489. goto out;
  1490. }
  1491. }
  1492. if (ocfs2_sparse_alloc(osb))
  1493. ret = ocfs2_zero_tail(inode, di_bh, pos);
  1494. else
  1495. ret = ocfs2_expand_nonsparse_inode(inode, di_bh, pos, len,
  1496. wc);
  1497. if (ret) {
  1498. mlog_errno(ret);
  1499. goto out;
  1500. }
  1501. ret = ocfs2_check_range_for_refcount(inode, pos, len);
  1502. if (ret < 0) {
  1503. mlog_errno(ret);
  1504. goto out;
  1505. } else if (ret == 1) {
  1506. clusters_need = wc->w_clen;
  1507. ret = ocfs2_refcount_cow(inode, filp, di_bh,
  1508. wc->w_cpos, wc->w_clen, UINT_MAX);
  1509. if (ret) {
  1510. mlog_errno(ret);
  1511. goto out;
  1512. }
  1513. }
  1514. ret = ocfs2_populate_write_desc(inode, wc, &clusters_to_alloc,
  1515. &extents_to_split);
  1516. if (ret) {
  1517. mlog_errno(ret);
  1518. goto out;
  1519. }
  1520. clusters_need += clusters_to_alloc;
  1521. di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
  1522. trace_ocfs2_write_begin_nolock(
  1523. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1524. (long long)i_size_read(inode),
  1525. le32_to_cpu(di->i_clusters),
  1526. pos, len, flags, mmap_page,
  1527. clusters_to_alloc, extents_to_split);
  1528. /*
  1529. * We set w_target_from, w_target_to here so that
  1530. * ocfs2_write_end() knows which range in the target page to
  1531. * write out. An allocation requires that we write the entire
  1532. * cluster range.
  1533. */
  1534. if (clusters_to_alloc || extents_to_split) {
  1535. /*
  1536. * XXX: We are stretching the limits of
  1537. * ocfs2_lock_allocators(). It greatly over-estimates
  1538. * the work to be done.
  1539. */
  1540. ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode),
  1541. wc->w_di_bh);
  1542. ret = ocfs2_lock_allocators(inode, &et,
  1543. clusters_to_alloc, extents_to_split,
  1544. &data_ac, &meta_ac);
  1545. if (ret) {
  1546. mlog_errno(ret);
  1547. goto out;
  1548. }
  1549. if (data_ac)
  1550. data_ac->ac_resv = &OCFS2_I(inode)->ip_la_data_resv;
  1551. credits = ocfs2_calc_extend_credits(inode->i_sb,
  1552. &di->id2.i_list,
  1553. clusters_to_alloc);
  1554. }
  1555. /*
  1556. * We have to zero sparse allocated clusters, unwritten extent clusters,
  1557. * and non-sparse clusters we just extended. For non-sparse writes,
  1558. * we know zeros will only be needed in the first and/or last cluster.
  1559. */
  1560. if (clusters_to_alloc || extents_to_split ||
  1561. (wc->w_clen && (wc->w_desc[0].c_needs_zero ||
  1562. wc->w_desc[wc->w_clen - 1].c_needs_zero)))
  1563. cluster_of_pages = 1;
  1564. else
  1565. cluster_of_pages = 0;
  1566. ocfs2_set_target_boundaries(osb, wc, pos, len, cluster_of_pages);
  1567. handle = ocfs2_start_trans(osb, credits);
  1568. if (IS_ERR(handle)) {
  1569. ret = PTR_ERR(handle);
  1570. mlog_errno(ret);
  1571. goto out;
  1572. }
  1573. wc->w_handle = handle;
  1574. if (clusters_to_alloc) {
  1575. ret = dquot_alloc_space_nodirty(inode,
  1576. ocfs2_clusters_to_bytes(osb->sb, clusters_to_alloc));
  1577. if (ret)
  1578. goto out_commit;
  1579. }
  1580. /*
  1581. * We don't want this to fail in ocfs2_write_end(), so do it
  1582. * here.
  1583. */
  1584. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), wc->w_di_bh,
  1585. OCFS2_JOURNAL_ACCESS_WRITE);
  1586. if (ret) {
  1587. mlog_errno(ret);
  1588. goto out_quota;
  1589. }
  1590. /*
  1591. * Fill our page array first. That way we've grabbed enough so
  1592. * that we can zero and flush if we error after adding the
  1593. * extent.
  1594. */
  1595. ret = ocfs2_grab_pages_for_write(mapping, wc, wc->w_cpos, pos, len,
  1596. cluster_of_pages, mmap_page);
  1597. if (ret && ret != -EAGAIN) {
  1598. mlog_errno(ret);
  1599. goto out_quota;
  1600. }
  1601. /*
  1602. * ocfs2_grab_pages_for_write() returns -EAGAIN if it could not lock
  1603. * the target page. In this case, we exit with no error and no target
  1604. * page. This will trigger the caller, page_mkwrite(), to re-try
  1605. * the operation.
  1606. */
  1607. if (ret == -EAGAIN) {
  1608. BUG_ON(wc->w_target_page);
  1609. ret = 0;
  1610. goto out_quota;
  1611. }
  1612. ret = ocfs2_write_cluster_by_desc(mapping, data_ac, meta_ac, wc, pos,
  1613. len);
  1614. if (ret) {
  1615. mlog_errno(ret);
  1616. goto out_quota;
  1617. }
  1618. if (data_ac)
  1619. ocfs2_free_alloc_context(data_ac);
  1620. if (meta_ac)
  1621. ocfs2_free_alloc_context(meta_ac);
  1622. success:
  1623. *pagep = wc->w_target_page;
  1624. *fsdata = wc;
  1625. return 0;
  1626. out_quota:
  1627. if (clusters_to_alloc)
  1628. dquot_free_space(inode,
  1629. ocfs2_clusters_to_bytes(osb->sb, clusters_to_alloc));
  1630. out_commit:
  1631. ocfs2_commit_trans(osb, handle);
  1632. out:
  1633. ocfs2_free_write_ctxt(wc);
  1634. if (data_ac)
  1635. ocfs2_free_alloc_context(data_ac);
  1636. if (meta_ac)
  1637. ocfs2_free_alloc_context(meta_ac);
  1638. if (ret == -ENOSPC && try_free) {
  1639. /*
  1640. * Try to free some truncate log so that we can have enough
  1641. * clusters to allocate.
  1642. */
  1643. try_free = 0;
  1644. ret1 = ocfs2_try_to_free_truncate_log(osb, clusters_need);
  1645. if (ret1 == 1)
  1646. goto try_again;
  1647. if (ret1 < 0)
  1648. mlog_errno(ret1);
  1649. }
  1650. return ret;
  1651. }
  1652. static int ocfs2_write_begin(struct file *file, struct address_space *mapping,
  1653. loff_t pos, unsigned len, unsigned flags,
  1654. struct page **pagep, void **fsdata)
  1655. {
  1656. int ret;
  1657. struct buffer_head *di_bh = NULL;
  1658. struct inode *inode = mapping->host;
  1659. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  1660. if (ret) {
  1661. mlog_errno(ret);
  1662. return ret;
  1663. }
  1664. /*
  1665. * Take alloc sem here to prevent concurrent lookups. That way
  1666. * the mapping, zeroing and tree manipulation within
  1667. * ocfs2_write() will be safe against ->readpage(). This
  1668. * should also serve to lock out allocation from a shared
  1669. * writeable region.
  1670. */
  1671. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  1672. ret = ocfs2_write_begin_nolock(file, mapping, pos, len, flags, pagep,
  1673. fsdata, di_bh, NULL);
  1674. if (ret) {
  1675. mlog_errno(ret);
  1676. goto out_fail;
  1677. }
  1678. brelse(di_bh);
  1679. return 0;
  1680. out_fail:
  1681. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  1682. brelse(di_bh);
  1683. ocfs2_inode_unlock(inode, 1);
  1684. return ret;
  1685. }
  1686. static void ocfs2_write_end_inline(struct inode *inode, loff_t pos,
  1687. unsigned len, unsigned *copied,
  1688. struct ocfs2_dinode *di,
  1689. struct ocfs2_write_ctxt *wc)
  1690. {
  1691. void *kaddr;
  1692. if (unlikely(*copied < len)) {
  1693. if (!PageUptodate(wc->w_target_page)) {
  1694. *copied = 0;
  1695. return;
  1696. }
  1697. }
  1698. kaddr = kmap_atomic(wc->w_target_page);
  1699. memcpy(di->id2.i_data.id_data + pos, kaddr + pos, *copied);
  1700. kunmap_atomic(kaddr);
  1701. trace_ocfs2_write_end_inline(
  1702. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1703. (unsigned long long)pos, *copied,
  1704. le16_to_cpu(di->id2.i_data.id_count),
  1705. le16_to_cpu(di->i_dyn_features));
  1706. }
  1707. int ocfs2_write_end_nolock(struct address_space *mapping,
  1708. loff_t pos, unsigned len, unsigned copied,
  1709. struct page *page, void *fsdata)
  1710. {
  1711. int i;
  1712. unsigned from, to, start = pos & (PAGE_CACHE_SIZE - 1);
  1713. struct inode *inode = mapping->host;
  1714. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1715. struct ocfs2_write_ctxt *wc = fsdata;
  1716. struct ocfs2_dinode *di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
  1717. handle_t *handle = wc->w_handle;
  1718. struct page *tmppage;
  1719. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1720. ocfs2_write_end_inline(inode, pos, len, &copied, di, wc);
  1721. goto out_write_size;
  1722. }
  1723. if (unlikely(copied < len)) {
  1724. if (!PageUptodate(wc->w_target_page))
  1725. copied = 0;
  1726. ocfs2_zero_new_buffers(wc->w_target_page, start+copied,
  1727. start+len);
  1728. }
  1729. flush_dcache_page(wc->w_target_page);
  1730. for(i = 0; i < wc->w_num_pages; i++) {
  1731. tmppage = wc->w_pages[i];
  1732. if (tmppage == wc->w_target_page) {
  1733. from = wc->w_target_from;
  1734. to = wc->w_target_to;
  1735. BUG_ON(from > PAGE_CACHE_SIZE ||
  1736. to > PAGE_CACHE_SIZE ||
  1737. to < from);
  1738. } else {
  1739. /*
  1740. * Pages adjacent to the target (if any) imply
  1741. * a hole-filling write in which case we want
  1742. * to flush their entire range.
  1743. */
  1744. from = 0;
  1745. to = PAGE_CACHE_SIZE;
  1746. }
  1747. if (page_has_buffers(tmppage)) {
  1748. if (ocfs2_should_order_data(inode))
  1749. ocfs2_jbd2_file_inode(wc->w_handle, inode);
  1750. block_commit_write(tmppage, from, to);
  1751. }
  1752. }
  1753. out_write_size:
  1754. pos += copied;
  1755. if (pos > inode->i_size) {
  1756. i_size_write(inode, pos);
  1757. mark_inode_dirty(inode);
  1758. }
  1759. inode->i_blocks = ocfs2_inode_sector_count(inode);
  1760. di->i_size = cpu_to_le64((u64)i_size_read(inode));
  1761. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1762. di->i_mtime = di->i_ctime = cpu_to_le64(inode->i_mtime.tv_sec);
  1763. di->i_mtime_nsec = di->i_ctime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
  1764. ocfs2_journal_dirty(handle, wc->w_di_bh);
  1765. ocfs2_commit_trans(osb, handle);
  1766. ocfs2_run_deallocs(osb, &wc->w_dealloc);
  1767. ocfs2_free_write_ctxt(wc);
  1768. return copied;
  1769. }
  1770. static int ocfs2_write_end(struct file *file, struct address_space *mapping,
  1771. loff_t pos, unsigned len, unsigned copied,
  1772. struct page *page, void *fsdata)
  1773. {
  1774. int ret;
  1775. struct inode *inode = mapping->host;
  1776. ret = ocfs2_write_end_nolock(mapping, pos, len, copied, page, fsdata);
  1777. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  1778. ocfs2_inode_unlock(inode, 1);
  1779. return ret;
  1780. }
  1781. const struct address_space_operations ocfs2_aops = {
  1782. .readpage = ocfs2_readpage,
  1783. .readpages = ocfs2_readpages,
  1784. .writepage = ocfs2_writepage,
  1785. .write_begin = ocfs2_write_begin,
  1786. .write_end = ocfs2_write_end,
  1787. .bmap = ocfs2_bmap,
  1788. .direct_IO = ocfs2_direct_IO,
  1789. .invalidatepage = ocfs2_invalidatepage,
  1790. .releasepage = ocfs2_releasepage,
  1791. .migratepage = buffer_migrate_page,
  1792. .is_partially_uptodate = block_is_partially_uptodate,
  1793. .error_remove_page = generic_error_remove_page,
  1794. };