xfs_inode.c 108 KB

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