xfs_inode.c 107 KB

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