xfs_inode.c 105 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662
  1. /*
  2. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <linux/log2.h>
  19. #include "xfs.h"
  20. #include "xfs_fs.h"
  21. #include "xfs_types.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_trans_priv.h"
  26. #include "xfs_sb.h"
  27. #include "xfs_ag.h"
  28. #include "xfs_mount.h"
  29. #include "xfs_bmap_btree.h"
  30. #include "xfs_alloc_btree.h"
  31. #include "xfs_ialloc_btree.h"
  32. #include "xfs_attr_sf.h"
  33. #include "xfs_dinode.h"
  34. #include "xfs_inode.h"
  35. #include "xfs_buf_item.h"
  36. #include "xfs_inode_item.h"
  37. #include "xfs_btree.h"
  38. #include "xfs_alloc.h"
  39. #include "xfs_ialloc.h"
  40. #include "xfs_bmap.h"
  41. #include "xfs_error.h"
  42. #include "xfs_utils.h"
  43. #include "xfs_quota.h"
  44. #include "xfs_filestream.h"
  45. #include "xfs_vnodeops.h"
  46. #include "xfs_trace.h"
  47. kmem_zone_t *xfs_ifork_zone;
  48. kmem_zone_t *xfs_inode_zone;
  49. /*
  50. * Used in xfs_itruncate_extents(). This is the maximum number of extents
  51. * freed from a file in a single transaction.
  52. */
  53. #define XFS_ITRUNC_MAX_EXTENTS 2
  54. STATIC int xfs_iflush_int(xfs_inode_t *, xfs_buf_t *);
  55. STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int);
  56. STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int);
  57. STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int);
  58. /*
  59. * helper function to extract extent size hint from inode
  60. */
  61. xfs_extlen_t
  62. xfs_get_extsz_hint(
  63. struct xfs_inode *ip)
  64. {
  65. if ((ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE) && ip->i_d.di_extsize)
  66. return ip->i_d.di_extsize;
  67. if (XFS_IS_REALTIME_INODE(ip))
  68. return ip->i_mount->m_sb.sb_rextsize;
  69. return 0;
  70. }
  71. #ifdef DEBUG
  72. /*
  73. * Make sure that the extents in the given memory buffer
  74. * are valid.
  75. */
  76. STATIC void
  77. xfs_validate_extents(
  78. xfs_ifork_t *ifp,
  79. int nrecs,
  80. xfs_exntfmt_t fmt)
  81. {
  82. xfs_bmbt_irec_t irec;
  83. xfs_bmbt_rec_host_t rec;
  84. int i;
  85. for (i = 0; i < nrecs; i++) {
  86. xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
  87. rec.l0 = get_unaligned(&ep->l0);
  88. rec.l1 = get_unaligned(&ep->l1);
  89. xfs_bmbt_get_all(&rec, &irec);
  90. if (fmt == XFS_EXTFMT_NOSTATE)
  91. ASSERT(irec.br_state == XFS_EXT_NORM);
  92. }
  93. }
  94. #else /* DEBUG */
  95. #define xfs_validate_extents(ifp, nrecs, fmt)
  96. #endif /* DEBUG */
  97. /*
  98. * Check that none of the inode's in the buffer have a next
  99. * unlinked field of 0.
  100. */
  101. #if defined(DEBUG)
  102. void
  103. xfs_inobp_check(
  104. xfs_mount_t *mp,
  105. xfs_buf_t *bp)
  106. {
  107. int i;
  108. int j;
  109. xfs_dinode_t *dip;
  110. j = mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog;
  111. for (i = 0; i < j; i++) {
  112. dip = (xfs_dinode_t *)xfs_buf_offset(bp,
  113. i * mp->m_sb.sb_inodesize);
  114. if (!dip->di_next_unlinked) {
  115. xfs_alert(mp,
  116. "Detected bogus zero next_unlinked field in incore inode buffer 0x%p.",
  117. bp);
  118. ASSERT(dip->di_next_unlinked);
  119. }
  120. }
  121. }
  122. #endif
  123. /*
  124. * This routine is called to map an inode to the buffer containing the on-disk
  125. * version of the inode. It returns a pointer to the buffer containing the
  126. * on-disk inode in the bpp parameter, and in the dipp parameter it returns a
  127. * pointer to the on-disk inode within that buffer.
  128. *
  129. * If a non-zero error is returned, then the contents of bpp and dipp are
  130. * undefined.
  131. */
  132. int
  133. xfs_imap_to_bp(
  134. struct xfs_mount *mp,
  135. struct xfs_trans *tp,
  136. struct xfs_imap *imap,
  137. struct xfs_dinode **dipp,
  138. struct xfs_buf **bpp,
  139. uint buf_flags,
  140. uint iget_flags)
  141. {
  142. struct xfs_buf *bp;
  143. int error;
  144. int i;
  145. int ni;
  146. buf_flags |= XBF_UNMAPPED;
  147. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, imap->im_blkno,
  148. (int)imap->im_len, buf_flags, &bp);
  149. if (error) {
  150. if (error != EAGAIN) {
  151. xfs_warn(mp,
  152. "%s: xfs_trans_read_buf() returned error %d.",
  153. __func__, error);
  154. } else {
  155. ASSERT(buf_flags & XBF_TRYLOCK);
  156. }
  157. return error;
  158. }
  159. /*
  160. * Validate the magic number and version of every inode in the buffer
  161. * (if DEBUG kernel) or the first inode in the buffer, otherwise.
  162. */
  163. #ifdef DEBUG
  164. ni = BBTOB(imap->im_len) >> mp->m_sb.sb_inodelog;
  165. #else /* usual case */
  166. ni = 1;
  167. #endif
  168. for (i = 0; i < ni; i++) {
  169. int di_ok;
  170. xfs_dinode_t *dip;
  171. dip = (xfs_dinode_t *)xfs_buf_offset(bp,
  172. (i << mp->m_sb.sb_inodelog));
  173. di_ok = dip->di_magic == cpu_to_be16(XFS_DINODE_MAGIC) &&
  174. XFS_DINODE_GOOD_VERSION(dip->di_version);
  175. if (unlikely(XFS_TEST_ERROR(!di_ok, mp,
  176. XFS_ERRTAG_ITOBP_INOTOBP,
  177. XFS_RANDOM_ITOBP_INOTOBP))) {
  178. if (iget_flags & XFS_IGET_UNTRUSTED) {
  179. xfs_trans_brelse(tp, bp);
  180. return XFS_ERROR(EINVAL);
  181. }
  182. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_HIGH,
  183. mp, dip);
  184. #ifdef DEBUG
  185. xfs_emerg(mp,
  186. "bad inode magic/vsn daddr %lld #%d (magic=%x)",
  187. (unsigned long long)imap->im_blkno, i,
  188. be16_to_cpu(dip->di_magic));
  189. ASSERT(0);
  190. #endif
  191. xfs_trans_brelse(tp, bp);
  192. return XFS_ERROR(EFSCORRUPTED);
  193. }
  194. }
  195. xfs_inobp_check(mp, bp);
  196. *bpp = bp;
  197. *dipp = (struct xfs_dinode *)xfs_buf_offset(bp, imap->im_boffset);
  198. return 0;
  199. }
  200. /*
  201. * Move inode type and inode format specific information from the
  202. * on-disk inode to the in-core inode. For fifos, devs, and sockets
  203. * this means set if_rdev to the proper value. For files, directories,
  204. * and symlinks this means to bring in the in-line data or extent
  205. * pointers. For a file in B-tree format, only the root is immediately
  206. * brought in-core. The rest will be in-lined in if_extents when it
  207. * is first referenced (see xfs_iread_extents()).
  208. */
  209. STATIC int
  210. xfs_iformat(
  211. xfs_inode_t *ip,
  212. xfs_dinode_t *dip)
  213. {
  214. xfs_attr_shortform_t *atp;
  215. int size;
  216. int error = 0;
  217. xfs_fsize_t di_size;
  218. if (unlikely(be32_to_cpu(dip->di_nextents) +
  219. be16_to_cpu(dip->di_anextents) >
  220. be64_to_cpu(dip->di_nblocks))) {
  221. xfs_warn(ip->i_mount,
  222. "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.",
  223. (unsigned long long)ip->i_ino,
  224. (int)(be32_to_cpu(dip->di_nextents) +
  225. be16_to_cpu(dip->di_anextents)),
  226. (unsigned long long)
  227. be64_to_cpu(dip->di_nblocks));
  228. XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW,
  229. ip->i_mount, dip);
  230. return XFS_ERROR(EFSCORRUPTED);
  231. }
  232. if (unlikely(dip->di_forkoff > ip->i_mount->m_sb.sb_inodesize)) {
  233. xfs_warn(ip->i_mount, "corrupt dinode %Lu, forkoff = 0x%x.",
  234. (unsigned long long)ip->i_ino,
  235. dip->di_forkoff);
  236. XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW,
  237. ip->i_mount, dip);
  238. return XFS_ERROR(EFSCORRUPTED);
  239. }
  240. if (unlikely((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) &&
  241. !ip->i_mount->m_rtdev_targp)) {
  242. xfs_warn(ip->i_mount,
  243. "corrupt dinode %Lu, has realtime flag set.",
  244. ip->i_ino);
  245. XFS_CORRUPTION_ERROR("xfs_iformat(realtime)",
  246. XFS_ERRLEVEL_LOW, ip->i_mount, dip);
  247. return XFS_ERROR(EFSCORRUPTED);
  248. }
  249. switch (ip->i_d.di_mode & S_IFMT) {
  250. case S_IFIFO:
  251. case S_IFCHR:
  252. case S_IFBLK:
  253. case S_IFSOCK:
  254. if (unlikely(dip->di_format != XFS_DINODE_FMT_DEV)) {
  255. XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW,
  256. ip->i_mount, dip);
  257. return XFS_ERROR(EFSCORRUPTED);
  258. }
  259. ip->i_d.di_size = 0;
  260. ip->i_df.if_u2.if_rdev = xfs_dinode_get_rdev(dip);
  261. break;
  262. case S_IFREG:
  263. case S_IFLNK:
  264. case S_IFDIR:
  265. switch (dip->di_format) {
  266. case XFS_DINODE_FMT_LOCAL:
  267. /*
  268. * no local regular files yet
  269. */
  270. if (unlikely(S_ISREG(be16_to_cpu(dip->di_mode)))) {
  271. xfs_warn(ip->i_mount,
  272. "corrupt inode %Lu (local format for regular file).",
  273. (unsigned long long) ip->i_ino);
  274. XFS_CORRUPTION_ERROR("xfs_iformat(4)",
  275. XFS_ERRLEVEL_LOW,
  276. ip->i_mount, dip);
  277. return XFS_ERROR(EFSCORRUPTED);
  278. }
  279. di_size = be64_to_cpu(dip->di_size);
  280. if (unlikely(di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) {
  281. xfs_warn(ip->i_mount,
  282. "corrupt inode %Lu (bad size %Ld for local inode).",
  283. (unsigned long long) ip->i_ino,
  284. (long long) di_size);
  285. XFS_CORRUPTION_ERROR("xfs_iformat(5)",
  286. XFS_ERRLEVEL_LOW,
  287. ip->i_mount, dip);
  288. return XFS_ERROR(EFSCORRUPTED);
  289. }
  290. size = (int)di_size;
  291. error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
  292. break;
  293. case XFS_DINODE_FMT_EXTENTS:
  294. error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
  295. break;
  296. case XFS_DINODE_FMT_BTREE:
  297. error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
  298. break;
  299. default:
  300. XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW,
  301. ip->i_mount);
  302. return XFS_ERROR(EFSCORRUPTED);
  303. }
  304. break;
  305. default:
  306. XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount);
  307. return XFS_ERROR(EFSCORRUPTED);
  308. }
  309. if (error) {
  310. return error;
  311. }
  312. if (!XFS_DFORK_Q(dip))
  313. return 0;
  314. ASSERT(ip->i_afp == NULL);
  315. ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP | KM_NOFS);
  316. switch (dip->di_aformat) {
  317. case XFS_DINODE_FMT_LOCAL:
  318. atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
  319. size = be16_to_cpu(atp->hdr.totsize);
  320. if (unlikely(size < sizeof(struct xfs_attr_sf_hdr))) {
  321. xfs_warn(ip->i_mount,
  322. "corrupt inode %Lu (bad attr fork size %Ld).",
  323. (unsigned long long) ip->i_ino,
  324. (long long) size);
  325. XFS_CORRUPTION_ERROR("xfs_iformat(8)",
  326. XFS_ERRLEVEL_LOW,
  327. ip->i_mount, dip);
  328. return XFS_ERROR(EFSCORRUPTED);
  329. }
  330. error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
  331. break;
  332. case XFS_DINODE_FMT_EXTENTS:
  333. error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
  334. break;
  335. case XFS_DINODE_FMT_BTREE:
  336. error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
  337. break;
  338. default:
  339. error = XFS_ERROR(EFSCORRUPTED);
  340. break;
  341. }
  342. if (error) {
  343. kmem_zone_free(xfs_ifork_zone, ip->i_afp);
  344. ip->i_afp = NULL;
  345. xfs_idestroy_fork(ip, XFS_DATA_FORK);
  346. }
  347. return error;
  348. }
  349. /*
  350. * The file is in-lined in the on-disk inode.
  351. * If it fits into if_inline_data, then copy
  352. * it there, otherwise allocate a buffer for it
  353. * and copy the data there. Either way, set
  354. * if_data to point at the data.
  355. * If we allocate a buffer for the data, make
  356. * sure that its size is a multiple of 4 and
  357. * record the real size in i_real_bytes.
  358. */
  359. STATIC int
  360. xfs_iformat_local(
  361. xfs_inode_t *ip,
  362. xfs_dinode_t *dip,
  363. int whichfork,
  364. int size)
  365. {
  366. xfs_ifork_t *ifp;
  367. int real_size;
  368. /*
  369. * If the size is unreasonable, then something
  370. * is wrong and we just bail out rather than crash in
  371. * kmem_alloc() or memcpy() below.
  372. */
  373. if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
  374. xfs_warn(ip->i_mount,
  375. "corrupt inode %Lu (bad size %d for local fork, size = %d).",
  376. (unsigned long long) ip->i_ino, size,
  377. XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
  378. XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW,
  379. ip->i_mount, dip);
  380. return XFS_ERROR(EFSCORRUPTED);
  381. }
  382. ifp = XFS_IFORK_PTR(ip, whichfork);
  383. real_size = 0;
  384. if (size == 0)
  385. ifp->if_u1.if_data = NULL;
  386. else if (size <= sizeof(ifp->if_u2.if_inline_data))
  387. ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
  388. else {
  389. real_size = roundup(size, 4);
  390. ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP | KM_NOFS);
  391. }
  392. ifp->if_bytes = size;
  393. ifp->if_real_bytes = real_size;
  394. if (size)
  395. memcpy(ifp->if_u1.if_data, XFS_DFORK_PTR(dip, whichfork), size);
  396. ifp->if_flags &= ~XFS_IFEXTENTS;
  397. ifp->if_flags |= XFS_IFINLINE;
  398. return 0;
  399. }
  400. /*
  401. * The file consists of a set of extents all
  402. * of which fit into the on-disk inode.
  403. * If there are few enough extents to fit into
  404. * the if_inline_ext, then copy them there.
  405. * Otherwise allocate a buffer for them and copy
  406. * them into it. Either way, set if_extents
  407. * to point at the extents.
  408. */
  409. STATIC int
  410. xfs_iformat_extents(
  411. xfs_inode_t *ip,
  412. xfs_dinode_t *dip,
  413. int whichfork)
  414. {
  415. xfs_bmbt_rec_t *dp;
  416. xfs_ifork_t *ifp;
  417. int nex;
  418. int size;
  419. int i;
  420. ifp = XFS_IFORK_PTR(ip, whichfork);
  421. nex = XFS_DFORK_NEXTENTS(dip, whichfork);
  422. size = nex * (uint)sizeof(xfs_bmbt_rec_t);
  423. /*
  424. * If the number of extents is unreasonable, then something
  425. * is wrong and we just bail out rather than crash in
  426. * kmem_alloc() or memcpy() below.
  427. */
  428. if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
  429. xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
  430. (unsigned long long) ip->i_ino, nex);
  431. XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW,
  432. ip->i_mount, dip);
  433. return XFS_ERROR(EFSCORRUPTED);
  434. }
  435. ifp->if_real_bytes = 0;
  436. if (nex == 0)
  437. ifp->if_u1.if_extents = NULL;
  438. else if (nex <= XFS_INLINE_EXTS)
  439. ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
  440. else
  441. xfs_iext_add(ifp, 0, nex);
  442. ifp->if_bytes = size;
  443. if (size) {
  444. dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
  445. xfs_validate_extents(ifp, nex, XFS_EXTFMT_INODE(ip));
  446. for (i = 0; i < nex; i++, dp++) {
  447. xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
  448. ep->l0 = get_unaligned_be64(&dp->l0);
  449. ep->l1 = get_unaligned_be64(&dp->l1);
  450. }
  451. XFS_BMAP_TRACE_EXLIST(ip, nex, whichfork);
  452. if (whichfork != XFS_DATA_FORK ||
  453. XFS_EXTFMT_INODE(ip) == XFS_EXTFMT_NOSTATE)
  454. if (unlikely(xfs_check_nostate_extents(
  455. ifp, 0, nex))) {
  456. XFS_ERROR_REPORT("xfs_iformat_extents(2)",
  457. XFS_ERRLEVEL_LOW,
  458. ip->i_mount);
  459. return XFS_ERROR(EFSCORRUPTED);
  460. }
  461. }
  462. ifp->if_flags |= XFS_IFEXTENTS;
  463. return 0;
  464. }
  465. /*
  466. * The file has too many extents to fit into
  467. * the inode, so they are in B-tree format.
  468. * Allocate a buffer for the root of the B-tree
  469. * and copy the root into it. The i_extents
  470. * field will remain NULL until all of the
  471. * extents are read in (when they are needed).
  472. */
  473. STATIC int
  474. xfs_iformat_btree(
  475. xfs_inode_t *ip,
  476. xfs_dinode_t *dip,
  477. int whichfork)
  478. {
  479. xfs_bmdr_block_t *dfp;
  480. xfs_ifork_t *ifp;
  481. /* REFERENCED */
  482. int nrecs;
  483. int size;
  484. ifp = XFS_IFORK_PTR(ip, whichfork);
  485. dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
  486. size = XFS_BMAP_BROOT_SPACE(dfp);
  487. nrecs = be16_to_cpu(dfp->bb_numrecs);
  488. /*
  489. * blow out if -- fork has less extents than can fit in
  490. * fork (fork shouldn't be a btree format), root btree
  491. * block has more records than can fit into the fork,
  492. * or the number of extents is greater than the number of
  493. * blocks.
  494. */
  495. if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <=
  496. XFS_IFORK_MAXEXT(ip, whichfork) ||
  497. XFS_BMDR_SPACE_CALC(nrecs) >
  498. XFS_DFORK_SIZE(dip, ip->i_mount, whichfork) ||
  499. XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks)) {
  500. xfs_warn(ip->i_mount, "corrupt inode %Lu (btree).",
  501. (unsigned long long) ip->i_ino);
  502. XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW,
  503. ip->i_mount, dip);
  504. return XFS_ERROR(EFSCORRUPTED);
  505. }
  506. ifp->if_broot_bytes = size;
  507. ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS);
  508. ASSERT(ifp->if_broot != NULL);
  509. /*
  510. * Copy and convert from the on-disk structure
  511. * to the in-memory structure.
  512. */
  513. xfs_bmdr_to_bmbt(ip->i_mount, dfp,
  514. XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
  515. ifp->if_broot, size);
  516. ifp->if_flags &= ~XFS_IFEXTENTS;
  517. ifp->if_flags |= XFS_IFBROOT;
  518. return 0;
  519. }
  520. STATIC void
  521. xfs_dinode_from_disk(
  522. xfs_icdinode_t *to,
  523. xfs_dinode_t *from)
  524. {
  525. to->di_magic = be16_to_cpu(from->di_magic);
  526. to->di_mode = be16_to_cpu(from->di_mode);
  527. to->di_version = from ->di_version;
  528. to->di_format = from->di_format;
  529. to->di_onlink = be16_to_cpu(from->di_onlink);
  530. to->di_uid = be32_to_cpu(from->di_uid);
  531. to->di_gid = be32_to_cpu(from->di_gid);
  532. to->di_nlink = be32_to_cpu(from->di_nlink);
  533. to->di_projid_lo = be16_to_cpu(from->di_projid_lo);
  534. to->di_projid_hi = be16_to_cpu(from->di_projid_hi);
  535. memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
  536. to->di_flushiter = be16_to_cpu(from->di_flushiter);
  537. to->di_atime.t_sec = be32_to_cpu(from->di_atime.t_sec);
  538. to->di_atime.t_nsec = be32_to_cpu(from->di_atime.t_nsec);
  539. to->di_mtime.t_sec = be32_to_cpu(from->di_mtime.t_sec);
  540. to->di_mtime.t_nsec = be32_to_cpu(from->di_mtime.t_nsec);
  541. to->di_ctime.t_sec = be32_to_cpu(from->di_ctime.t_sec);
  542. to->di_ctime.t_nsec = be32_to_cpu(from->di_ctime.t_nsec);
  543. to->di_size = be64_to_cpu(from->di_size);
  544. to->di_nblocks = be64_to_cpu(from->di_nblocks);
  545. to->di_extsize = be32_to_cpu(from->di_extsize);
  546. to->di_nextents = be32_to_cpu(from->di_nextents);
  547. to->di_anextents = be16_to_cpu(from->di_anextents);
  548. to->di_forkoff = from->di_forkoff;
  549. to->di_aformat = from->di_aformat;
  550. to->di_dmevmask = be32_to_cpu(from->di_dmevmask);
  551. to->di_dmstate = be16_to_cpu(from->di_dmstate);
  552. to->di_flags = be16_to_cpu(from->di_flags);
  553. to->di_gen = be32_to_cpu(from->di_gen);
  554. }
  555. void
  556. xfs_dinode_to_disk(
  557. xfs_dinode_t *to,
  558. xfs_icdinode_t *from)
  559. {
  560. to->di_magic = cpu_to_be16(from->di_magic);
  561. to->di_mode = cpu_to_be16(from->di_mode);
  562. to->di_version = from ->di_version;
  563. to->di_format = from->di_format;
  564. to->di_onlink = cpu_to_be16(from->di_onlink);
  565. to->di_uid = cpu_to_be32(from->di_uid);
  566. to->di_gid = cpu_to_be32(from->di_gid);
  567. to->di_nlink = cpu_to_be32(from->di_nlink);
  568. to->di_projid_lo = cpu_to_be16(from->di_projid_lo);
  569. to->di_projid_hi = cpu_to_be16(from->di_projid_hi);
  570. memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
  571. to->di_flushiter = cpu_to_be16(from->di_flushiter);
  572. to->di_atime.t_sec = cpu_to_be32(from->di_atime.t_sec);
  573. to->di_atime.t_nsec = cpu_to_be32(from->di_atime.t_nsec);
  574. to->di_mtime.t_sec = cpu_to_be32(from->di_mtime.t_sec);
  575. to->di_mtime.t_nsec = cpu_to_be32(from->di_mtime.t_nsec);
  576. to->di_ctime.t_sec = cpu_to_be32(from->di_ctime.t_sec);
  577. to->di_ctime.t_nsec = cpu_to_be32(from->di_ctime.t_nsec);
  578. to->di_size = cpu_to_be64(from->di_size);
  579. to->di_nblocks = cpu_to_be64(from->di_nblocks);
  580. to->di_extsize = cpu_to_be32(from->di_extsize);
  581. to->di_nextents = cpu_to_be32(from->di_nextents);
  582. to->di_anextents = cpu_to_be16(from->di_anextents);
  583. to->di_forkoff = from->di_forkoff;
  584. to->di_aformat = from->di_aformat;
  585. to->di_dmevmask = cpu_to_be32(from->di_dmevmask);
  586. to->di_dmstate = cpu_to_be16(from->di_dmstate);
  587. to->di_flags = cpu_to_be16(from->di_flags);
  588. to->di_gen = cpu_to_be32(from->di_gen);
  589. }
  590. STATIC uint
  591. _xfs_dic2xflags(
  592. __uint16_t di_flags)
  593. {
  594. uint flags = 0;
  595. if (di_flags & XFS_DIFLAG_ANY) {
  596. if (di_flags & XFS_DIFLAG_REALTIME)
  597. flags |= XFS_XFLAG_REALTIME;
  598. if (di_flags & XFS_DIFLAG_PREALLOC)
  599. flags |= XFS_XFLAG_PREALLOC;
  600. if (di_flags & XFS_DIFLAG_IMMUTABLE)
  601. flags |= XFS_XFLAG_IMMUTABLE;
  602. if (di_flags & XFS_DIFLAG_APPEND)
  603. flags |= XFS_XFLAG_APPEND;
  604. if (di_flags & XFS_DIFLAG_SYNC)
  605. flags |= XFS_XFLAG_SYNC;
  606. if (di_flags & XFS_DIFLAG_NOATIME)
  607. flags |= XFS_XFLAG_NOATIME;
  608. if (di_flags & XFS_DIFLAG_NODUMP)
  609. flags |= XFS_XFLAG_NODUMP;
  610. if (di_flags & XFS_DIFLAG_RTINHERIT)
  611. flags |= XFS_XFLAG_RTINHERIT;
  612. if (di_flags & XFS_DIFLAG_PROJINHERIT)
  613. flags |= XFS_XFLAG_PROJINHERIT;
  614. if (di_flags & XFS_DIFLAG_NOSYMLINKS)
  615. flags |= XFS_XFLAG_NOSYMLINKS;
  616. if (di_flags & XFS_DIFLAG_EXTSIZE)
  617. flags |= XFS_XFLAG_EXTSIZE;
  618. if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
  619. flags |= XFS_XFLAG_EXTSZINHERIT;
  620. if (di_flags & XFS_DIFLAG_NODEFRAG)
  621. flags |= XFS_XFLAG_NODEFRAG;
  622. if (di_flags & XFS_DIFLAG_FILESTREAM)
  623. flags |= XFS_XFLAG_FILESTREAM;
  624. }
  625. return flags;
  626. }
  627. uint
  628. xfs_ip2xflags(
  629. xfs_inode_t *ip)
  630. {
  631. xfs_icdinode_t *dic = &ip->i_d;
  632. return _xfs_dic2xflags(dic->di_flags) |
  633. (XFS_IFORK_Q(ip) ? XFS_XFLAG_HASATTR : 0);
  634. }
  635. uint
  636. xfs_dic2xflags(
  637. xfs_dinode_t *dip)
  638. {
  639. return _xfs_dic2xflags(be16_to_cpu(dip->di_flags)) |
  640. (XFS_DFORK_Q(dip) ? XFS_XFLAG_HASATTR : 0);
  641. }
  642. /*
  643. * Read the disk inode attributes into the in-core inode structure.
  644. */
  645. int
  646. xfs_iread(
  647. xfs_mount_t *mp,
  648. xfs_trans_t *tp,
  649. xfs_inode_t *ip,
  650. uint iget_flags)
  651. {
  652. xfs_buf_t *bp;
  653. xfs_dinode_t *dip;
  654. int error;
  655. /*
  656. * Fill in the location information in the in-core inode.
  657. */
  658. error = xfs_imap(mp, tp, ip->i_ino, &ip->i_imap, iget_flags);
  659. if (error)
  660. return error;
  661. /*
  662. * Get pointers to the on-disk inode and the buffer containing it.
  663. */
  664. error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &bp, 0, iget_flags);
  665. if (error)
  666. return error;
  667. /*
  668. * If we got something that isn't an inode it means someone
  669. * (nfs or dmi) has a stale handle.
  670. */
  671. if (dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC)) {
  672. #ifdef DEBUG
  673. xfs_alert(mp,
  674. "%s: dip->di_magic (0x%x) != XFS_DINODE_MAGIC (0x%x)",
  675. __func__, be16_to_cpu(dip->di_magic), XFS_DINODE_MAGIC);
  676. #endif /* DEBUG */
  677. error = XFS_ERROR(EINVAL);
  678. goto out_brelse;
  679. }
  680. /*
  681. * If the on-disk inode is already linked to a directory
  682. * entry, copy all of the inode into the in-core inode.
  683. * xfs_iformat() handles copying in the inode format
  684. * specific information.
  685. * Otherwise, just get the truly permanent information.
  686. */
  687. if (dip->di_mode) {
  688. xfs_dinode_from_disk(&ip->i_d, dip);
  689. error = xfs_iformat(ip, dip);
  690. if (error) {
  691. #ifdef DEBUG
  692. xfs_alert(mp, "%s: xfs_iformat() returned error %d",
  693. __func__, error);
  694. #endif /* DEBUG */
  695. goto out_brelse;
  696. }
  697. } else {
  698. ip->i_d.di_magic = be16_to_cpu(dip->di_magic);
  699. ip->i_d.di_version = dip->di_version;
  700. ip->i_d.di_gen = be32_to_cpu(dip->di_gen);
  701. ip->i_d.di_flushiter = be16_to_cpu(dip->di_flushiter);
  702. /*
  703. * Make sure to pull in the mode here as well in
  704. * case the inode is released without being used.
  705. * This ensures that xfs_inactive() will see that
  706. * the inode is already free and not try to mess
  707. * with the uninitialized part of it.
  708. */
  709. ip->i_d.di_mode = 0;
  710. }
  711. /*
  712. * The inode format changed when we moved the link count and
  713. * made it 32 bits long. If this is an old format inode,
  714. * convert it in memory to look like a new one. If it gets
  715. * flushed to disk we will convert back before flushing or
  716. * logging it. We zero out the new projid field and the old link
  717. * count field. We'll handle clearing the pad field (the remains
  718. * of the old uuid field) when we actually convert the inode to
  719. * the new format. We don't change the version number so that we
  720. * can distinguish this from a real new format inode.
  721. */
  722. if (ip->i_d.di_version == 1) {
  723. ip->i_d.di_nlink = ip->i_d.di_onlink;
  724. ip->i_d.di_onlink = 0;
  725. xfs_set_projid(ip, 0);
  726. }
  727. ip->i_delayed_blks = 0;
  728. /*
  729. * Mark the buffer containing the inode as something to keep
  730. * around for a while. This helps to keep recently accessed
  731. * meta-data in-core longer.
  732. */
  733. xfs_buf_set_ref(bp, XFS_INO_REF);
  734. /*
  735. * Use xfs_trans_brelse() to release the buffer containing the
  736. * on-disk inode, because it was acquired with xfs_trans_read_buf()
  737. * in xfs_imap_to_bp() above. If tp is NULL, this is just a normal
  738. * brelse(). If we're within a transaction, then xfs_trans_brelse()
  739. * will only release the buffer if it is not dirty within the
  740. * transaction. It will be OK to release the buffer in this case,
  741. * because inodes on disk are never destroyed and we will be
  742. * locking the new in-core inode before putting it in the hash
  743. * table where other processes can find it. Thus we don't have
  744. * to worry about the inode being changed just because we released
  745. * the buffer.
  746. */
  747. out_brelse:
  748. xfs_trans_brelse(tp, bp);
  749. return error;
  750. }
  751. /*
  752. * Read in extents from a btree-format inode.
  753. * Allocate and fill in if_extents. Real work is done in xfs_bmap.c.
  754. */
  755. int
  756. xfs_iread_extents(
  757. xfs_trans_t *tp,
  758. xfs_inode_t *ip,
  759. int whichfork)
  760. {
  761. int error;
  762. xfs_ifork_t *ifp;
  763. xfs_extnum_t nextents;
  764. if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
  765. XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW,
  766. ip->i_mount);
  767. return XFS_ERROR(EFSCORRUPTED);
  768. }
  769. nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
  770. ifp = XFS_IFORK_PTR(ip, whichfork);
  771. /*
  772. * We know that the size is valid (it's checked in iformat_btree)
  773. */
  774. ifp->if_bytes = ifp->if_real_bytes = 0;
  775. ifp->if_flags |= XFS_IFEXTENTS;
  776. xfs_iext_add(ifp, 0, nextents);
  777. error = xfs_bmap_read_extents(tp, ip, whichfork);
  778. if (error) {
  779. xfs_iext_destroy(ifp);
  780. ifp->if_flags &= ~XFS_IFEXTENTS;
  781. return error;
  782. }
  783. xfs_validate_extents(ifp, nextents, XFS_EXTFMT_INODE(ip));
  784. return 0;
  785. }
  786. /*
  787. * Allocate an inode on disk and return a copy of its in-core version.
  788. * The in-core inode is locked exclusively. Set mode, nlink, and rdev
  789. * appropriately within the inode. The uid and gid for the inode are
  790. * set according to the contents of the given cred structure.
  791. *
  792. * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
  793. * has a free inode available, call xfs_iget()
  794. * to obtain the in-core version of the allocated inode. Finally,
  795. * fill in the inode and log its initial contents. In this case,
  796. * ialloc_context would be set to NULL and call_again set to false.
  797. *
  798. * If xfs_dialloc() does not have an available inode,
  799. * it will replenish its supply by doing an allocation. Since we can
  800. * only do one allocation within a transaction without deadlocks, we
  801. * must commit the current transaction before returning the inode itself.
  802. * In this case, therefore, we will set call_again to true and return.
  803. * The caller should then commit the current transaction, start a new
  804. * transaction, and call xfs_ialloc() again to actually get the inode.
  805. *
  806. * To ensure that some other process does not grab the inode that
  807. * was allocated during the first call to xfs_ialloc(), this routine
  808. * also returns the [locked] bp pointing to the head of the freelist
  809. * as ialloc_context. The caller should hold this buffer across
  810. * the commit and pass it back into this routine on the second call.
  811. *
  812. * If we are allocating quota inodes, we do not have a parent inode
  813. * to attach to or associate with (i.e. pip == NULL) because they
  814. * are not linked into the directory structure - they are attached
  815. * directly to the superblock - and so have no parent.
  816. */
  817. int
  818. xfs_ialloc(
  819. xfs_trans_t *tp,
  820. xfs_inode_t *pip,
  821. umode_t mode,
  822. xfs_nlink_t nlink,
  823. xfs_dev_t rdev,
  824. prid_t prid,
  825. int okalloc,
  826. xfs_buf_t **ialloc_context,
  827. xfs_inode_t **ipp)
  828. {
  829. xfs_ino_t ino;
  830. xfs_inode_t *ip;
  831. uint flags;
  832. int error;
  833. timespec_t tv;
  834. int filestreams = 0;
  835. /*
  836. * Call the space management code to pick
  837. * the on-disk inode to be allocated.
  838. */
  839. error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc,
  840. ialloc_context, &ino);
  841. if (error)
  842. return error;
  843. if (*ialloc_context || ino == NULLFSINO) {
  844. *ipp = NULL;
  845. return 0;
  846. }
  847. ASSERT(*ialloc_context == NULL);
  848. /*
  849. * Get the in-core inode with the lock held exclusively.
  850. * This is because we're setting fields here we need
  851. * to prevent others from looking at until we're done.
  852. */
  853. error = xfs_iget(tp->t_mountp, tp, ino, XFS_IGET_CREATE,
  854. XFS_ILOCK_EXCL, &ip);
  855. if (error)
  856. return error;
  857. ASSERT(ip != NULL);
  858. ip->i_d.di_mode = mode;
  859. ip->i_d.di_onlink = 0;
  860. ip->i_d.di_nlink = nlink;
  861. ASSERT(ip->i_d.di_nlink == nlink);
  862. ip->i_d.di_uid = current_fsuid();
  863. ip->i_d.di_gid = current_fsgid();
  864. xfs_set_projid(ip, prid);
  865. memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
  866. /*
  867. * If the superblock version is up to where we support new format
  868. * inodes and this is currently an old format inode, then change
  869. * the inode version number now. This way we only do the conversion
  870. * here rather than here and in the flush/logging code.
  871. */
  872. if (xfs_sb_version_hasnlink(&tp->t_mountp->m_sb) &&
  873. ip->i_d.di_version == 1) {
  874. ip->i_d.di_version = 2;
  875. /*
  876. * We've already zeroed the old link count, the projid field,
  877. * and the pad field.
  878. */
  879. }
  880. /*
  881. * Project ids won't be stored on disk if we are using a version 1 inode.
  882. */
  883. if ((prid != 0) && (ip->i_d.di_version == 1))
  884. xfs_bump_ino_vers2(tp, ip);
  885. if (pip && XFS_INHERIT_GID(pip)) {
  886. ip->i_d.di_gid = pip->i_d.di_gid;
  887. if ((pip->i_d.di_mode & S_ISGID) && S_ISDIR(mode)) {
  888. ip->i_d.di_mode |= S_ISGID;
  889. }
  890. }
  891. /*
  892. * If the group ID of the new file does not match the effective group
  893. * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
  894. * (and only if the irix_sgid_inherit compatibility variable is set).
  895. */
  896. if ((irix_sgid_inherit) &&
  897. (ip->i_d.di_mode & S_ISGID) &&
  898. (!in_group_p((gid_t)ip->i_d.di_gid))) {
  899. ip->i_d.di_mode &= ~S_ISGID;
  900. }
  901. ip->i_d.di_size = 0;
  902. ip->i_d.di_nextents = 0;
  903. ASSERT(ip->i_d.di_nblocks == 0);
  904. nanotime(&tv);
  905. ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
  906. ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
  907. ip->i_d.di_atime = ip->i_d.di_mtime;
  908. ip->i_d.di_ctime = ip->i_d.di_mtime;
  909. /*
  910. * di_gen will have been taken care of in xfs_iread.
  911. */
  912. ip->i_d.di_extsize = 0;
  913. ip->i_d.di_dmevmask = 0;
  914. ip->i_d.di_dmstate = 0;
  915. ip->i_d.di_flags = 0;
  916. flags = XFS_ILOG_CORE;
  917. switch (mode & S_IFMT) {
  918. case S_IFIFO:
  919. case S_IFCHR:
  920. case S_IFBLK:
  921. case S_IFSOCK:
  922. ip->i_d.di_format = XFS_DINODE_FMT_DEV;
  923. ip->i_df.if_u2.if_rdev = rdev;
  924. ip->i_df.if_flags = 0;
  925. flags |= XFS_ILOG_DEV;
  926. break;
  927. case S_IFREG:
  928. /*
  929. * we can't set up filestreams until after the VFS inode
  930. * is set up properly.
  931. */
  932. if (pip && xfs_inode_is_filestream(pip))
  933. filestreams = 1;
  934. /* fall through */
  935. case S_IFDIR:
  936. if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
  937. uint di_flags = 0;
  938. if (S_ISDIR(mode)) {
  939. if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
  940. di_flags |= XFS_DIFLAG_RTINHERIT;
  941. if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
  942. di_flags |= XFS_DIFLAG_EXTSZINHERIT;
  943. ip->i_d.di_extsize = pip->i_d.di_extsize;
  944. }
  945. } else if (S_ISREG(mode)) {
  946. if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
  947. di_flags |= XFS_DIFLAG_REALTIME;
  948. if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
  949. di_flags |= XFS_DIFLAG_EXTSIZE;
  950. ip->i_d.di_extsize = pip->i_d.di_extsize;
  951. }
  952. }
  953. if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
  954. xfs_inherit_noatime)
  955. di_flags |= XFS_DIFLAG_NOATIME;
  956. if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
  957. xfs_inherit_nodump)
  958. di_flags |= XFS_DIFLAG_NODUMP;
  959. if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
  960. xfs_inherit_sync)
  961. di_flags |= XFS_DIFLAG_SYNC;
  962. if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
  963. xfs_inherit_nosymlinks)
  964. di_flags |= XFS_DIFLAG_NOSYMLINKS;
  965. if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  966. di_flags |= XFS_DIFLAG_PROJINHERIT;
  967. if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
  968. xfs_inherit_nodefrag)
  969. di_flags |= XFS_DIFLAG_NODEFRAG;
  970. if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
  971. di_flags |= XFS_DIFLAG_FILESTREAM;
  972. ip->i_d.di_flags |= di_flags;
  973. }
  974. /* FALLTHROUGH */
  975. case S_IFLNK:
  976. ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
  977. ip->i_df.if_flags = XFS_IFEXTENTS;
  978. ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
  979. ip->i_df.if_u1.if_extents = NULL;
  980. break;
  981. default:
  982. ASSERT(0);
  983. }
  984. /*
  985. * Attribute fork settings for new inode.
  986. */
  987. ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
  988. ip->i_d.di_anextents = 0;
  989. /*
  990. * Log the new values stuffed into the inode.
  991. */
  992. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  993. xfs_trans_log_inode(tp, ip, flags);
  994. /* now that we have an i_mode we can setup inode ops and unlock */
  995. xfs_setup_inode(ip);
  996. /* now we have set up the vfs inode we can associate the filestream */
  997. if (filestreams) {
  998. error = xfs_filestream_associate(pip, ip);
  999. if (error < 0)
  1000. return -error;
  1001. if (!error)
  1002. xfs_iflags_set(ip, XFS_IFILESTREAM);
  1003. }
  1004. *ipp = ip;
  1005. return 0;
  1006. }
  1007. /*
  1008. * Free up the underlying blocks past new_size. The new size must be smaller
  1009. * than the current size. This routine can be used both for the attribute and
  1010. * data fork, and does not modify the inode size, which is left to the caller.
  1011. *
  1012. * The transaction passed to this routine must have made a permanent log
  1013. * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the
  1014. * given transaction and start new ones, so make sure everything involved in
  1015. * the transaction is tidy before calling here. Some transaction will be
  1016. * returned to the caller to be committed. The incoming transaction must
  1017. * already include the inode, and both inode locks must be held exclusively.
  1018. * The inode must also be "held" within the transaction. On return the inode
  1019. * will be "held" within the returned transaction. This routine does NOT
  1020. * require any disk space to be reserved for it within the transaction.
  1021. *
  1022. * If we get an error, we must return with the inode locked and linked into the
  1023. * current transaction. This keeps things simple for the higher level code,
  1024. * because it always knows that the inode is locked and held in the transaction
  1025. * that returns to it whether errors occur or not. We don't mark the inode
  1026. * dirty on error so that transactions can be easily aborted if possible.
  1027. */
  1028. int
  1029. xfs_itruncate_extents(
  1030. struct xfs_trans **tpp,
  1031. struct xfs_inode *ip,
  1032. int whichfork,
  1033. xfs_fsize_t new_size)
  1034. {
  1035. struct xfs_mount *mp = ip->i_mount;
  1036. struct xfs_trans *tp = *tpp;
  1037. struct xfs_trans *ntp;
  1038. xfs_bmap_free_t free_list;
  1039. xfs_fsblock_t first_block;
  1040. xfs_fileoff_t first_unmap_block;
  1041. xfs_fileoff_t last_block;
  1042. xfs_filblks_t unmap_len;
  1043. int committed;
  1044. int error = 0;
  1045. int done = 0;
  1046. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  1047. ASSERT(!atomic_read(&VFS_I(ip)->i_count) ||
  1048. xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  1049. ASSERT(new_size <= XFS_ISIZE(ip));
  1050. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  1051. ASSERT(ip->i_itemp != NULL);
  1052. ASSERT(ip->i_itemp->ili_lock_flags == 0);
  1053. ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
  1054. trace_xfs_itruncate_extents_start(ip, new_size);
  1055. /*
  1056. * Since it is possible for space to become allocated beyond
  1057. * the end of the file (in a crash where the space is allocated
  1058. * but the inode size is not yet updated), simply remove any
  1059. * blocks which show up between the new EOF and the maximum
  1060. * possible file size. If the first block to be removed is
  1061. * beyond the maximum file size (ie it is the same as last_block),
  1062. * then there is nothing to do.
  1063. */
  1064. first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
  1065. last_block = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
  1066. if (first_unmap_block == last_block)
  1067. return 0;
  1068. ASSERT(first_unmap_block < last_block);
  1069. unmap_len = last_block - first_unmap_block + 1;
  1070. while (!done) {
  1071. xfs_bmap_init(&free_list, &first_block);
  1072. error = xfs_bunmapi(tp, ip,
  1073. first_unmap_block, unmap_len,
  1074. xfs_bmapi_aflag(whichfork),
  1075. XFS_ITRUNC_MAX_EXTENTS,
  1076. &first_block, &free_list,
  1077. &done);
  1078. if (error)
  1079. goto out_bmap_cancel;
  1080. /*
  1081. * Duplicate the transaction that has the permanent
  1082. * reservation and commit the old transaction.
  1083. */
  1084. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1085. if (committed)
  1086. xfs_trans_ijoin(tp, ip, 0);
  1087. if (error)
  1088. goto out_bmap_cancel;
  1089. if (committed) {
  1090. /*
  1091. * Mark the inode dirty so it will be logged and
  1092. * moved forward in the log as part of every commit.
  1093. */
  1094. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1095. }
  1096. ntp = xfs_trans_dup(tp);
  1097. error = xfs_trans_commit(tp, 0);
  1098. tp = ntp;
  1099. xfs_trans_ijoin(tp, ip, 0);
  1100. if (error)
  1101. goto out;
  1102. /*
  1103. * Transaction commit worked ok so we can drop the extra ticket
  1104. * reference that we gained in xfs_trans_dup()
  1105. */
  1106. xfs_log_ticket_put(tp->t_ticket);
  1107. error = xfs_trans_reserve(tp, 0,
  1108. XFS_ITRUNCATE_LOG_RES(mp), 0,
  1109. XFS_TRANS_PERM_LOG_RES,
  1110. XFS_ITRUNCATE_LOG_COUNT);
  1111. if (error)
  1112. goto out;
  1113. }
  1114. /*
  1115. * Always re-log the inode so that our permanent transaction can keep
  1116. * on rolling it forward in the log.
  1117. */
  1118. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1119. trace_xfs_itruncate_extents_end(ip, new_size);
  1120. out:
  1121. *tpp = tp;
  1122. return error;
  1123. out_bmap_cancel:
  1124. /*
  1125. * If the bunmapi call encounters an error, return to the caller where
  1126. * the transaction can be properly aborted. We just need to make sure
  1127. * we're not holding any resources that we were not when we came in.
  1128. */
  1129. xfs_bmap_cancel(&free_list);
  1130. goto out;
  1131. }
  1132. /*
  1133. * This is called when the inode's link count goes to 0.
  1134. * We place the on-disk inode on a list in the AGI. It
  1135. * will be pulled from this list when the inode is freed.
  1136. */
  1137. int
  1138. xfs_iunlink(
  1139. xfs_trans_t *tp,
  1140. xfs_inode_t *ip)
  1141. {
  1142. xfs_mount_t *mp;
  1143. xfs_agi_t *agi;
  1144. xfs_dinode_t *dip;
  1145. xfs_buf_t *agibp;
  1146. xfs_buf_t *ibp;
  1147. xfs_agino_t agino;
  1148. short bucket_index;
  1149. int offset;
  1150. int error;
  1151. ASSERT(ip->i_d.di_nlink == 0);
  1152. ASSERT(ip->i_d.di_mode != 0);
  1153. mp = tp->t_mountp;
  1154. /*
  1155. * Get the agi buffer first. It ensures lock ordering
  1156. * on the list.
  1157. */
  1158. error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp);
  1159. if (error)
  1160. return error;
  1161. agi = XFS_BUF_TO_AGI(agibp);
  1162. /*
  1163. * Get the index into the agi hash table for the
  1164. * list this inode will go on.
  1165. */
  1166. agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
  1167. ASSERT(agino != 0);
  1168. bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
  1169. ASSERT(agi->agi_unlinked[bucket_index]);
  1170. ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
  1171. if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) {
  1172. /*
  1173. * There is already another inode in the bucket we need
  1174. * to add ourselves to. Add us at the front of the list.
  1175. * Here we put the head pointer into our next pointer,
  1176. * and then we fall through to point the head at us.
  1177. */
  1178. error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
  1179. 0, 0);
  1180. if (error)
  1181. return error;
  1182. ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO));
  1183. dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
  1184. offset = ip->i_imap.im_boffset +
  1185. offsetof(xfs_dinode_t, di_next_unlinked);
  1186. xfs_trans_inode_buf(tp, ibp);
  1187. xfs_trans_log_buf(tp, ibp, offset,
  1188. (offset + sizeof(xfs_agino_t) - 1));
  1189. xfs_inobp_check(mp, ibp);
  1190. }
  1191. /*
  1192. * Point the bucket head pointer at the inode being inserted.
  1193. */
  1194. ASSERT(agino != 0);
  1195. agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
  1196. offset = offsetof(xfs_agi_t, agi_unlinked) +
  1197. (sizeof(xfs_agino_t) * bucket_index);
  1198. xfs_trans_log_buf(tp, agibp, offset,
  1199. (offset + sizeof(xfs_agino_t) - 1));
  1200. return 0;
  1201. }
  1202. /*
  1203. * Pull the on-disk inode from the AGI unlinked list.
  1204. */
  1205. STATIC int
  1206. xfs_iunlink_remove(
  1207. xfs_trans_t *tp,
  1208. xfs_inode_t *ip)
  1209. {
  1210. xfs_ino_t next_ino;
  1211. xfs_mount_t *mp;
  1212. xfs_agi_t *agi;
  1213. xfs_dinode_t *dip;
  1214. xfs_buf_t *agibp;
  1215. xfs_buf_t *ibp;
  1216. xfs_agnumber_t agno;
  1217. xfs_agino_t agino;
  1218. xfs_agino_t next_agino;
  1219. xfs_buf_t *last_ibp;
  1220. xfs_dinode_t *last_dip = NULL;
  1221. short bucket_index;
  1222. int offset, last_offset = 0;
  1223. int error;
  1224. mp = tp->t_mountp;
  1225. agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
  1226. /*
  1227. * Get the agi buffer first. It ensures lock ordering
  1228. * on the list.
  1229. */
  1230. error = xfs_read_agi(mp, tp, agno, &agibp);
  1231. if (error)
  1232. return error;
  1233. agi = XFS_BUF_TO_AGI(agibp);
  1234. /*
  1235. * Get the index into the agi hash table for the
  1236. * list this inode will go on.
  1237. */
  1238. agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
  1239. ASSERT(agino != 0);
  1240. bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
  1241. ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO));
  1242. ASSERT(agi->agi_unlinked[bucket_index]);
  1243. if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
  1244. /*
  1245. * We're at the head of the list. Get the inode's on-disk
  1246. * buffer to see if there is anyone after us on the list.
  1247. * Only modify our next pointer if it is not already NULLAGINO.
  1248. * This saves us the overhead of dealing with the buffer when
  1249. * there is no need to change it.
  1250. */
  1251. error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
  1252. 0, 0);
  1253. if (error) {
  1254. xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
  1255. __func__, error);
  1256. return error;
  1257. }
  1258. next_agino = be32_to_cpu(dip->di_next_unlinked);
  1259. ASSERT(next_agino != 0);
  1260. if (next_agino != NULLAGINO) {
  1261. dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
  1262. offset = ip->i_imap.im_boffset +
  1263. offsetof(xfs_dinode_t, di_next_unlinked);
  1264. xfs_trans_inode_buf(tp, ibp);
  1265. xfs_trans_log_buf(tp, ibp, offset,
  1266. (offset + sizeof(xfs_agino_t) - 1));
  1267. xfs_inobp_check(mp, ibp);
  1268. } else {
  1269. xfs_trans_brelse(tp, ibp);
  1270. }
  1271. /*
  1272. * Point the bucket head pointer at the next inode.
  1273. */
  1274. ASSERT(next_agino != 0);
  1275. ASSERT(next_agino != agino);
  1276. agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
  1277. offset = offsetof(xfs_agi_t, agi_unlinked) +
  1278. (sizeof(xfs_agino_t) * bucket_index);
  1279. xfs_trans_log_buf(tp, agibp, offset,
  1280. (offset + sizeof(xfs_agino_t) - 1));
  1281. } else {
  1282. /*
  1283. * We need to search the list for the inode being freed.
  1284. */
  1285. next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
  1286. last_ibp = NULL;
  1287. while (next_agino != agino) {
  1288. struct xfs_imap imap;
  1289. if (last_ibp)
  1290. xfs_trans_brelse(tp, last_ibp);
  1291. imap.im_blkno = 0;
  1292. next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
  1293. error = xfs_imap(mp, tp, next_ino, &imap, 0);
  1294. if (error) {
  1295. xfs_warn(mp,
  1296. "%s: xfs_imap returned error %d.",
  1297. __func__, error);
  1298. return error;
  1299. }
  1300. error = xfs_imap_to_bp(mp, tp, &imap, &last_dip,
  1301. &last_ibp, 0, 0);
  1302. if (error) {
  1303. xfs_warn(mp,
  1304. "%s: xfs_imap_to_bp returned error %d.",
  1305. __func__, error);
  1306. return error;
  1307. }
  1308. last_offset = imap.im_boffset;
  1309. next_agino = be32_to_cpu(last_dip->di_next_unlinked);
  1310. ASSERT(next_agino != NULLAGINO);
  1311. ASSERT(next_agino != 0);
  1312. }
  1313. /*
  1314. * Now last_ibp points to the buffer previous to us on the
  1315. * unlinked list. Pull us from the list.
  1316. */
  1317. error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
  1318. 0, 0);
  1319. if (error) {
  1320. xfs_warn(mp, "%s: xfs_imap_to_bp(2) returned error %d.",
  1321. __func__, error);
  1322. return error;
  1323. }
  1324. next_agino = be32_to_cpu(dip->di_next_unlinked);
  1325. ASSERT(next_agino != 0);
  1326. ASSERT(next_agino != agino);
  1327. if (next_agino != NULLAGINO) {
  1328. dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
  1329. offset = ip->i_imap.im_boffset +
  1330. offsetof(xfs_dinode_t, di_next_unlinked);
  1331. xfs_trans_inode_buf(tp, ibp);
  1332. xfs_trans_log_buf(tp, ibp, offset,
  1333. (offset + sizeof(xfs_agino_t) - 1));
  1334. xfs_inobp_check(mp, ibp);
  1335. } else {
  1336. xfs_trans_brelse(tp, ibp);
  1337. }
  1338. /*
  1339. * Point the previous inode on the list to the next inode.
  1340. */
  1341. last_dip->di_next_unlinked = cpu_to_be32(next_agino);
  1342. ASSERT(next_agino != 0);
  1343. offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
  1344. xfs_trans_inode_buf(tp, last_ibp);
  1345. xfs_trans_log_buf(tp, last_ibp, offset,
  1346. (offset + sizeof(xfs_agino_t) - 1));
  1347. xfs_inobp_check(mp, last_ibp);
  1348. }
  1349. return 0;
  1350. }
  1351. /*
  1352. * A big issue when freeing the inode cluster is is that we _cannot_ skip any
  1353. * inodes that are in memory - they all must be marked stale and attached to
  1354. * the cluster buffer.
  1355. */
  1356. STATIC int
  1357. xfs_ifree_cluster(
  1358. xfs_inode_t *free_ip,
  1359. xfs_trans_t *tp,
  1360. xfs_ino_t inum)
  1361. {
  1362. xfs_mount_t *mp = free_ip->i_mount;
  1363. int blks_per_cluster;
  1364. int nbufs;
  1365. int ninodes;
  1366. int i, j;
  1367. xfs_daddr_t blkno;
  1368. xfs_buf_t *bp;
  1369. xfs_inode_t *ip;
  1370. xfs_inode_log_item_t *iip;
  1371. xfs_log_item_t *lip;
  1372. struct xfs_perag *pag;
  1373. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum));
  1374. if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
  1375. blks_per_cluster = 1;
  1376. ninodes = mp->m_sb.sb_inopblock;
  1377. nbufs = XFS_IALLOC_BLOCKS(mp);
  1378. } else {
  1379. blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
  1380. mp->m_sb.sb_blocksize;
  1381. ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
  1382. nbufs = XFS_IALLOC_BLOCKS(mp) / blks_per_cluster;
  1383. }
  1384. for (j = 0; j < nbufs; j++, inum += ninodes) {
  1385. blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
  1386. XFS_INO_TO_AGBNO(mp, inum));
  1387. /*
  1388. * We obtain and lock the backing buffer first in the process
  1389. * here, as we have to ensure that any dirty inode that we
  1390. * can't get the flush lock on is attached to the buffer.
  1391. * If we scan the in-memory inodes first, then buffer IO can
  1392. * complete before we get a lock on it, and hence we may fail
  1393. * to mark all the active inodes on the buffer stale.
  1394. */
  1395. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
  1396. mp->m_bsize * blks_per_cluster, 0);
  1397. if (!bp)
  1398. return ENOMEM;
  1399. /*
  1400. * Walk the inodes already attached to the buffer and mark them
  1401. * stale. These will all have the flush locks held, so an
  1402. * in-memory inode walk can't lock them. By marking them all
  1403. * stale first, we will not attempt to lock them in the loop
  1404. * below as the XFS_ISTALE flag will be set.
  1405. */
  1406. lip = bp->b_fspriv;
  1407. while (lip) {
  1408. if (lip->li_type == XFS_LI_INODE) {
  1409. iip = (xfs_inode_log_item_t *)lip;
  1410. ASSERT(iip->ili_logged == 1);
  1411. lip->li_cb = xfs_istale_done;
  1412. xfs_trans_ail_copy_lsn(mp->m_ail,
  1413. &iip->ili_flush_lsn,
  1414. &iip->ili_item.li_lsn);
  1415. xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
  1416. }
  1417. lip = lip->li_bio_list;
  1418. }
  1419. /*
  1420. * For each inode in memory attempt to add it to the inode
  1421. * buffer and set it up for being staled on buffer IO
  1422. * completion. This is safe as we've locked out tail pushing
  1423. * and flushing by locking the buffer.
  1424. *
  1425. * We have already marked every inode that was part of a
  1426. * transaction stale above, which means there is no point in
  1427. * even trying to lock them.
  1428. */
  1429. for (i = 0; i < ninodes; i++) {
  1430. retry:
  1431. rcu_read_lock();
  1432. ip = radix_tree_lookup(&pag->pag_ici_root,
  1433. XFS_INO_TO_AGINO(mp, (inum + i)));
  1434. /* Inode not in memory, nothing to do */
  1435. if (!ip) {
  1436. rcu_read_unlock();
  1437. continue;
  1438. }
  1439. /*
  1440. * because this is an RCU protected lookup, we could
  1441. * find a recently freed or even reallocated inode
  1442. * during the lookup. We need to check under the
  1443. * i_flags_lock for a valid inode here. Skip it if it
  1444. * is not valid, the wrong inode or stale.
  1445. */
  1446. spin_lock(&ip->i_flags_lock);
  1447. if (ip->i_ino != inum + i ||
  1448. __xfs_iflags_test(ip, XFS_ISTALE)) {
  1449. spin_unlock(&ip->i_flags_lock);
  1450. rcu_read_unlock();
  1451. continue;
  1452. }
  1453. spin_unlock(&ip->i_flags_lock);
  1454. /*
  1455. * Don't try to lock/unlock the current inode, but we
  1456. * _cannot_ skip the other inodes that we did not find
  1457. * in the list attached to the buffer and are not
  1458. * already marked stale. If we can't lock it, back off
  1459. * and retry.
  1460. */
  1461. if (ip != free_ip &&
  1462. !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
  1463. rcu_read_unlock();
  1464. delay(1);
  1465. goto retry;
  1466. }
  1467. rcu_read_unlock();
  1468. xfs_iflock(ip);
  1469. xfs_iflags_set(ip, XFS_ISTALE);
  1470. /*
  1471. * we don't need to attach clean inodes or those only
  1472. * with unlogged changes (which we throw away, anyway).
  1473. */
  1474. iip = ip->i_itemp;
  1475. if (!iip || xfs_inode_clean(ip)) {
  1476. ASSERT(ip != free_ip);
  1477. xfs_ifunlock(ip);
  1478. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1479. continue;
  1480. }
  1481. iip->ili_last_fields = iip->ili_fields;
  1482. iip->ili_fields = 0;
  1483. iip->ili_logged = 1;
  1484. xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
  1485. &iip->ili_item.li_lsn);
  1486. xfs_buf_attach_iodone(bp, xfs_istale_done,
  1487. &iip->ili_item);
  1488. if (ip != free_ip)
  1489. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1490. }
  1491. xfs_trans_stale_inode_buf(tp, bp);
  1492. xfs_trans_binval(tp, bp);
  1493. }
  1494. xfs_perag_put(pag);
  1495. return 0;
  1496. }
  1497. /*
  1498. * This is called to return an inode to the inode free list.
  1499. * The inode should already be truncated to 0 length and have
  1500. * no pages associated with it. This routine also assumes that
  1501. * the inode is already a part of the transaction.
  1502. *
  1503. * The on-disk copy of the inode will have been added to the list
  1504. * of unlinked inodes in the AGI. We need to remove the inode from
  1505. * that list atomically with respect to freeing it here.
  1506. */
  1507. int
  1508. xfs_ifree(
  1509. xfs_trans_t *tp,
  1510. xfs_inode_t *ip,
  1511. xfs_bmap_free_t *flist)
  1512. {
  1513. int error;
  1514. int delete;
  1515. xfs_ino_t first_ino;
  1516. xfs_dinode_t *dip;
  1517. xfs_buf_t *ibp;
  1518. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  1519. ASSERT(ip->i_d.di_nlink == 0);
  1520. ASSERT(ip->i_d.di_nextents == 0);
  1521. ASSERT(ip->i_d.di_anextents == 0);
  1522. ASSERT(ip->i_d.di_size == 0 || !S_ISREG(ip->i_d.di_mode));
  1523. ASSERT(ip->i_d.di_nblocks == 0);
  1524. /*
  1525. * Pull the on-disk inode from the AGI unlinked list.
  1526. */
  1527. error = xfs_iunlink_remove(tp, ip);
  1528. if (error != 0) {
  1529. return error;
  1530. }
  1531. error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino);
  1532. if (error != 0) {
  1533. return error;
  1534. }
  1535. ip->i_d.di_mode = 0; /* mark incore inode as free */
  1536. ip->i_d.di_flags = 0;
  1537. ip->i_d.di_dmevmask = 0;
  1538. ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */
  1539. ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
  1540. ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
  1541. /*
  1542. * Bump the generation count so no one will be confused
  1543. * by reincarnations of this inode.
  1544. */
  1545. ip->i_d.di_gen++;
  1546. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1547. error = xfs_imap_to_bp(ip->i_mount, tp, &ip->i_imap, &dip, &ibp,
  1548. 0, 0);
  1549. if (error)
  1550. return error;
  1551. /*
  1552. * Clear the on-disk di_mode. This is to prevent xfs_bulkstat
  1553. * from picking up this inode when it is reclaimed (its incore state
  1554. * initialzed but not flushed to disk yet). The in-core di_mode is
  1555. * already cleared and a corresponding transaction logged.
  1556. * The hack here just synchronizes the in-core to on-disk
  1557. * di_mode value in advance before the actual inode sync to disk.
  1558. * This is OK because the inode is already unlinked and would never
  1559. * change its di_mode again for this inode generation.
  1560. * This is a temporary hack that would require a proper fix
  1561. * in the future.
  1562. */
  1563. dip->di_mode = 0;
  1564. if (delete) {
  1565. error = xfs_ifree_cluster(ip, tp, first_ino);
  1566. }
  1567. return error;
  1568. }
  1569. /*
  1570. * Reallocate the space for if_broot based on the number of records
  1571. * being added or deleted as indicated in rec_diff. Move the records
  1572. * and pointers in if_broot to fit the new size. When shrinking this
  1573. * will eliminate holes between the records and pointers created by
  1574. * the caller. When growing this will create holes to be filled in
  1575. * by the caller.
  1576. *
  1577. * The caller must not request to add more records than would fit in
  1578. * the on-disk inode root. If the if_broot is currently NULL, then
  1579. * if we adding records one will be allocated. The caller must also
  1580. * not request that the number of records go below zero, although
  1581. * it can go to zero.
  1582. *
  1583. * ip -- the inode whose if_broot area is changing
  1584. * ext_diff -- the change in the number of records, positive or negative,
  1585. * requested for the if_broot array.
  1586. */
  1587. void
  1588. xfs_iroot_realloc(
  1589. xfs_inode_t *ip,
  1590. int rec_diff,
  1591. int whichfork)
  1592. {
  1593. struct xfs_mount *mp = ip->i_mount;
  1594. int cur_max;
  1595. xfs_ifork_t *ifp;
  1596. struct xfs_btree_block *new_broot;
  1597. int new_max;
  1598. size_t new_size;
  1599. char *np;
  1600. char *op;
  1601. /*
  1602. * Handle the degenerate case quietly.
  1603. */
  1604. if (rec_diff == 0) {
  1605. return;
  1606. }
  1607. ifp = XFS_IFORK_PTR(ip, whichfork);
  1608. if (rec_diff > 0) {
  1609. /*
  1610. * If there wasn't any memory allocated before, just
  1611. * allocate it now and get out.
  1612. */
  1613. if (ifp->if_broot_bytes == 0) {
  1614. new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(rec_diff);
  1615. ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
  1616. ifp->if_broot_bytes = (int)new_size;
  1617. return;
  1618. }
  1619. /*
  1620. * If there is already an existing if_broot, then we need
  1621. * to realloc() it and shift the pointers to their new
  1622. * location. The records don't change location because
  1623. * they are kept butted up against the btree block header.
  1624. */
  1625. cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
  1626. new_max = cur_max + rec_diff;
  1627. new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max);
  1628. ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
  1629. (size_t)XFS_BMAP_BROOT_SPACE_CALC(cur_max), /* old size */
  1630. KM_SLEEP | KM_NOFS);
  1631. op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
  1632. ifp->if_broot_bytes);
  1633. np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
  1634. (int)new_size);
  1635. ifp->if_broot_bytes = (int)new_size;
  1636. ASSERT(ifp->if_broot_bytes <=
  1637. XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ);
  1638. memmove(np, op, cur_max * (uint)sizeof(xfs_dfsbno_t));
  1639. return;
  1640. }
  1641. /*
  1642. * rec_diff is less than 0. In this case, we are shrinking the
  1643. * if_broot buffer. It must already exist. If we go to zero
  1644. * records, just get rid of the root and clear the status bit.
  1645. */
  1646. ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
  1647. cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
  1648. new_max = cur_max + rec_diff;
  1649. ASSERT(new_max >= 0);
  1650. if (new_max > 0)
  1651. new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max);
  1652. else
  1653. new_size = 0;
  1654. if (new_size > 0) {
  1655. new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
  1656. /*
  1657. * First copy over the btree block header.
  1658. */
  1659. memcpy(new_broot, ifp->if_broot, XFS_BTREE_LBLOCK_LEN);
  1660. } else {
  1661. new_broot = NULL;
  1662. ifp->if_flags &= ~XFS_IFBROOT;
  1663. }
  1664. /*
  1665. * Only copy the records and pointers if there are any.
  1666. */
  1667. if (new_max > 0) {
  1668. /*
  1669. * First copy the records.
  1670. */
  1671. op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
  1672. np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
  1673. memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
  1674. /*
  1675. * Then copy the pointers.
  1676. */
  1677. op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
  1678. ifp->if_broot_bytes);
  1679. np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
  1680. (int)new_size);
  1681. memcpy(np, op, new_max * (uint)sizeof(xfs_dfsbno_t));
  1682. }
  1683. kmem_free(ifp->if_broot);
  1684. ifp->if_broot = new_broot;
  1685. ifp->if_broot_bytes = (int)new_size;
  1686. ASSERT(ifp->if_broot_bytes <=
  1687. XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ);
  1688. return;
  1689. }
  1690. /*
  1691. * This is called when the amount of space needed for if_data
  1692. * is increased or decreased. The change in size is indicated by
  1693. * the number of bytes that need to be added or deleted in the
  1694. * byte_diff parameter.
  1695. *
  1696. * If the amount of space needed has decreased below the size of the
  1697. * inline buffer, then switch to using the inline buffer. Otherwise,
  1698. * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
  1699. * to what is needed.
  1700. *
  1701. * ip -- the inode whose if_data area is changing
  1702. * byte_diff -- the change in the number of bytes, positive or negative,
  1703. * requested for the if_data array.
  1704. */
  1705. void
  1706. xfs_idata_realloc(
  1707. xfs_inode_t *ip,
  1708. int byte_diff,
  1709. int whichfork)
  1710. {
  1711. xfs_ifork_t *ifp;
  1712. int new_size;
  1713. int real_size;
  1714. if (byte_diff == 0) {
  1715. return;
  1716. }
  1717. ifp = XFS_IFORK_PTR(ip, whichfork);
  1718. new_size = (int)ifp->if_bytes + byte_diff;
  1719. ASSERT(new_size >= 0);
  1720. if (new_size == 0) {
  1721. if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
  1722. kmem_free(ifp->if_u1.if_data);
  1723. }
  1724. ifp->if_u1.if_data = NULL;
  1725. real_size = 0;
  1726. } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) {
  1727. /*
  1728. * If the valid extents/data can fit in if_inline_ext/data,
  1729. * copy them from the malloc'd vector and free it.
  1730. */
  1731. if (ifp->if_u1.if_data == NULL) {
  1732. ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
  1733. } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
  1734. ASSERT(ifp->if_real_bytes != 0);
  1735. memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data,
  1736. new_size);
  1737. kmem_free(ifp->if_u1.if_data);
  1738. ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
  1739. }
  1740. real_size = 0;
  1741. } else {
  1742. /*
  1743. * Stuck with malloc/realloc.
  1744. * For inline data, the underlying buffer must be
  1745. * a multiple of 4 bytes in size so that it can be
  1746. * logged and stay on word boundaries. We enforce
  1747. * that here.
  1748. */
  1749. real_size = roundup(new_size, 4);
  1750. if (ifp->if_u1.if_data == NULL) {
  1751. ASSERT(ifp->if_real_bytes == 0);
  1752. ifp->if_u1.if_data = kmem_alloc(real_size,
  1753. KM_SLEEP | KM_NOFS);
  1754. } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
  1755. /*
  1756. * Only do the realloc if the underlying size
  1757. * is really changing.
  1758. */
  1759. if (ifp->if_real_bytes != real_size) {
  1760. ifp->if_u1.if_data =
  1761. kmem_realloc(ifp->if_u1.if_data,
  1762. real_size,
  1763. ifp->if_real_bytes,
  1764. KM_SLEEP | KM_NOFS);
  1765. }
  1766. } else {
  1767. ASSERT(ifp->if_real_bytes == 0);
  1768. ifp->if_u1.if_data = kmem_alloc(real_size,
  1769. KM_SLEEP | KM_NOFS);
  1770. memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data,
  1771. ifp->if_bytes);
  1772. }
  1773. }
  1774. ifp->if_real_bytes = real_size;
  1775. ifp->if_bytes = new_size;
  1776. ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
  1777. }
  1778. void
  1779. xfs_idestroy_fork(
  1780. xfs_inode_t *ip,
  1781. int whichfork)
  1782. {
  1783. xfs_ifork_t *ifp;
  1784. ifp = XFS_IFORK_PTR(ip, whichfork);
  1785. if (ifp->if_broot != NULL) {
  1786. kmem_free(ifp->if_broot);
  1787. ifp->if_broot = NULL;
  1788. }
  1789. /*
  1790. * If the format is local, then we can't have an extents
  1791. * array so just look for an inline data array. If we're
  1792. * not local then we may or may not have an extents list,
  1793. * so check and free it up if we do.
  1794. */
  1795. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
  1796. if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) &&
  1797. (ifp->if_u1.if_data != NULL)) {
  1798. ASSERT(ifp->if_real_bytes != 0);
  1799. kmem_free(ifp->if_u1.if_data);
  1800. ifp->if_u1.if_data = NULL;
  1801. ifp->if_real_bytes = 0;
  1802. }
  1803. } else if ((ifp->if_flags & XFS_IFEXTENTS) &&
  1804. ((ifp->if_flags & XFS_IFEXTIREC) ||
  1805. ((ifp->if_u1.if_extents != NULL) &&
  1806. (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) {
  1807. ASSERT(ifp->if_real_bytes != 0);
  1808. xfs_iext_destroy(ifp);
  1809. }
  1810. ASSERT(ifp->if_u1.if_extents == NULL ||
  1811. ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext);
  1812. ASSERT(ifp->if_real_bytes == 0);
  1813. if (whichfork == XFS_ATTR_FORK) {
  1814. kmem_zone_free(xfs_ifork_zone, ip->i_afp);
  1815. ip->i_afp = NULL;
  1816. }
  1817. }
  1818. /*
  1819. * This is called to unpin an inode. The caller must have the inode locked
  1820. * in at least shared mode so that the buffer cannot be subsequently pinned
  1821. * once someone is waiting for it to be unpinned.
  1822. */
  1823. static void
  1824. xfs_iunpin(
  1825. struct xfs_inode *ip)
  1826. {
  1827. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  1828. trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
  1829. /* Give the log a push to start the unpinning I/O */
  1830. xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0);
  1831. }
  1832. static void
  1833. __xfs_iunpin_wait(
  1834. struct xfs_inode *ip)
  1835. {
  1836. wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
  1837. DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
  1838. xfs_iunpin(ip);
  1839. do {
  1840. prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
  1841. if (xfs_ipincount(ip))
  1842. io_schedule();
  1843. } while (xfs_ipincount(ip));
  1844. finish_wait(wq, &wait.wait);
  1845. }
  1846. void
  1847. xfs_iunpin_wait(
  1848. struct xfs_inode *ip)
  1849. {
  1850. if (xfs_ipincount(ip))
  1851. __xfs_iunpin_wait(ip);
  1852. }
  1853. /*
  1854. * xfs_iextents_copy()
  1855. *
  1856. * This is called to copy the REAL extents (as opposed to the delayed
  1857. * allocation extents) from the inode into the given buffer. It
  1858. * returns the number of bytes copied into the buffer.
  1859. *
  1860. * If there are no delayed allocation extents, then we can just
  1861. * memcpy() the extents into the buffer. Otherwise, we need to
  1862. * examine each extent in turn and skip those which are delayed.
  1863. */
  1864. int
  1865. xfs_iextents_copy(
  1866. xfs_inode_t *ip,
  1867. xfs_bmbt_rec_t *dp,
  1868. int whichfork)
  1869. {
  1870. int copied;
  1871. int i;
  1872. xfs_ifork_t *ifp;
  1873. int nrecs;
  1874. xfs_fsblock_t start_block;
  1875. ifp = XFS_IFORK_PTR(ip, whichfork);
  1876. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  1877. ASSERT(ifp->if_bytes > 0);
  1878. nrecs = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  1879. XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork);
  1880. ASSERT(nrecs > 0);
  1881. /*
  1882. * There are some delayed allocation extents in the
  1883. * inode, so copy the extents one at a time and skip
  1884. * the delayed ones. There must be at least one
  1885. * non-delayed extent.
  1886. */
  1887. copied = 0;
  1888. for (i = 0; i < nrecs; i++) {
  1889. xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
  1890. start_block = xfs_bmbt_get_startblock(ep);
  1891. if (isnullstartblock(start_block)) {
  1892. /*
  1893. * It's a delayed allocation extent, so skip it.
  1894. */
  1895. continue;
  1896. }
  1897. /* Translate to on disk format */
  1898. put_unaligned(cpu_to_be64(ep->l0), &dp->l0);
  1899. put_unaligned(cpu_to_be64(ep->l1), &dp->l1);
  1900. dp++;
  1901. copied++;
  1902. }
  1903. ASSERT(copied != 0);
  1904. xfs_validate_extents(ifp, copied, XFS_EXTFMT_INODE(ip));
  1905. return (copied * (uint)sizeof(xfs_bmbt_rec_t));
  1906. }
  1907. /*
  1908. * Each of the following cases stores data into the same region
  1909. * of the on-disk inode, so only one of them can be valid at
  1910. * any given time. While it is possible to have conflicting formats
  1911. * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
  1912. * in EXTENTS format, this can only happen when the fork has
  1913. * changed formats after being modified but before being flushed.
  1914. * In these cases, the format always takes precedence, because the
  1915. * format indicates the current state of the fork.
  1916. */
  1917. /*ARGSUSED*/
  1918. STATIC void
  1919. xfs_iflush_fork(
  1920. xfs_inode_t *ip,
  1921. xfs_dinode_t *dip,
  1922. xfs_inode_log_item_t *iip,
  1923. int whichfork,
  1924. xfs_buf_t *bp)
  1925. {
  1926. char *cp;
  1927. xfs_ifork_t *ifp;
  1928. xfs_mount_t *mp;
  1929. #ifdef XFS_TRANS_DEBUG
  1930. int first;
  1931. #endif
  1932. static const short brootflag[2] =
  1933. { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
  1934. static const short dataflag[2] =
  1935. { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
  1936. static const short extflag[2] =
  1937. { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
  1938. if (!iip)
  1939. return;
  1940. ifp = XFS_IFORK_PTR(ip, whichfork);
  1941. /*
  1942. * This can happen if we gave up in iformat in an error path,
  1943. * for the attribute fork.
  1944. */
  1945. if (!ifp) {
  1946. ASSERT(whichfork == XFS_ATTR_FORK);
  1947. return;
  1948. }
  1949. cp = XFS_DFORK_PTR(dip, whichfork);
  1950. mp = ip->i_mount;
  1951. switch (XFS_IFORK_FORMAT(ip, whichfork)) {
  1952. case XFS_DINODE_FMT_LOCAL:
  1953. if ((iip->ili_fields & dataflag[whichfork]) &&
  1954. (ifp->if_bytes > 0)) {
  1955. ASSERT(ifp->if_u1.if_data != NULL);
  1956. ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
  1957. memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
  1958. }
  1959. break;
  1960. case XFS_DINODE_FMT_EXTENTS:
  1961. ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
  1962. !(iip->ili_fields & extflag[whichfork]));
  1963. if ((iip->ili_fields & extflag[whichfork]) &&
  1964. (ifp->if_bytes > 0)) {
  1965. ASSERT(xfs_iext_get_ext(ifp, 0));
  1966. ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
  1967. (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
  1968. whichfork);
  1969. }
  1970. break;
  1971. case XFS_DINODE_FMT_BTREE:
  1972. if ((iip->ili_fields & brootflag[whichfork]) &&
  1973. (ifp->if_broot_bytes > 0)) {
  1974. ASSERT(ifp->if_broot != NULL);
  1975. ASSERT(ifp->if_broot_bytes <=
  1976. (XFS_IFORK_SIZE(ip, whichfork) +
  1977. XFS_BROOT_SIZE_ADJ));
  1978. xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
  1979. (xfs_bmdr_block_t *)cp,
  1980. XFS_DFORK_SIZE(dip, mp, whichfork));
  1981. }
  1982. break;
  1983. case XFS_DINODE_FMT_DEV:
  1984. if (iip->ili_fields & XFS_ILOG_DEV) {
  1985. ASSERT(whichfork == XFS_DATA_FORK);
  1986. xfs_dinode_put_rdev(dip, ip->i_df.if_u2.if_rdev);
  1987. }
  1988. break;
  1989. case XFS_DINODE_FMT_UUID:
  1990. if (iip->ili_fields & XFS_ILOG_UUID) {
  1991. ASSERT(whichfork == XFS_DATA_FORK);
  1992. memcpy(XFS_DFORK_DPTR(dip),
  1993. &ip->i_df.if_u2.if_uuid,
  1994. sizeof(uuid_t));
  1995. }
  1996. break;
  1997. default:
  1998. ASSERT(0);
  1999. break;
  2000. }
  2001. }
  2002. STATIC int
  2003. xfs_iflush_cluster(
  2004. xfs_inode_t *ip,
  2005. xfs_buf_t *bp)
  2006. {
  2007. xfs_mount_t *mp = ip->i_mount;
  2008. struct xfs_perag *pag;
  2009. unsigned long first_index, mask;
  2010. unsigned long inodes_per_cluster;
  2011. int ilist_size;
  2012. xfs_inode_t **ilist;
  2013. xfs_inode_t *iq;
  2014. int nr_found;
  2015. int clcount = 0;
  2016. int bufwasdelwri;
  2017. int i;
  2018. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
  2019. inodes_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog;
  2020. ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *);
  2021. ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS);
  2022. if (!ilist)
  2023. goto out_put;
  2024. mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
  2025. first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask;
  2026. rcu_read_lock();
  2027. /* really need a gang lookup range call here */
  2028. nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist,
  2029. first_index, inodes_per_cluster);
  2030. if (nr_found == 0)
  2031. goto out_free;
  2032. for (i = 0; i < nr_found; i++) {
  2033. iq = ilist[i];
  2034. if (iq == ip)
  2035. continue;
  2036. /*
  2037. * because this is an RCU protected lookup, we could find a
  2038. * recently freed or even reallocated inode during the lookup.
  2039. * We need to check under the i_flags_lock for a valid inode
  2040. * here. Skip it if it is not valid or the wrong inode.
  2041. */
  2042. spin_lock(&ip->i_flags_lock);
  2043. if (!ip->i_ino ||
  2044. (XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) {
  2045. spin_unlock(&ip->i_flags_lock);
  2046. continue;
  2047. }
  2048. spin_unlock(&ip->i_flags_lock);
  2049. /*
  2050. * Do an un-protected check to see if the inode is dirty and
  2051. * is a candidate for flushing. These checks will be repeated
  2052. * later after the appropriate locks are acquired.
  2053. */
  2054. if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0)
  2055. continue;
  2056. /*
  2057. * Try to get locks. If any are unavailable or it is pinned,
  2058. * then this inode cannot be flushed and is skipped.
  2059. */
  2060. if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED))
  2061. continue;
  2062. if (!xfs_iflock_nowait(iq)) {
  2063. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2064. continue;
  2065. }
  2066. if (xfs_ipincount(iq)) {
  2067. xfs_ifunlock(iq);
  2068. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2069. continue;
  2070. }
  2071. /*
  2072. * arriving here means that this inode can be flushed. First
  2073. * re-check that it's dirty before flushing.
  2074. */
  2075. if (!xfs_inode_clean(iq)) {
  2076. int error;
  2077. error = xfs_iflush_int(iq, bp);
  2078. if (error) {
  2079. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2080. goto cluster_corrupt_out;
  2081. }
  2082. clcount++;
  2083. } else {
  2084. xfs_ifunlock(iq);
  2085. }
  2086. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2087. }
  2088. if (clcount) {
  2089. XFS_STATS_INC(xs_icluster_flushcnt);
  2090. XFS_STATS_ADD(xs_icluster_flushinode, clcount);
  2091. }
  2092. out_free:
  2093. rcu_read_unlock();
  2094. kmem_free(ilist);
  2095. out_put:
  2096. xfs_perag_put(pag);
  2097. return 0;
  2098. cluster_corrupt_out:
  2099. /*
  2100. * Corruption detected in the clustering loop. Invalidate the
  2101. * inode buffer and shut down the filesystem.
  2102. */
  2103. rcu_read_unlock();
  2104. /*
  2105. * Clean up the buffer. If it was delwri, just release it --
  2106. * brelse can handle it with no problems. If not, shut down the
  2107. * filesystem before releasing the buffer.
  2108. */
  2109. bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q);
  2110. if (bufwasdelwri)
  2111. xfs_buf_relse(bp);
  2112. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  2113. if (!bufwasdelwri) {
  2114. /*
  2115. * Just like incore_relse: if we have b_iodone functions,
  2116. * mark the buffer as an error and call them. Otherwise
  2117. * mark it as stale and brelse.
  2118. */
  2119. if (bp->b_iodone) {
  2120. XFS_BUF_UNDONE(bp);
  2121. xfs_buf_stale(bp);
  2122. xfs_buf_ioerror(bp, EIO);
  2123. xfs_buf_ioend(bp, 0);
  2124. } else {
  2125. xfs_buf_stale(bp);
  2126. xfs_buf_relse(bp);
  2127. }
  2128. }
  2129. /*
  2130. * Unlocks the flush lock
  2131. */
  2132. xfs_iflush_abort(iq, false);
  2133. kmem_free(ilist);
  2134. xfs_perag_put(pag);
  2135. return XFS_ERROR(EFSCORRUPTED);
  2136. }
  2137. /*
  2138. * Flush dirty inode metadata into the backing buffer.
  2139. *
  2140. * The caller must have the inode lock and the inode flush lock held. The
  2141. * inode lock will still be held upon return to the caller, and the inode
  2142. * flush lock will be released after the inode has reached the disk.
  2143. *
  2144. * The caller must write out the buffer returned in *bpp and release it.
  2145. */
  2146. int
  2147. xfs_iflush(
  2148. struct xfs_inode *ip,
  2149. struct xfs_buf **bpp)
  2150. {
  2151. struct xfs_mount *mp = ip->i_mount;
  2152. struct xfs_buf *bp;
  2153. struct xfs_dinode *dip;
  2154. int error;
  2155. XFS_STATS_INC(xs_iflush_count);
  2156. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  2157. ASSERT(xfs_isiflocked(ip));
  2158. ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
  2159. ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
  2160. *bpp = NULL;
  2161. xfs_iunpin_wait(ip);
  2162. /*
  2163. * For stale inodes we cannot rely on the backing buffer remaining
  2164. * stale in cache for the remaining life of the stale inode and so
  2165. * xfs_imap_to_bp() below may give us a buffer that no longer contains
  2166. * inodes below. We have to check this after ensuring the inode is
  2167. * unpinned so that it is safe to reclaim the stale inode after the
  2168. * flush call.
  2169. */
  2170. if (xfs_iflags_test(ip, XFS_ISTALE)) {
  2171. xfs_ifunlock(ip);
  2172. return 0;
  2173. }
  2174. /*
  2175. * This may have been unpinned because the filesystem is shutting
  2176. * down forcibly. If that's the case we must not write this inode
  2177. * to disk, because the log record didn't make it to disk.
  2178. *
  2179. * We also have to remove the log item from the AIL in this case,
  2180. * as we wait for an empty AIL as part of the unmount process.
  2181. */
  2182. if (XFS_FORCED_SHUTDOWN(mp)) {
  2183. error = XFS_ERROR(EIO);
  2184. goto abort_out;
  2185. }
  2186. /*
  2187. * Get the buffer containing the on-disk inode.
  2188. */
  2189. error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, XBF_TRYLOCK,
  2190. 0);
  2191. if (error || !bp) {
  2192. xfs_ifunlock(ip);
  2193. return error;
  2194. }
  2195. /*
  2196. * First flush out the inode that xfs_iflush was called with.
  2197. */
  2198. error = xfs_iflush_int(ip, bp);
  2199. if (error)
  2200. goto corrupt_out;
  2201. /*
  2202. * If the buffer is pinned then push on the log now so we won't
  2203. * get stuck waiting in the write for too long.
  2204. */
  2205. if (xfs_buf_ispinned(bp))
  2206. xfs_log_force(mp, 0);
  2207. /*
  2208. * inode clustering:
  2209. * see if other inodes can be gathered into this write
  2210. */
  2211. error = xfs_iflush_cluster(ip, bp);
  2212. if (error)
  2213. goto cluster_corrupt_out;
  2214. *bpp = bp;
  2215. return 0;
  2216. corrupt_out:
  2217. xfs_buf_relse(bp);
  2218. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  2219. cluster_corrupt_out:
  2220. error = XFS_ERROR(EFSCORRUPTED);
  2221. abort_out:
  2222. /*
  2223. * Unlocks the flush lock
  2224. */
  2225. xfs_iflush_abort(ip, false);
  2226. return error;
  2227. }
  2228. STATIC int
  2229. xfs_iflush_int(
  2230. xfs_inode_t *ip,
  2231. xfs_buf_t *bp)
  2232. {
  2233. xfs_inode_log_item_t *iip;
  2234. xfs_dinode_t *dip;
  2235. xfs_mount_t *mp;
  2236. #ifdef XFS_TRANS_DEBUG
  2237. int first;
  2238. #endif
  2239. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  2240. ASSERT(xfs_isiflocked(ip));
  2241. ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
  2242. ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
  2243. iip = ip->i_itemp;
  2244. mp = ip->i_mount;
  2245. /* set *dip = inode's place in the buffer */
  2246. dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
  2247. if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
  2248. mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
  2249. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2250. "%s: Bad inode %Lu magic number 0x%x, ptr 0x%p",
  2251. __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
  2252. goto corrupt_out;
  2253. }
  2254. if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC,
  2255. mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) {
  2256. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2257. "%s: Bad inode %Lu, ptr 0x%p, magic number 0x%x",
  2258. __func__, ip->i_ino, ip, ip->i_d.di_magic);
  2259. goto corrupt_out;
  2260. }
  2261. if (S_ISREG(ip->i_d.di_mode)) {
  2262. if (XFS_TEST_ERROR(
  2263. (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
  2264. (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
  2265. mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
  2266. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2267. "%s: Bad regular inode %Lu, ptr 0x%p",
  2268. __func__, ip->i_ino, ip);
  2269. goto corrupt_out;
  2270. }
  2271. } else if (S_ISDIR(ip->i_d.di_mode)) {
  2272. if (XFS_TEST_ERROR(
  2273. (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
  2274. (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
  2275. (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
  2276. mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
  2277. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2278. "%s: Bad directory inode %Lu, ptr 0x%p",
  2279. __func__, ip->i_ino, ip);
  2280. goto corrupt_out;
  2281. }
  2282. }
  2283. if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
  2284. ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
  2285. XFS_RANDOM_IFLUSH_5)) {
  2286. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2287. "%s: detected corrupt incore inode %Lu, "
  2288. "total extents = %d, nblocks = %Ld, ptr 0x%p",
  2289. __func__, ip->i_ino,
  2290. ip->i_d.di_nextents + ip->i_d.di_anextents,
  2291. ip->i_d.di_nblocks, ip);
  2292. goto corrupt_out;
  2293. }
  2294. if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
  2295. mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
  2296. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2297. "%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
  2298. __func__, ip->i_ino, ip->i_d.di_forkoff, ip);
  2299. goto corrupt_out;
  2300. }
  2301. /*
  2302. * bump the flush iteration count, used to detect flushes which
  2303. * postdate a log record during recovery.
  2304. */
  2305. ip->i_d.di_flushiter++;
  2306. /*
  2307. * Copy the dirty parts of the inode into the on-disk
  2308. * inode. We always copy out the core of the inode,
  2309. * because if the inode is dirty at all the core must
  2310. * be.
  2311. */
  2312. xfs_dinode_to_disk(dip, &ip->i_d);
  2313. /* Wrap, we never let the log put out DI_MAX_FLUSH */
  2314. if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
  2315. ip->i_d.di_flushiter = 0;
  2316. /*
  2317. * If this is really an old format inode and the superblock version
  2318. * has not been updated to support only new format inodes, then
  2319. * convert back to the old inode format. If the superblock version
  2320. * has been updated, then make the conversion permanent.
  2321. */
  2322. ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb));
  2323. if (ip->i_d.di_version == 1) {
  2324. if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
  2325. /*
  2326. * Convert it back.
  2327. */
  2328. ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
  2329. dip->di_onlink = cpu_to_be16(ip->i_d.di_nlink);
  2330. } else {
  2331. /*
  2332. * The superblock version has already been bumped,
  2333. * so just make the conversion to the new inode
  2334. * format permanent.
  2335. */
  2336. ip->i_d.di_version = 2;
  2337. dip->di_version = 2;
  2338. ip->i_d.di_onlink = 0;
  2339. dip->di_onlink = 0;
  2340. memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
  2341. memset(&(dip->di_pad[0]), 0,
  2342. sizeof(dip->di_pad));
  2343. ASSERT(xfs_get_projid(ip) == 0);
  2344. }
  2345. }
  2346. xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK, bp);
  2347. if (XFS_IFORK_Q(ip))
  2348. xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK, bp);
  2349. xfs_inobp_check(mp, bp);
  2350. /*
  2351. * We've recorded everything logged in the inode, so we'd like to clear
  2352. * the ili_fields bits so we don't log and flush things unnecessarily.
  2353. * However, we can't stop logging all this information until the data
  2354. * we've copied into the disk buffer is written to disk. If we did we
  2355. * might overwrite the copy of the inode in the log with all the data
  2356. * after re-logging only part of it, and in the face of a crash we
  2357. * wouldn't have all the data we need to recover.
  2358. *
  2359. * What we do is move the bits to the ili_last_fields field. When
  2360. * logging the inode, these bits are moved back to the ili_fields field.
  2361. * In the xfs_iflush_done() routine we clear ili_last_fields, since we
  2362. * know that the information those bits represent is permanently on
  2363. * disk. As long as the flush completes before the inode is logged
  2364. * again, then both ili_fields and ili_last_fields will be cleared.
  2365. *
  2366. * We can play with the ili_fields bits here, because the inode lock
  2367. * must be held exclusively in order to set bits there and the flush
  2368. * lock protects the ili_last_fields bits. Set ili_logged so the flush
  2369. * done routine can tell whether or not to look in the AIL. Also, store
  2370. * the current LSN of the inode so that we can tell whether the item has
  2371. * moved in the AIL from xfs_iflush_done(). In order to read the lsn we
  2372. * need the AIL lock, because it is a 64 bit value that cannot be read
  2373. * atomically.
  2374. */
  2375. if (iip != NULL && iip->ili_fields != 0) {
  2376. iip->ili_last_fields = iip->ili_fields;
  2377. iip->ili_fields = 0;
  2378. iip->ili_logged = 1;
  2379. xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
  2380. &iip->ili_item.li_lsn);
  2381. /*
  2382. * Attach the function xfs_iflush_done to the inode's
  2383. * buffer. This will remove the inode from the AIL
  2384. * and unlock the inode's flush lock when the inode is
  2385. * completely written to disk.
  2386. */
  2387. xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item);
  2388. ASSERT(bp->b_fspriv != NULL);
  2389. ASSERT(bp->b_iodone != NULL);
  2390. } else {
  2391. /*
  2392. * We're flushing an inode which is not in the AIL and has
  2393. * not been logged. For this case we can immediately drop
  2394. * the inode flush lock because we can avoid the whole
  2395. * AIL state thing. It's OK to drop the flush lock now,
  2396. * because we've already locked the buffer and to do anything
  2397. * you really need both.
  2398. */
  2399. if (iip != NULL) {
  2400. ASSERT(iip->ili_logged == 0);
  2401. ASSERT(iip->ili_last_fields == 0);
  2402. ASSERT((iip->ili_item.li_flags & XFS_LI_IN_AIL) == 0);
  2403. }
  2404. xfs_ifunlock(ip);
  2405. }
  2406. return 0;
  2407. corrupt_out:
  2408. return XFS_ERROR(EFSCORRUPTED);
  2409. }
  2410. /*
  2411. * Return a pointer to the extent record at file index idx.
  2412. */
  2413. xfs_bmbt_rec_host_t *
  2414. xfs_iext_get_ext(
  2415. xfs_ifork_t *ifp, /* inode fork pointer */
  2416. xfs_extnum_t idx) /* index of target extent */
  2417. {
  2418. ASSERT(idx >= 0);
  2419. ASSERT(idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
  2420. if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) {
  2421. return ifp->if_u1.if_ext_irec->er_extbuf;
  2422. } else if (ifp->if_flags & XFS_IFEXTIREC) {
  2423. xfs_ext_irec_t *erp; /* irec pointer */
  2424. int erp_idx = 0; /* irec index */
  2425. xfs_extnum_t page_idx = idx; /* ext index in target list */
  2426. erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
  2427. return &erp->er_extbuf[page_idx];
  2428. } else if (ifp->if_bytes) {
  2429. return &ifp->if_u1.if_extents[idx];
  2430. } else {
  2431. return NULL;
  2432. }
  2433. }
  2434. /*
  2435. * Insert new item(s) into the extent records for incore inode
  2436. * fork 'ifp'. 'count' new items are inserted at index 'idx'.
  2437. */
  2438. void
  2439. xfs_iext_insert(
  2440. xfs_inode_t *ip, /* incore inode pointer */
  2441. xfs_extnum_t idx, /* starting index of new items */
  2442. xfs_extnum_t count, /* number of inserted items */
  2443. xfs_bmbt_irec_t *new, /* items to insert */
  2444. int state) /* type of extent conversion */
  2445. {
  2446. xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
  2447. xfs_extnum_t i; /* extent record index */
  2448. trace_xfs_iext_insert(ip, idx, new, state, _RET_IP_);
  2449. ASSERT(ifp->if_flags & XFS_IFEXTENTS);
  2450. xfs_iext_add(ifp, idx, count);
  2451. for (i = idx; i < idx + count; i++, new++)
  2452. xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new);
  2453. }
  2454. /*
  2455. * This is called when the amount of space required for incore file
  2456. * extents needs to be increased. The ext_diff parameter stores the
  2457. * number of new extents being added and the idx parameter contains
  2458. * the extent index where the new extents will be added. If the new
  2459. * extents are being appended, then we just need to (re)allocate and
  2460. * initialize the space. Otherwise, if the new extents are being
  2461. * inserted into the middle of the existing entries, a bit more work
  2462. * is required to make room for the new extents to be inserted. The
  2463. * caller is responsible for filling in the new extent entries upon
  2464. * return.
  2465. */
  2466. void
  2467. xfs_iext_add(
  2468. xfs_ifork_t *ifp, /* inode fork pointer */
  2469. xfs_extnum_t idx, /* index to begin adding exts */
  2470. int ext_diff) /* number of extents to add */
  2471. {
  2472. int byte_diff; /* new bytes being added */
  2473. int new_size; /* size of extents after adding */
  2474. xfs_extnum_t nextents; /* number of extents in file */
  2475. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  2476. ASSERT((idx >= 0) && (idx <= nextents));
  2477. byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t);
  2478. new_size = ifp->if_bytes + byte_diff;
  2479. /*
  2480. * If the new number of extents (nextents + ext_diff)
  2481. * fits inside the inode, then continue to use the inline
  2482. * extent buffer.
  2483. */
  2484. if (nextents + ext_diff <= XFS_INLINE_EXTS) {
  2485. if (idx < nextents) {
  2486. memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff],
  2487. &ifp->if_u2.if_inline_ext[idx],
  2488. (nextents - idx) * sizeof(xfs_bmbt_rec_t));
  2489. memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff);
  2490. }
  2491. ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
  2492. ifp->if_real_bytes = 0;
  2493. }
  2494. /*
  2495. * Otherwise use a linear (direct) extent list.
  2496. * If the extents are currently inside the inode,
  2497. * xfs_iext_realloc_direct will switch us from
  2498. * inline to direct extent allocation mode.
  2499. */
  2500. else if (nextents + ext_diff <= XFS_LINEAR_EXTS) {
  2501. xfs_iext_realloc_direct(ifp, new_size);
  2502. if (idx < nextents) {
  2503. memmove(&ifp->if_u1.if_extents[idx + ext_diff],
  2504. &ifp->if_u1.if_extents[idx],
  2505. (nextents - idx) * sizeof(xfs_bmbt_rec_t));
  2506. memset(&ifp->if_u1.if_extents[idx], 0, byte_diff);
  2507. }
  2508. }
  2509. /* Indirection array */
  2510. else {
  2511. xfs_ext_irec_t *erp;
  2512. int erp_idx = 0;
  2513. int page_idx = idx;
  2514. ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS);
  2515. if (ifp->if_flags & XFS_IFEXTIREC) {
  2516. erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1);
  2517. } else {
  2518. xfs_iext_irec_init(ifp);
  2519. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  2520. erp = ifp->if_u1.if_ext_irec;
  2521. }
  2522. /* Extents fit in target extent page */
  2523. if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) {
  2524. if (page_idx < erp->er_extcount) {
  2525. memmove(&erp->er_extbuf[page_idx + ext_diff],
  2526. &erp->er_extbuf[page_idx],
  2527. (erp->er_extcount - page_idx) *
  2528. sizeof(xfs_bmbt_rec_t));
  2529. memset(&erp->er_extbuf[page_idx], 0, byte_diff);
  2530. }
  2531. erp->er_extcount += ext_diff;
  2532. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
  2533. }
  2534. /* Insert a new extent page */
  2535. else if (erp) {
  2536. xfs_iext_add_indirect_multi(ifp,
  2537. erp_idx, page_idx, ext_diff);
  2538. }
  2539. /*
  2540. * If extent(s) are being appended to the last page in
  2541. * the indirection array and the new extent(s) don't fit
  2542. * in the page, then erp is NULL and erp_idx is set to
  2543. * the next index needed in the indirection array.
  2544. */
  2545. else {
  2546. int count = ext_diff;
  2547. while (count) {
  2548. erp = xfs_iext_irec_new(ifp, erp_idx);
  2549. erp->er_extcount = count;
  2550. count -= MIN(count, (int)XFS_LINEAR_EXTS);
  2551. if (count) {
  2552. erp_idx++;
  2553. }
  2554. }
  2555. }
  2556. }
  2557. ifp->if_bytes = new_size;
  2558. }
  2559. /*
  2560. * This is called when incore extents are being added to the indirection
  2561. * array and the new extents do not fit in the target extent list. The
  2562. * erp_idx parameter contains the irec index for the target extent list
  2563. * in the indirection array, and the idx parameter contains the extent
  2564. * index within the list. The number of extents being added is stored
  2565. * in the count parameter.
  2566. *
  2567. * |-------| |-------|
  2568. * | | | | idx - number of extents before idx
  2569. * | idx | | count |
  2570. * | | | | count - number of extents being inserted at idx
  2571. * |-------| |-------|
  2572. * | count | | nex2 | nex2 - number of extents after idx + count
  2573. * |-------| |-------|
  2574. */
  2575. void
  2576. xfs_iext_add_indirect_multi(
  2577. xfs_ifork_t *ifp, /* inode fork pointer */
  2578. int erp_idx, /* target extent irec index */
  2579. xfs_extnum_t idx, /* index within target list */
  2580. int count) /* new extents being added */
  2581. {
  2582. int byte_diff; /* new bytes being added */
  2583. xfs_ext_irec_t *erp; /* pointer to irec entry */
  2584. xfs_extnum_t ext_diff; /* number of extents to add */
  2585. xfs_extnum_t ext_cnt; /* new extents still needed */
  2586. xfs_extnum_t nex2; /* extents after idx + count */
  2587. xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */
  2588. int nlists; /* number of irec's (lists) */
  2589. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  2590. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  2591. nex2 = erp->er_extcount - idx;
  2592. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  2593. /*
  2594. * Save second part of target extent list
  2595. * (all extents past */
  2596. if (nex2) {
  2597. byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
  2598. nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_NOFS);
  2599. memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff);
  2600. erp->er_extcount -= nex2;
  2601. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2);
  2602. memset(&erp->er_extbuf[idx], 0, byte_diff);
  2603. }
  2604. /*
  2605. * Add the new extents to the end of the target
  2606. * list, then allocate new irec record(s) and
  2607. * extent buffer(s) as needed to store the rest
  2608. * of the new extents.
  2609. */
  2610. ext_cnt = count;
  2611. ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount);
  2612. if (ext_diff) {
  2613. erp->er_extcount += ext_diff;
  2614. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
  2615. ext_cnt -= ext_diff;
  2616. }
  2617. while (ext_cnt) {
  2618. erp_idx++;
  2619. erp = xfs_iext_irec_new(ifp, erp_idx);
  2620. ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS);
  2621. erp->er_extcount = ext_diff;
  2622. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
  2623. ext_cnt -= ext_diff;
  2624. }
  2625. /* Add nex2 extents back to indirection array */
  2626. if (nex2) {
  2627. xfs_extnum_t ext_avail;
  2628. int i;
  2629. byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
  2630. ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
  2631. i = 0;
  2632. /*
  2633. * If nex2 extents fit in the current page, append
  2634. * nex2_ep after the new extents.
  2635. */
  2636. if (nex2 <= ext_avail) {
  2637. i = erp->er_extcount;
  2638. }
  2639. /*
  2640. * Otherwise, check if space is available in the
  2641. * next page.
  2642. */
  2643. else if ((erp_idx < nlists - 1) &&
  2644. (nex2 <= (ext_avail = XFS_LINEAR_EXTS -
  2645. ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) {
  2646. erp_idx++;
  2647. erp++;
  2648. /* Create a hole for nex2 extents */
  2649. memmove(&erp->er_extbuf[nex2], erp->er_extbuf,
  2650. erp->er_extcount * sizeof(xfs_bmbt_rec_t));
  2651. }
  2652. /*
  2653. * Final choice, create a new extent page for
  2654. * nex2 extents.
  2655. */
  2656. else {
  2657. erp_idx++;
  2658. erp = xfs_iext_irec_new(ifp, erp_idx);
  2659. }
  2660. memmove(&erp->er_extbuf[i], nex2_ep, byte_diff);
  2661. kmem_free(nex2_ep);
  2662. erp->er_extcount += nex2;
  2663. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2);
  2664. }
  2665. }
  2666. /*
  2667. * This is called when the amount of space required for incore file
  2668. * extents needs to be decreased. The ext_diff parameter stores the
  2669. * number of extents to be removed and the idx parameter contains
  2670. * the extent index where the extents will be removed from.
  2671. *
  2672. * If the amount of space needed has decreased below the linear
  2673. * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
  2674. * extent array. Otherwise, use kmem_realloc() to adjust the
  2675. * size to what is needed.
  2676. */
  2677. void
  2678. xfs_iext_remove(
  2679. xfs_inode_t *ip, /* incore inode pointer */
  2680. xfs_extnum_t idx, /* index to begin removing exts */
  2681. int ext_diff, /* number of extents to remove */
  2682. int state) /* type of extent conversion */
  2683. {
  2684. xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
  2685. xfs_extnum_t nextents; /* number of extents in file */
  2686. int new_size; /* size of extents after removal */
  2687. trace_xfs_iext_remove(ip, idx, state, _RET_IP_);
  2688. ASSERT(ext_diff > 0);
  2689. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  2690. new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t);
  2691. if (new_size == 0) {
  2692. xfs_iext_destroy(ifp);
  2693. } else if (ifp->if_flags & XFS_IFEXTIREC) {
  2694. xfs_iext_remove_indirect(ifp, idx, ext_diff);
  2695. } else if (ifp->if_real_bytes) {
  2696. xfs_iext_remove_direct(ifp, idx, ext_diff);
  2697. } else {
  2698. xfs_iext_remove_inline(ifp, idx, ext_diff);
  2699. }
  2700. ifp->if_bytes = new_size;
  2701. }
  2702. /*
  2703. * This removes ext_diff extents from the inline buffer, beginning
  2704. * at extent index idx.
  2705. */
  2706. void
  2707. xfs_iext_remove_inline(
  2708. xfs_ifork_t *ifp, /* inode fork pointer */
  2709. xfs_extnum_t idx, /* index to begin removing exts */
  2710. int ext_diff) /* number of extents to remove */
  2711. {
  2712. int nextents; /* number of extents in file */
  2713. ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
  2714. ASSERT(idx < XFS_INLINE_EXTS);
  2715. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  2716. ASSERT(((nextents - ext_diff) > 0) &&
  2717. (nextents - ext_diff) < XFS_INLINE_EXTS);
  2718. if (idx + ext_diff < nextents) {
  2719. memmove(&ifp->if_u2.if_inline_ext[idx],
  2720. &ifp->if_u2.if_inline_ext[idx + ext_diff],
  2721. (nextents - (idx + ext_diff)) *
  2722. sizeof(xfs_bmbt_rec_t));
  2723. memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff],
  2724. 0, ext_diff * sizeof(xfs_bmbt_rec_t));
  2725. } else {
  2726. memset(&ifp->if_u2.if_inline_ext[idx], 0,
  2727. ext_diff * sizeof(xfs_bmbt_rec_t));
  2728. }
  2729. }
  2730. /*
  2731. * This removes ext_diff extents from a linear (direct) extent list,
  2732. * beginning at extent index idx. If the extents are being removed
  2733. * from the end of the list (ie. truncate) then we just need to re-
  2734. * allocate the list to remove the extra space. Otherwise, if the
  2735. * extents are being removed from the middle of the existing extent
  2736. * entries, then we first need to move the extent records beginning
  2737. * at idx + ext_diff up in the list to overwrite the records being
  2738. * removed, then remove the extra space via kmem_realloc.
  2739. */
  2740. void
  2741. xfs_iext_remove_direct(
  2742. xfs_ifork_t *ifp, /* inode fork pointer */
  2743. xfs_extnum_t idx, /* index to begin removing exts */
  2744. int ext_diff) /* number of extents to remove */
  2745. {
  2746. xfs_extnum_t nextents; /* number of extents in file */
  2747. int new_size; /* size of extents after removal */
  2748. ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
  2749. new_size = ifp->if_bytes -
  2750. (ext_diff * sizeof(xfs_bmbt_rec_t));
  2751. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  2752. if (new_size == 0) {
  2753. xfs_iext_destroy(ifp);
  2754. return;
  2755. }
  2756. /* Move extents up in the list (if needed) */
  2757. if (idx + ext_diff < nextents) {
  2758. memmove(&ifp->if_u1.if_extents[idx],
  2759. &ifp->if_u1.if_extents[idx + ext_diff],
  2760. (nextents - (idx + ext_diff)) *
  2761. sizeof(xfs_bmbt_rec_t));
  2762. }
  2763. memset(&ifp->if_u1.if_extents[nextents - ext_diff],
  2764. 0, ext_diff * sizeof(xfs_bmbt_rec_t));
  2765. /*
  2766. * Reallocate the direct extent list. If the extents
  2767. * will fit inside the inode then xfs_iext_realloc_direct
  2768. * will switch from direct to inline extent allocation
  2769. * mode for us.
  2770. */
  2771. xfs_iext_realloc_direct(ifp, new_size);
  2772. ifp->if_bytes = new_size;
  2773. }
  2774. /*
  2775. * This is called when incore extents are being removed from the
  2776. * indirection array and the extents being removed span multiple extent
  2777. * buffers. The idx parameter contains the file extent index where we
  2778. * want to begin removing extents, and the count parameter contains
  2779. * how many extents need to be removed.
  2780. *
  2781. * |-------| |-------|
  2782. * | nex1 | | | nex1 - number of extents before idx
  2783. * |-------| | count |
  2784. * | | | | count - number of extents being removed at idx
  2785. * | count | |-------|
  2786. * | | | nex2 | nex2 - number of extents after idx + count
  2787. * |-------| |-------|
  2788. */
  2789. void
  2790. xfs_iext_remove_indirect(
  2791. xfs_ifork_t *ifp, /* inode fork pointer */
  2792. xfs_extnum_t idx, /* index to begin removing extents */
  2793. int count) /* number of extents to remove */
  2794. {
  2795. xfs_ext_irec_t *erp; /* indirection array pointer */
  2796. int erp_idx = 0; /* indirection array index */
  2797. xfs_extnum_t ext_cnt; /* extents left to remove */
  2798. xfs_extnum_t ext_diff; /* extents to remove in current list */
  2799. xfs_extnum_t nex1; /* number of extents before idx */
  2800. xfs_extnum_t nex2; /* extents after idx + count */
  2801. int page_idx = idx; /* index in target extent list */
  2802. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  2803. erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
  2804. ASSERT(erp != NULL);
  2805. nex1 = page_idx;
  2806. ext_cnt = count;
  2807. while (ext_cnt) {
  2808. nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0);
  2809. ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1));
  2810. /*
  2811. * Check for deletion of entire list;
  2812. * xfs_iext_irec_remove() updates extent offsets.
  2813. */
  2814. if (ext_diff == erp->er_extcount) {
  2815. xfs_iext_irec_remove(ifp, erp_idx);
  2816. ext_cnt -= ext_diff;
  2817. nex1 = 0;
  2818. if (ext_cnt) {
  2819. ASSERT(erp_idx < ifp->if_real_bytes /
  2820. XFS_IEXT_BUFSZ);
  2821. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  2822. nex1 = 0;
  2823. continue;
  2824. } else {
  2825. break;
  2826. }
  2827. }
  2828. /* Move extents up (if needed) */
  2829. if (nex2) {
  2830. memmove(&erp->er_extbuf[nex1],
  2831. &erp->er_extbuf[nex1 + ext_diff],
  2832. nex2 * sizeof(xfs_bmbt_rec_t));
  2833. }
  2834. /* Zero out rest of page */
  2835. memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ -
  2836. ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t))));
  2837. /* Update remaining counters */
  2838. erp->er_extcount -= ext_diff;
  2839. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff);
  2840. ext_cnt -= ext_diff;
  2841. nex1 = 0;
  2842. erp_idx++;
  2843. erp++;
  2844. }
  2845. ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t);
  2846. xfs_iext_irec_compact(ifp);
  2847. }
  2848. /*
  2849. * Create, destroy, or resize a linear (direct) block of extents.
  2850. */
  2851. void
  2852. xfs_iext_realloc_direct(
  2853. xfs_ifork_t *ifp, /* inode fork pointer */
  2854. int new_size) /* new size of extents */
  2855. {
  2856. int rnew_size; /* real new size of extents */
  2857. rnew_size = new_size;
  2858. ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) ||
  2859. ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) &&
  2860. (new_size != ifp->if_real_bytes)));
  2861. /* Free extent records */
  2862. if (new_size == 0) {
  2863. xfs_iext_destroy(ifp);
  2864. }
  2865. /* Resize direct extent list and zero any new bytes */
  2866. else if (ifp->if_real_bytes) {
  2867. /* Check if extents will fit inside the inode */
  2868. if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) {
  2869. xfs_iext_direct_to_inline(ifp, new_size /
  2870. (uint)sizeof(xfs_bmbt_rec_t));
  2871. ifp->if_bytes = new_size;
  2872. return;
  2873. }
  2874. if (!is_power_of_2(new_size)){
  2875. rnew_size = roundup_pow_of_two(new_size);
  2876. }
  2877. if (rnew_size != ifp->if_real_bytes) {
  2878. ifp->if_u1.if_extents =
  2879. kmem_realloc(ifp->if_u1.if_extents,
  2880. rnew_size,
  2881. ifp->if_real_bytes, KM_NOFS);
  2882. }
  2883. if (rnew_size > ifp->if_real_bytes) {
  2884. memset(&ifp->if_u1.if_extents[ifp->if_bytes /
  2885. (uint)sizeof(xfs_bmbt_rec_t)], 0,
  2886. rnew_size - ifp->if_real_bytes);
  2887. }
  2888. }
  2889. /*
  2890. * Switch from the inline extent buffer to a direct
  2891. * extent list. Be sure to include the inline extent
  2892. * bytes in new_size.
  2893. */
  2894. else {
  2895. new_size += ifp->if_bytes;
  2896. if (!is_power_of_2(new_size)) {
  2897. rnew_size = roundup_pow_of_two(new_size);
  2898. }
  2899. xfs_iext_inline_to_direct(ifp, rnew_size);
  2900. }
  2901. ifp->if_real_bytes = rnew_size;
  2902. ifp->if_bytes = new_size;
  2903. }
  2904. /*
  2905. * Switch from linear (direct) extent records to inline buffer.
  2906. */
  2907. void
  2908. xfs_iext_direct_to_inline(
  2909. xfs_ifork_t *ifp, /* inode fork pointer */
  2910. xfs_extnum_t nextents) /* number of extents in file */
  2911. {
  2912. ASSERT(ifp->if_flags & XFS_IFEXTENTS);
  2913. ASSERT(nextents <= XFS_INLINE_EXTS);
  2914. /*
  2915. * The inline buffer was zeroed when we switched
  2916. * from inline to direct extent allocation mode,
  2917. * so we don't need to clear it here.
  2918. */
  2919. memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents,
  2920. nextents * sizeof(xfs_bmbt_rec_t));
  2921. kmem_free(ifp->if_u1.if_extents);
  2922. ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
  2923. ifp->if_real_bytes = 0;
  2924. }
  2925. /*
  2926. * Switch from inline buffer to linear (direct) extent records.
  2927. * new_size should already be rounded up to the next power of 2
  2928. * by the caller (when appropriate), so use new_size as it is.
  2929. * However, since new_size may be rounded up, we can't update
  2930. * if_bytes here. It is the caller's responsibility to update
  2931. * if_bytes upon return.
  2932. */
  2933. void
  2934. xfs_iext_inline_to_direct(
  2935. xfs_ifork_t *ifp, /* inode fork pointer */
  2936. int new_size) /* number of extents in file */
  2937. {
  2938. ifp->if_u1.if_extents = kmem_alloc(new_size, KM_NOFS);
  2939. memset(ifp->if_u1.if_extents, 0, new_size);
  2940. if (ifp->if_bytes) {
  2941. memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext,
  2942. ifp->if_bytes);
  2943. memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
  2944. sizeof(xfs_bmbt_rec_t));
  2945. }
  2946. ifp->if_real_bytes = new_size;
  2947. }
  2948. /*
  2949. * Resize an extent indirection array to new_size bytes.
  2950. */
  2951. STATIC void
  2952. xfs_iext_realloc_indirect(
  2953. xfs_ifork_t *ifp, /* inode fork pointer */
  2954. int new_size) /* new indirection array size */
  2955. {
  2956. int nlists; /* number of irec's (ex lists) */
  2957. int size; /* current indirection array size */
  2958. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  2959. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  2960. size = nlists * sizeof(xfs_ext_irec_t);
  2961. ASSERT(ifp->if_real_bytes);
  2962. ASSERT((new_size >= 0) && (new_size != size));
  2963. if (new_size == 0) {
  2964. xfs_iext_destroy(ifp);
  2965. } else {
  2966. ifp->if_u1.if_ext_irec = (xfs_ext_irec_t *)
  2967. kmem_realloc(ifp->if_u1.if_ext_irec,
  2968. new_size, size, KM_NOFS);
  2969. }
  2970. }
  2971. /*
  2972. * Switch from indirection array to linear (direct) extent allocations.
  2973. */
  2974. STATIC void
  2975. xfs_iext_indirect_to_direct(
  2976. xfs_ifork_t *ifp) /* inode fork pointer */
  2977. {
  2978. xfs_bmbt_rec_host_t *ep; /* extent record pointer */
  2979. xfs_extnum_t nextents; /* number of extents in file */
  2980. int size; /* size of file extents */
  2981. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  2982. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  2983. ASSERT(nextents <= XFS_LINEAR_EXTS);
  2984. size = nextents * sizeof(xfs_bmbt_rec_t);
  2985. xfs_iext_irec_compact_pages(ifp);
  2986. ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ);
  2987. ep = ifp->if_u1.if_ext_irec->er_extbuf;
  2988. kmem_free(ifp->if_u1.if_ext_irec);
  2989. ifp->if_flags &= ~XFS_IFEXTIREC;
  2990. ifp->if_u1.if_extents = ep;
  2991. ifp->if_bytes = size;
  2992. if (nextents < XFS_LINEAR_EXTS) {
  2993. xfs_iext_realloc_direct(ifp, size);
  2994. }
  2995. }
  2996. /*
  2997. * Free incore file extents.
  2998. */
  2999. void
  3000. xfs_iext_destroy(
  3001. xfs_ifork_t *ifp) /* inode fork pointer */
  3002. {
  3003. if (ifp->if_flags & XFS_IFEXTIREC) {
  3004. int erp_idx;
  3005. int nlists;
  3006. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3007. for (erp_idx = nlists - 1; erp_idx >= 0 ; erp_idx--) {
  3008. xfs_iext_irec_remove(ifp, erp_idx);
  3009. }
  3010. ifp->if_flags &= ~XFS_IFEXTIREC;
  3011. } else if (ifp->if_real_bytes) {
  3012. kmem_free(ifp->if_u1.if_extents);
  3013. } else if (ifp->if_bytes) {
  3014. memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
  3015. sizeof(xfs_bmbt_rec_t));
  3016. }
  3017. ifp->if_u1.if_extents = NULL;
  3018. ifp->if_real_bytes = 0;
  3019. ifp->if_bytes = 0;
  3020. }
  3021. /*
  3022. * Return a pointer to the extent record for file system block bno.
  3023. */
  3024. xfs_bmbt_rec_host_t * /* pointer to found extent record */
  3025. xfs_iext_bno_to_ext(
  3026. xfs_ifork_t *ifp, /* inode fork pointer */
  3027. xfs_fileoff_t bno, /* block number to search for */
  3028. xfs_extnum_t *idxp) /* index of target extent */
  3029. {
  3030. xfs_bmbt_rec_host_t *base; /* pointer to first extent */
  3031. xfs_filblks_t blockcount = 0; /* number of blocks in extent */
  3032. xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */
  3033. xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
  3034. int high; /* upper boundary in search */
  3035. xfs_extnum_t idx = 0; /* index of target extent */
  3036. int low; /* lower boundary in search */
  3037. xfs_extnum_t nextents; /* number of file extents */
  3038. xfs_fileoff_t startoff = 0; /* start offset of extent */
  3039. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3040. if (nextents == 0) {
  3041. *idxp = 0;
  3042. return NULL;
  3043. }
  3044. low = 0;
  3045. if (ifp->if_flags & XFS_IFEXTIREC) {
  3046. /* Find target extent list */
  3047. int erp_idx = 0;
  3048. erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
  3049. base = erp->er_extbuf;
  3050. high = erp->er_extcount - 1;
  3051. } else {
  3052. base = ifp->if_u1.if_extents;
  3053. high = nextents - 1;
  3054. }
  3055. /* Binary search extent records */
  3056. while (low <= high) {
  3057. idx = (low + high) >> 1;
  3058. ep = base + idx;
  3059. startoff = xfs_bmbt_get_startoff(ep);
  3060. blockcount = xfs_bmbt_get_blockcount(ep);
  3061. if (bno < startoff) {
  3062. high = idx - 1;
  3063. } else if (bno >= startoff + blockcount) {
  3064. low = idx + 1;
  3065. } else {
  3066. /* Convert back to file-based extent index */
  3067. if (ifp->if_flags & XFS_IFEXTIREC) {
  3068. idx += erp->er_extoff;
  3069. }
  3070. *idxp = idx;
  3071. return ep;
  3072. }
  3073. }
  3074. /* Convert back to file-based extent index */
  3075. if (ifp->if_flags & XFS_IFEXTIREC) {
  3076. idx += erp->er_extoff;
  3077. }
  3078. if (bno >= startoff + blockcount) {
  3079. if (++idx == nextents) {
  3080. ep = NULL;
  3081. } else {
  3082. ep = xfs_iext_get_ext(ifp, idx);
  3083. }
  3084. }
  3085. *idxp = idx;
  3086. return ep;
  3087. }
  3088. /*
  3089. * Return a pointer to the indirection array entry containing the
  3090. * extent record for filesystem block bno. Store the index of the
  3091. * target irec in *erp_idxp.
  3092. */
  3093. xfs_ext_irec_t * /* pointer to found extent record */
  3094. xfs_iext_bno_to_irec(
  3095. xfs_ifork_t *ifp, /* inode fork pointer */
  3096. xfs_fileoff_t bno, /* block number to search for */
  3097. int *erp_idxp) /* irec index of target ext list */
  3098. {
  3099. xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
  3100. xfs_ext_irec_t *erp_next; /* next indirection array entry */
  3101. int erp_idx; /* indirection array index */
  3102. int nlists; /* number of extent irec's (lists) */
  3103. int high; /* binary search upper limit */
  3104. int low; /* binary search lower limit */
  3105. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3106. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3107. erp_idx = 0;
  3108. low = 0;
  3109. high = nlists - 1;
  3110. while (low <= high) {
  3111. erp_idx = (low + high) >> 1;
  3112. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  3113. erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL;
  3114. if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) {
  3115. high = erp_idx - 1;
  3116. } else if (erp_next && bno >=
  3117. xfs_bmbt_get_startoff(erp_next->er_extbuf)) {
  3118. low = erp_idx + 1;
  3119. } else {
  3120. break;
  3121. }
  3122. }
  3123. *erp_idxp = erp_idx;
  3124. return erp;
  3125. }
  3126. /*
  3127. * Return a pointer to the indirection array entry containing the
  3128. * extent record at file extent index *idxp. Store the index of the
  3129. * target irec in *erp_idxp and store the page index of the target
  3130. * extent record in *idxp.
  3131. */
  3132. xfs_ext_irec_t *
  3133. xfs_iext_idx_to_irec(
  3134. xfs_ifork_t *ifp, /* inode fork pointer */
  3135. xfs_extnum_t *idxp, /* extent index (file -> page) */
  3136. int *erp_idxp, /* pointer to target irec */
  3137. int realloc) /* new bytes were just added */
  3138. {
  3139. xfs_ext_irec_t *prev; /* pointer to previous irec */
  3140. xfs_ext_irec_t *erp = NULL; /* pointer to current irec */
  3141. int erp_idx; /* indirection array index */
  3142. int nlists; /* number of irec's (ex lists) */
  3143. int high; /* binary search upper limit */
  3144. int low; /* binary search lower limit */
  3145. xfs_extnum_t page_idx = *idxp; /* extent index in target list */
  3146. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3147. ASSERT(page_idx >= 0);
  3148. ASSERT(page_idx <= ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
  3149. ASSERT(page_idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t) || realloc);
  3150. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3151. erp_idx = 0;
  3152. low = 0;
  3153. high = nlists - 1;
  3154. /* Binary search extent irec's */
  3155. while (low <= high) {
  3156. erp_idx = (low + high) >> 1;
  3157. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  3158. prev = erp_idx > 0 ? erp - 1 : NULL;
  3159. if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff &&
  3160. realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) {
  3161. high = erp_idx - 1;
  3162. } else if (page_idx > erp->er_extoff + erp->er_extcount ||
  3163. (page_idx == erp->er_extoff + erp->er_extcount &&
  3164. !realloc)) {
  3165. low = erp_idx + 1;
  3166. } else if (page_idx == erp->er_extoff + erp->er_extcount &&
  3167. erp->er_extcount == XFS_LINEAR_EXTS) {
  3168. ASSERT(realloc);
  3169. page_idx = 0;
  3170. erp_idx++;
  3171. erp = erp_idx < nlists ? erp + 1 : NULL;
  3172. break;
  3173. } else {
  3174. page_idx -= erp->er_extoff;
  3175. break;
  3176. }
  3177. }
  3178. *idxp = page_idx;
  3179. *erp_idxp = erp_idx;
  3180. return(erp);
  3181. }
  3182. /*
  3183. * Allocate and initialize an indirection array once the space needed
  3184. * for incore extents increases above XFS_IEXT_BUFSZ.
  3185. */
  3186. void
  3187. xfs_iext_irec_init(
  3188. xfs_ifork_t *ifp) /* inode fork pointer */
  3189. {
  3190. xfs_ext_irec_t *erp; /* indirection array pointer */
  3191. xfs_extnum_t nextents; /* number of extents in file */
  3192. ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
  3193. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3194. ASSERT(nextents <= XFS_LINEAR_EXTS);
  3195. erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS);
  3196. if (nextents == 0) {
  3197. ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
  3198. } else if (!ifp->if_real_bytes) {
  3199. xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ);
  3200. } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) {
  3201. xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ);
  3202. }
  3203. erp->er_extbuf = ifp->if_u1.if_extents;
  3204. erp->er_extcount = nextents;
  3205. erp->er_extoff = 0;
  3206. ifp->if_flags |= XFS_IFEXTIREC;
  3207. ifp->if_real_bytes = XFS_IEXT_BUFSZ;
  3208. ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t);
  3209. ifp->if_u1.if_ext_irec = erp;
  3210. return;
  3211. }
  3212. /*
  3213. * Allocate and initialize a new entry in the indirection array.
  3214. */
  3215. xfs_ext_irec_t *
  3216. xfs_iext_irec_new(
  3217. xfs_ifork_t *ifp, /* inode fork pointer */
  3218. int erp_idx) /* index for new irec */
  3219. {
  3220. xfs_ext_irec_t *erp; /* indirection array pointer */
  3221. int i; /* loop counter */
  3222. int nlists; /* number of irec's (ex lists) */
  3223. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3224. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3225. /* Resize indirection array */
  3226. xfs_iext_realloc_indirect(ifp, ++nlists *
  3227. sizeof(xfs_ext_irec_t));
  3228. /*
  3229. * Move records down in the array so the
  3230. * new page can use erp_idx.
  3231. */
  3232. erp = ifp->if_u1.if_ext_irec;
  3233. for (i = nlists - 1; i > erp_idx; i--) {
  3234. memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t));
  3235. }
  3236. ASSERT(i == erp_idx);
  3237. /* Initialize new extent record */
  3238. erp = ifp->if_u1.if_ext_irec;
  3239. erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
  3240. ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
  3241. memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ);
  3242. erp[erp_idx].er_extcount = 0;
  3243. erp[erp_idx].er_extoff = erp_idx > 0 ?
  3244. erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0;
  3245. return (&erp[erp_idx]);
  3246. }
  3247. /*
  3248. * Remove a record from the indirection array.
  3249. */
  3250. void
  3251. xfs_iext_irec_remove(
  3252. xfs_ifork_t *ifp, /* inode fork pointer */
  3253. int erp_idx) /* irec index to remove */
  3254. {
  3255. xfs_ext_irec_t *erp; /* indirection array pointer */
  3256. int i; /* loop counter */
  3257. int nlists; /* number of irec's (ex lists) */
  3258. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3259. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3260. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  3261. if (erp->er_extbuf) {
  3262. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1,
  3263. -erp->er_extcount);
  3264. kmem_free(erp->er_extbuf);
  3265. }
  3266. /* Compact extent records */
  3267. erp = ifp->if_u1.if_ext_irec;
  3268. for (i = erp_idx; i < nlists - 1; i++) {
  3269. memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t));
  3270. }
  3271. /*
  3272. * Manually free the last extent record from the indirection
  3273. * array. A call to xfs_iext_realloc_indirect() with a size
  3274. * of zero would result in a call to xfs_iext_destroy() which
  3275. * would in turn call this function again, creating a nasty
  3276. * infinite loop.
  3277. */
  3278. if (--nlists) {
  3279. xfs_iext_realloc_indirect(ifp,
  3280. nlists * sizeof(xfs_ext_irec_t));
  3281. } else {
  3282. kmem_free(ifp->if_u1.if_ext_irec);
  3283. }
  3284. ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
  3285. }
  3286. /*
  3287. * This is called to clean up large amounts of unused memory allocated
  3288. * by the indirection array. Before compacting anything though, verify
  3289. * that the indirection array is still needed and switch back to the
  3290. * linear extent list (or even the inline buffer) if possible. The
  3291. * compaction policy is as follows:
  3292. *
  3293. * Full Compaction: Extents fit into a single page (or inline buffer)
  3294. * Partial Compaction: Extents occupy less than 50% of allocated space
  3295. * No Compaction: Extents occupy at least 50% of allocated space
  3296. */
  3297. void
  3298. xfs_iext_irec_compact(
  3299. xfs_ifork_t *ifp) /* inode fork pointer */
  3300. {
  3301. xfs_extnum_t nextents; /* number of extents in file */
  3302. int nlists; /* number of irec's (ex lists) */
  3303. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3304. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3305. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3306. if (nextents == 0) {
  3307. xfs_iext_destroy(ifp);
  3308. } else if (nextents <= XFS_INLINE_EXTS) {
  3309. xfs_iext_indirect_to_direct(ifp);
  3310. xfs_iext_direct_to_inline(ifp, nextents);
  3311. } else if (nextents <= XFS_LINEAR_EXTS) {
  3312. xfs_iext_indirect_to_direct(ifp);
  3313. } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) {
  3314. xfs_iext_irec_compact_pages(ifp);
  3315. }
  3316. }
  3317. /*
  3318. * Combine extents from neighboring extent pages.
  3319. */
  3320. void
  3321. xfs_iext_irec_compact_pages(
  3322. xfs_ifork_t *ifp) /* inode fork pointer */
  3323. {
  3324. xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */
  3325. int erp_idx = 0; /* indirection array index */
  3326. int nlists; /* number of irec's (ex lists) */
  3327. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3328. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3329. while (erp_idx < nlists - 1) {
  3330. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  3331. erp_next = erp + 1;
  3332. if (erp_next->er_extcount <=
  3333. (XFS_LINEAR_EXTS - erp->er_extcount)) {
  3334. memcpy(&erp->er_extbuf[erp->er_extcount],
  3335. erp_next->er_extbuf, erp_next->er_extcount *
  3336. sizeof(xfs_bmbt_rec_t));
  3337. erp->er_extcount += erp_next->er_extcount;
  3338. /*
  3339. * Free page before removing extent record
  3340. * so er_extoffs don't get modified in
  3341. * xfs_iext_irec_remove.
  3342. */
  3343. kmem_free(erp_next->er_extbuf);
  3344. erp_next->er_extbuf = NULL;
  3345. xfs_iext_irec_remove(ifp, erp_idx + 1);
  3346. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3347. } else {
  3348. erp_idx++;
  3349. }
  3350. }
  3351. }
  3352. /*
  3353. * This is called to update the er_extoff field in the indirection
  3354. * array when extents have been added or removed from one of the
  3355. * extent lists. erp_idx contains the irec index to begin updating
  3356. * at and ext_diff contains the number of extents that were added
  3357. * or removed.
  3358. */
  3359. void
  3360. xfs_iext_irec_update_extoffs(
  3361. xfs_ifork_t *ifp, /* inode fork pointer */
  3362. int erp_idx, /* irec index to update */
  3363. int ext_diff) /* number of new extents */
  3364. {
  3365. int i; /* loop counter */
  3366. int nlists; /* number of irec's (ex lists */
  3367. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3368. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3369. for (i = erp_idx; i < nlists; i++) {
  3370. ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff;
  3371. }
  3372. }