aops.c 63 KB

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