xfs_vnodeops.c 110 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484
  1. /*
  2. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_types.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_dir2.h"
  28. #include "xfs_dmapi.h"
  29. #include "xfs_mount.h"
  30. #include "xfs_da_btree.h"
  31. #include "xfs_bmap_btree.h"
  32. #include "xfs_alloc_btree.h"
  33. #include "xfs_ialloc_btree.h"
  34. #include "xfs_dir2_sf.h"
  35. #include "xfs_attr_sf.h"
  36. #include "xfs_dinode.h"
  37. #include "xfs_inode.h"
  38. #include "xfs_inode_item.h"
  39. #include "xfs_itable.h"
  40. #include "xfs_btree.h"
  41. #include "xfs_ialloc.h"
  42. #include "xfs_alloc.h"
  43. #include "xfs_bmap.h"
  44. #include "xfs_attr.h"
  45. #include "xfs_rw.h"
  46. #include "xfs_error.h"
  47. #include "xfs_quota.h"
  48. #include "xfs_utils.h"
  49. #include "xfs_rtalloc.h"
  50. #include "xfs_refcache.h"
  51. #include "xfs_trans_space.h"
  52. #include "xfs_log_priv.h"
  53. #include "xfs_filestream.h"
  54. #include "xfs_vnodeops.h"
  55. int
  56. xfs_open(
  57. xfs_inode_t *ip)
  58. {
  59. int mode;
  60. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  61. return XFS_ERROR(EIO);
  62. /*
  63. * If it's a directory with any blocks, read-ahead block 0
  64. * as we're almost certain to have the next operation be a read there.
  65. */
  66. if (S_ISDIR(ip->i_d.di_mode) && ip->i_d.di_nextents > 0) {
  67. mode = xfs_ilock_map_shared(ip);
  68. if (ip->i_d.di_nextents > 0)
  69. (void)xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
  70. xfs_iunlock(ip, mode);
  71. }
  72. return 0;
  73. }
  74. /*
  75. * xfs_getattr
  76. */
  77. int
  78. xfs_getattr(
  79. xfs_inode_t *ip,
  80. bhv_vattr_t *vap,
  81. int flags)
  82. {
  83. bhv_vnode_t *vp = XFS_ITOV(ip);
  84. xfs_mount_t *mp = ip->i_mount;
  85. xfs_itrace_entry(ip);
  86. if (XFS_FORCED_SHUTDOWN(mp))
  87. return XFS_ERROR(EIO);
  88. if (!(flags & ATTR_LAZY))
  89. xfs_ilock(ip, XFS_ILOCK_SHARED);
  90. vap->va_size = XFS_ISIZE(ip);
  91. if (vap->va_mask == XFS_AT_SIZE)
  92. goto all_done;
  93. vap->va_nblocks =
  94. XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
  95. vap->va_nodeid = ip->i_ino;
  96. #if XFS_BIG_INUMS
  97. vap->va_nodeid += mp->m_inoadd;
  98. #endif
  99. vap->va_nlink = ip->i_d.di_nlink;
  100. /*
  101. * Quick exit for non-stat callers
  102. */
  103. if ((vap->va_mask &
  104. ~(XFS_AT_SIZE|XFS_AT_FSID|XFS_AT_NODEID|
  105. XFS_AT_NLINK|XFS_AT_BLKSIZE)) == 0)
  106. goto all_done;
  107. /*
  108. * Copy from in-core inode.
  109. */
  110. vap->va_mode = ip->i_d.di_mode;
  111. vap->va_uid = ip->i_d.di_uid;
  112. vap->va_gid = ip->i_d.di_gid;
  113. vap->va_projid = ip->i_d.di_projid;
  114. /*
  115. * Check vnode type block/char vs. everything else.
  116. */
  117. switch (ip->i_d.di_mode & S_IFMT) {
  118. case S_IFBLK:
  119. case S_IFCHR:
  120. vap->va_rdev = ip->i_df.if_u2.if_rdev;
  121. vap->va_blocksize = BLKDEV_IOSIZE;
  122. break;
  123. default:
  124. vap->va_rdev = 0;
  125. if (!(XFS_IS_REALTIME_INODE(ip))) {
  126. vap->va_blocksize = xfs_preferred_iosize(mp);
  127. } else {
  128. /*
  129. * If the file blocks are being allocated from a
  130. * realtime partition, then return the inode's
  131. * realtime extent size or the realtime volume's
  132. * extent size.
  133. */
  134. vap->va_blocksize =
  135. xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
  136. }
  137. break;
  138. }
  139. vn_atime_to_timespec(vp, &vap->va_atime);
  140. vap->va_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
  141. vap->va_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
  142. vap->va_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
  143. vap->va_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
  144. /*
  145. * Exit for stat callers. See if any of the rest of the fields
  146. * to be filled in are needed.
  147. */
  148. if ((vap->va_mask &
  149. (XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
  150. XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
  151. goto all_done;
  152. /*
  153. * Convert di_flags to xflags.
  154. */
  155. vap->va_xflags = xfs_ip2xflags(ip);
  156. /*
  157. * Exit for inode revalidate. See if any of the rest of
  158. * the fields to be filled in are needed.
  159. */
  160. if ((vap->va_mask &
  161. (XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
  162. XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
  163. goto all_done;
  164. vap->va_extsize = ip->i_d.di_extsize << mp->m_sb.sb_blocklog;
  165. vap->va_nextents =
  166. (ip->i_df.if_flags & XFS_IFEXTENTS) ?
  167. ip->i_df.if_bytes / sizeof(xfs_bmbt_rec_t) :
  168. ip->i_d.di_nextents;
  169. if (ip->i_afp)
  170. vap->va_anextents =
  171. (ip->i_afp->if_flags & XFS_IFEXTENTS) ?
  172. ip->i_afp->if_bytes / sizeof(xfs_bmbt_rec_t) :
  173. ip->i_d.di_anextents;
  174. else
  175. vap->va_anextents = 0;
  176. vap->va_gen = ip->i_d.di_gen;
  177. all_done:
  178. if (!(flags & ATTR_LAZY))
  179. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  180. return 0;
  181. }
  182. /*
  183. * xfs_setattr
  184. */
  185. int
  186. xfs_setattr(
  187. xfs_inode_t *ip,
  188. bhv_vattr_t *vap,
  189. int flags,
  190. cred_t *credp)
  191. {
  192. bhv_vnode_t *vp = XFS_ITOV(ip);
  193. xfs_mount_t *mp = ip->i_mount;
  194. xfs_trans_t *tp;
  195. int mask;
  196. int code;
  197. uint lock_flags;
  198. uint commit_flags=0;
  199. uid_t uid=0, iuid=0;
  200. gid_t gid=0, igid=0;
  201. int timeflags = 0;
  202. xfs_prid_t projid=0, iprojid=0;
  203. int mandlock_before, mandlock_after;
  204. struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
  205. int file_owner;
  206. int need_iolock = 1;
  207. xfs_itrace_entry(ip);
  208. if (mp->m_flags & XFS_MOUNT_RDONLY)
  209. return XFS_ERROR(EROFS);
  210. /*
  211. * Cannot set certain attributes.
  212. */
  213. mask = vap->va_mask;
  214. if (mask & XFS_AT_NOSET) {
  215. return XFS_ERROR(EINVAL);
  216. }
  217. if (XFS_FORCED_SHUTDOWN(mp))
  218. return XFS_ERROR(EIO);
  219. /*
  220. * Timestamps do not need to be logged and hence do not
  221. * need to be done within a transaction.
  222. */
  223. if (mask & XFS_AT_UPDTIMES) {
  224. ASSERT((mask & ~XFS_AT_UPDTIMES) == 0);
  225. timeflags = ((mask & XFS_AT_UPDATIME) ? XFS_ICHGTIME_ACC : 0) |
  226. ((mask & XFS_AT_UPDCTIME) ? XFS_ICHGTIME_CHG : 0) |
  227. ((mask & XFS_AT_UPDMTIME) ? XFS_ICHGTIME_MOD : 0);
  228. xfs_ichgtime(ip, timeflags);
  229. return 0;
  230. }
  231. olddquot1 = olddquot2 = NULL;
  232. udqp = gdqp = NULL;
  233. /*
  234. * If disk quotas is on, we make sure that the dquots do exist on disk,
  235. * before we start any other transactions. Trying to do this later
  236. * is messy. We don't care to take a readlock to look at the ids
  237. * in inode here, because we can't hold it across the trans_reserve.
  238. * If the IDs do change before we take the ilock, we're covered
  239. * because the i_*dquot fields will get updated anyway.
  240. */
  241. if (XFS_IS_QUOTA_ON(mp) &&
  242. (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID))) {
  243. uint qflags = 0;
  244. if ((mask & XFS_AT_UID) && XFS_IS_UQUOTA_ON(mp)) {
  245. uid = vap->va_uid;
  246. qflags |= XFS_QMOPT_UQUOTA;
  247. } else {
  248. uid = ip->i_d.di_uid;
  249. }
  250. if ((mask & XFS_AT_GID) && XFS_IS_GQUOTA_ON(mp)) {
  251. gid = vap->va_gid;
  252. qflags |= XFS_QMOPT_GQUOTA;
  253. } else {
  254. gid = ip->i_d.di_gid;
  255. }
  256. if ((mask & XFS_AT_PROJID) && XFS_IS_PQUOTA_ON(mp)) {
  257. projid = vap->va_projid;
  258. qflags |= XFS_QMOPT_PQUOTA;
  259. } else {
  260. projid = ip->i_d.di_projid;
  261. }
  262. /*
  263. * We take a reference when we initialize udqp and gdqp,
  264. * so it is important that we never blindly double trip on
  265. * the same variable. See xfs_create() for an example.
  266. */
  267. ASSERT(udqp == NULL);
  268. ASSERT(gdqp == NULL);
  269. code = XFS_QM_DQVOPALLOC(mp, ip, uid, gid, projid, qflags,
  270. &udqp, &gdqp);
  271. if (code)
  272. return code;
  273. }
  274. /*
  275. * For the other attributes, we acquire the inode lock and
  276. * first do an error checking pass.
  277. */
  278. tp = NULL;
  279. lock_flags = XFS_ILOCK_EXCL;
  280. if (flags & ATTR_NOLOCK)
  281. need_iolock = 0;
  282. if (!(mask & XFS_AT_SIZE)) {
  283. if ((mask != (XFS_AT_CTIME|XFS_AT_ATIME|XFS_AT_MTIME)) ||
  284. (mp->m_flags & XFS_MOUNT_WSYNC)) {
  285. tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
  286. commit_flags = 0;
  287. if ((code = xfs_trans_reserve(tp, 0,
  288. XFS_ICHANGE_LOG_RES(mp), 0,
  289. 0, 0))) {
  290. lock_flags = 0;
  291. goto error_return;
  292. }
  293. }
  294. } else {
  295. if (DM_EVENT_ENABLED(ip, DM_EVENT_TRUNCATE) &&
  296. !(flags & ATTR_DMI)) {
  297. int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
  298. code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, vp,
  299. vap->va_size, 0, dmflags, NULL);
  300. if (code) {
  301. lock_flags = 0;
  302. goto error_return;
  303. }
  304. }
  305. if (need_iolock)
  306. lock_flags |= XFS_IOLOCK_EXCL;
  307. }
  308. xfs_ilock(ip, lock_flags);
  309. /* boolean: are we the file owner? */
  310. file_owner = (current_fsuid(credp) == ip->i_d.di_uid);
  311. /*
  312. * Change various properties of a file.
  313. * Only the owner or users with CAP_FOWNER
  314. * capability may do these things.
  315. */
  316. if (mask &
  317. (XFS_AT_MODE|XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_UID|
  318. XFS_AT_GID|XFS_AT_PROJID)) {
  319. /*
  320. * CAP_FOWNER overrides the following restrictions:
  321. *
  322. * The user ID of the calling process must be equal
  323. * to the file owner ID, except in cases where the
  324. * CAP_FSETID capability is applicable.
  325. */
  326. if (!file_owner && !capable(CAP_FOWNER)) {
  327. code = XFS_ERROR(EPERM);
  328. goto error_return;
  329. }
  330. /*
  331. * CAP_FSETID overrides the following restrictions:
  332. *
  333. * The effective user ID of the calling process shall match
  334. * the file owner when setting the set-user-ID and
  335. * set-group-ID bits on that file.
  336. *
  337. * The effective group ID or one of the supplementary group
  338. * IDs of the calling process shall match the group owner of
  339. * the file when setting the set-group-ID bit on that file
  340. */
  341. if (mask & XFS_AT_MODE) {
  342. mode_t m = 0;
  343. if ((vap->va_mode & S_ISUID) && !file_owner)
  344. m |= S_ISUID;
  345. if ((vap->va_mode & S_ISGID) &&
  346. !in_group_p((gid_t)ip->i_d.di_gid))
  347. m |= S_ISGID;
  348. #if 0
  349. /* Linux allows this, Irix doesn't. */
  350. if ((vap->va_mode & S_ISVTX) && !VN_ISDIR(vp))
  351. m |= S_ISVTX;
  352. #endif
  353. if (m && !capable(CAP_FSETID))
  354. vap->va_mode &= ~m;
  355. }
  356. }
  357. /*
  358. * Change file ownership. Must be the owner or privileged.
  359. * If the system was configured with the "restricted_chown"
  360. * option, the owner is not permitted to give away the file,
  361. * and can change the group id only to a group of which he
  362. * or she is a member.
  363. */
  364. if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
  365. /*
  366. * These IDs could have changed since we last looked at them.
  367. * But, we're assured that if the ownership did change
  368. * while we didn't have the inode locked, inode's dquot(s)
  369. * would have changed also.
  370. */
  371. iuid = ip->i_d.di_uid;
  372. iprojid = ip->i_d.di_projid;
  373. igid = ip->i_d.di_gid;
  374. gid = (mask & XFS_AT_GID) ? vap->va_gid : igid;
  375. uid = (mask & XFS_AT_UID) ? vap->va_uid : iuid;
  376. projid = (mask & XFS_AT_PROJID) ? (xfs_prid_t)vap->va_projid :
  377. iprojid;
  378. /*
  379. * CAP_CHOWN overrides the following restrictions:
  380. *
  381. * If _POSIX_CHOWN_RESTRICTED is defined, this capability
  382. * shall override the restriction that a process cannot
  383. * change the user ID of a file it owns and the restriction
  384. * that the group ID supplied to the chown() function
  385. * shall be equal to either the group ID or one of the
  386. * supplementary group IDs of the calling process.
  387. */
  388. if (restricted_chown &&
  389. (iuid != uid || (igid != gid &&
  390. !in_group_p((gid_t)gid))) &&
  391. !capable(CAP_CHOWN)) {
  392. code = XFS_ERROR(EPERM);
  393. goto error_return;
  394. }
  395. /*
  396. * Do a quota reservation only if uid/projid/gid is actually
  397. * going to change.
  398. */
  399. if ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
  400. (XFS_IS_PQUOTA_ON(mp) && iprojid != projid) ||
  401. (XFS_IS_GQUOTA_ON(mp) && igid != gid)) {
  402. ASSERT(tp);
  403. code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
  404. capable(CAP_FOWNER) ?
  405. XFS_QMOPT_FORCE_RES : 0);
  406. if (code) /* out of quota */
  407. goto error_return;
  408. }
  409. }
  410. /*
  411. * Truncate file. Must have write permission and not be a directory.
  412. */
  413. if (mask & XFS_AT_SIZE) {
  414. /* Short circuit the truncate case for zero length files */
  415. if ((vap->va_size == 0) &&
  416. (ip->i_size == 0) && (ip->i_d.di_nextents == 0)) {
  417. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  418. lock_flags &= ~XFS_ILOCK_EXCL;
  419. if (mask & XFS_AT_CTIME)
  420. xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  421. code = 0;
  422. goto error_return;
  423. }
  424. if (VN_ISDIR(vp)) {
  425. code = XFS_ERROR(EISDIR);
  426. goto error_return;
  427. } else if (!VN_ISREG(vp)) {
  428. code = XFS_ERROR(EINVAL);
  429. goto error_return;
  430. }
  431. /*
  432. * Make sure that the dquots are attached to the inode.
  433. */
  434. if ((code = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED)))
  435. goto error_return;
  436. }
  437. /*
  438. * Change file access or modified times.
  439. */
  440. if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
  441. if (!file_owner) {
  442. if ((flags & ATTR_UTIME) &&
  443. !capable(CAP_FOWNER)) {
  444. code = XFS_ERROR(EPERM);
  445. goto error_return;
  446. }
  447. }
  448. }
  449. /*
  450. * Change extent size or realtime flag.
  451. */
  452. if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
  453. /*
  454. * Can't change extent size if any extents are allocated.
  455. */
  456. if (ip->i_d.di_nextents && (mask & XFS_AT_EXTSIZE) &&
  457. ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
  458. vap->va_extsize) ) {
  459. code = XFS_ERROR(EINVAL); /* EFBIG? */
  460. goto error_return;
  461. }
  462. /*
  463. * Can't change realtime flag if any extents are allocated.
  464. */
  465. if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
  466. (mask & XFS_AT_XFLAGS) &&
  467. (XFS_IS_REALTIME_INODE(ip)) !=
  468. (vap->va_xflags & XFS_XFLAG_REALTIME)) {
  469. code = XFS_ERROR(EINVAL); /* EFBIG? */
  470. goto error_return;
  471. }
  472. /*
  473. * Extent size must be a multiple of the appropriate block
  474. * size, if set at all.
  475. */
  476. if ((mask & XFS_AT_EXTSIZE) && vap->va_extsize != 0) {
  477. xfs_extlen_t size;
  478. if (XFS_IS_REALTIME_INODE(ip) ||
  479. ((mask & XFS_AT_XFLAGS) &&
  480. (vap->va_xflags & XFS_XFLAG_REALTIME))) {
  481. size = mp->m_sb.sb_rextsize <<
  482. mp->m_sb.sb_blocklog;
  483. } else {
  484. size = mp->m_sb.sb_blocksize;
  485. }
  486. if (vap->va_extsize % size) {
  487. code = XFS_ERROR(EINVAL);
  488. goto error_return;
  489. }
  490. }
  491. /*
  492. * If realtime flag is set then must have realtime data.
  493. */
  494. if ((mask & XFS_AT_XFLAGS) &&
  495. (vap->va_xflags & XFS_XFLAG_REALTIME)) {
  496. if ((mp->m_sb.sb_rblocks == 0) ||
  497. (mp->m_sb.sb_rextsize == 0) ||
  498. (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
  499. code = XFS_ERROR(EINVAL);
  500. goto error_return;
  501. }
  502. }
  503. /*
  504. * Can't modify an immutable/append-only file unless
  505. * we have appropriate permission.
  506. */
  507. if ((mask & XFS_AT_XFLAGS) &&
  508. (ip->i_d.di_flags &
  509. (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
  510. (vap->va_xflags &
  511. (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
  512. !capable(CAP_LINUX_IMMUTABLE)) {
  513. code = XFS_ERROR(EPERM);
  514. goto error_return;
  515. }
  516. }
  517. /*
  518. * Now we can make the changes. Before we join the inode
  519. * to the transaction, if XFS_AT_SIZE is set then take care of
  520. * the part of the truncation that must be done without the
  521. * inode lock. This needs to be done before joining the inode
  522. * to the transaction, because the inode cannot be unlocked
  523. * once it is a part of the transaction.
  524. */
  525. if (mask & XFS_AT_SIZE) {
  526. code = 0;
  527. if ((vap->va_size > ip->i_size) &&
  528. (flags & ATTR_NOSIZETOK) == 0) {
  529. code = xfs_igrow_start(ip, vap->va_size, credp);
  530. }
  531. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  532. /*
  533. * We are going to log the inode size change in this
  534. * transaction so any previous writes that are beyond the on
  535. * disk EOF and the new EOF that have not been written out need
  536. * to be written here. If we do not write the data out, we
  537. * expose ourselves to the null files problem.
  538. *
  539. * Only flush from the on disk size to the smaller of the in
  540. * memory file size or the new size as that's the range we
  541. * really care about here and prevents waiting for other data
  542. * not within the range we care about here.
  543. */
  544. if (!code &&
  545. (ip->i_size != ip->i_d.di_size) &&
  546. (vap->va_size > ip->i_d.di_size)) {
  547. code = xfs_flush_pages(ip,
  548. ip->i_d.di_size, vap->va_size,
  549. XFS_B_ASYNC, FI_NONE);
  550. }
  551. /* wait for all I/O to complete */
  552. vn_iowait(ip);
  553. if (!code)
  554. code = xfs_itruncate_data(ip, vap->va_size);
  555. if (code) {
  556. ASSERT(tp == NULL);
  557. lock_flags &= ~XFS_ILOCK_EXCL;
  558. ASSERT(lock_flags == XFS_IOLOCK_EXCL);
  559. goto error_return;
  560. }
  561. tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
  562. if ((code = xfs_trans_reserve(tp, 0,
  563. XFS_ITRUNCATE_LOG_RES(mp), 0,
  564. XFS_TRANS_PERM_LOG_RES,
  565. XFS_ITRUNCATE_LOG_COUNT))) {
  566. xfs_trans_cancel(tp, 0);
  567. if (need_iolock)
  568. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  569. return code;
  570. }
  571. commit_flags = XFS_TRANS_RELEASE_LOG_RES;
  572. xfs_ilock(ip, XFS_ILOCK_EXCL);
  573. }
  574. if (tp) {
  575. xfs_trans_ijoin(tp, ip, lock_flags);
  576. xfs_trans_ihold(tp, ip);
  577. }
  578. /* determine whether mandatory locking mode changes */
  579. mandlock_before = MANDLOCK(vp, ip->i_d.di_mode);
  580. /*
  581. * Truncate file. Must have write permission and not be a directory.
  582. */
  583. if (mask & XFS_AT_SIZE) {
  584. if (vap->va_size > ip->i_size) {
  585. xfs_igrow_finish(tp, ip, vap->va_size,
  586. !(flags & ATTR_DMI));
  587. } else if ((vap->va_size <= ip->i_size) ||
  588. ((vap->va_size == 0) && ip->i_d.di_nextents)) {
  589. /*
  590. * signal a sync transaction unless
  591. * we're truncating an already unlinked
  592. * file on a wsync filesystem
  593. */
  594. code = xfs_itruncate_finish(&tp, ip,
  595. (xfs_fsize_t)vap->va_size,
  596. XFS_DATA_FORK,
  597. ((ip->i_d.di_nlink != 0 ||
  598. !(mp->m_flags & XFS_MOUNT_WSYNC))
  599. ? 1 : 0));
  600. if (code)
  601. goto abort_return;
  602. /*
  603. * Truncated "down", so we're removing references
  604. * to old data here - if we now delay flushing for
  605. * a long time, we expose ourselves unduly to the
  606. * notorious NULL files problem. So, we mark this
  607. * vnode and flush it when the file is closed, and
  608. * do not wait the usual (long) time for writeout.
  609. */
  610. xfs_iflags_set(ip, XFS_ITRUNCATED);
  611. }
  612. /*
  613. * Have to do this even if the file's size doesn't change.
  614. */
  615. timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
  616. }
  617. /*
  618. * Change file access modes.
  619. */
  620. if (mask & XFS_AT_MODE) {
  621. ip->i_d.di_mode &= S_IFMT;
  622. ip->i_d.di_mode |= vap->va_mode & ~S_IFMT;
  623. xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
  624. timeflags |= XFS_ICHGTIME_CHG;
  625. }
  626. /*
  627. * Change file ownership. Must be the owner or privileged.
  628. * If the system was configured with the "restricted_chown"
  629. * option, the owner is not permitted to give away the file,
  630. * and can change the group id only to a group of which he
  631. * or she is a member.
  632. */
  633. if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
  634. /*
  635. * CAP_FSETID overrides the following restrictions:
  636. *
  637. * The set-user-ID and set-group-ID bits of a file will be
  638. * cleared upon successful return from chown()
  639. */
  640. if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
  641. !capable(CAP_FSETID)) {
  642. ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
  643. }
  644. /*
  645. * Change the ownerships and register quota modifications
  646. * in the transaction.
  647. */
  648. if (iuid != uid) {
  649. if (XFS_IS_UQUOTA_ON(mp)) {
  650. ASSERT(mask & XFS_AT_UID);
  651. ASSERT(udqp);
  652. olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
  653. &ip->i_udquot, udqp);
  654. }
  655. ip->i_d.di_uid = uid;
  656. }
  657. if (igid != gid) {
  658. if (XFS_IS_GQUOTA_ON(mp)) {
  659. ASSERT(!XFS_IS_PQUOTA_ON(mp));
  660. ASSERT(mask & XFS_AT_GID);
  661. ASSERT(gdqp);
  662. olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
  663. &ip->i_gdquot, gdqp);
  664. }
  665. ip->i_d.di_gid = gid;
  666. }
  667. if (iprojid != projid) {
  668. if (XFS_IS_PQUOTA_ON(mp)) {
  669. ASSERT(!XFS_IS_GQUOTA_ON(mp));
  670. ASSERT(mask & XFS_AT_PROJID);
  671. ASSERT(gdqp);
  672. olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
  673. &ip->i_gdquot, gdqp);
  674. }
  675. ip->i_d.di_projid = projid;
  676. /*
  677. * We may have to rev the inode as well as
  678. * the superblock version number since projids didn't
  679. * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
  680. */
  681. if (ip->i_d.di_version == XFS_DINODE_VERSION_1)
  682. xfs_bump_ino_vers2(tp, ip);
  683. }
  684. xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
  685. timeflags |= XFS_ICHGTIME_CHG;
  686. }
  687. /*
  688. * Change file access or modified times.
  689. */
  690. if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
  691. if (mask & XFS_AT_ATIME) {
  692. ip->i_d.di_atime.t_sec = vap->va_atime.tv_sec;
  693. ip->i_d.di_atime.t_nsec = vap->va_atime.tv_nsec;
  694. ip->i_update_core = 1;
  695. timeflags &= ~XFS_ICHGTIME_ACC;
  696. }
  697. if (mask & XFS_AT_MTIME) {
  698. ip->i_d.di_mtime.t_sec = vap->va_mtime.tv_sec;
  699. ip->i_d.di_mtime.t_nsec = vap->va_mtime.tv_nsec;
  700. timeflags &= ~XFS_ICHGTIME_MOD;
  701. timeflags |= XFS_ICHGTIME_CHG;
  702. }
  703. if (tp && (flags & ATTR_UTIME))
  704. xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
  705. }
  706. /*
  707. * Change XFS-added attributes.
  708. */
  709. if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
  710. if (mask & XFS_AT_EXTSIZE) {
  711. /*
  712. * Converting bytes to fs blocks.
  713. */
  714. ip->i_d.di_extsize = vap->va_extsize >>
  715. mp->m_sb.sb_blocklog;
  716. }
  717. if (mask & XFS_AT_XFLAGS) {
  718. uint di_flags;
  719. /* can't set PREALLOC this way, just preserve it */
  720. di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
  721. if (vap->va_xflags & XFS_XFLAG_IMMUTABLE)
  722. di_flags |= XFS_DIFLAG_IMMUTABLE;
  723. if (vap->va_xflags & XFS_XFLAG_APPEND)
  724. di_flags |= XFS_DIFLAG_APPEND;
  725. if (vap->va_xflags & XFS_XFLAG_SYNC)
  726. di_flags |= XFS_DIFLAG_SYNC;
  727. if (vap->va_xflags & XFS_XFLAG_NOATIME)
  728. di_flags |= XFS_DIFLAG_NOATIME;
  729. if (vap->va_xflags & XFS_XFLAG_NODUMP)
  730. di_flags |= XFS_DIFLAG_NODUMP;
  731. if (vap->va_xflags & XFS_XFLAG_PROJINHERIT)
  732. di_flags |= XFS_DIFLAG_PROJINHERIT;
  733. if (vap->va_xflags & XFS_XFLAG_NODEFRAG)
  734. di_flags |= XFS_DIFLAG_NODEFRAG;
  735. if (vap->va_xflags & XFS_XFLAG_FILESTREAM)
  736. di_flags |= XFS_DIFLAG_FILESTREAM;
  737. if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
  738. if (vap->va_xflags & XFS_XFLAG_RTINHERIT)
  739. di_flags |= XFS_DIFLAG_RTINHERIT;
  740. if (vap->va_xflags & XFS_XFLAG_NOSYMLINKS)
  741. di_flags |= XFS_DIFLAG_NOSYMLINKS;
  742. if (vap->va_xflags & XFS_XFLAG_EXTSZINHERIT)
  743. di_flags |= XFS_DIFLAG_EXTSZINHERIT;
  744. } else if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
  745. if (vap->va_xflags & XFS_XFLAG_REALTIME)
  746. di_flags |= XFS_DIFLAG_REALTIME;
  747. if (vap->va_xflags & XFS_XFLAG_EXTSIZE)
  748. di_flags |= XFS_DIFLAG_EXTSIZE;
  749. }
  750. ip->i_d.di_flags = di_flags;
  751. }
  752. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  753. timeflags |= XFS_ICHGTIME_CHG;
  754. }
  755. /*
  756. * Change file inode change time only if XFS_AT_CTIME set
  757. * AND we have been called by a DMI function.
  758. */
  759. if ( (flags & ATTR_DMI) && (mask & XFS_AT_CTIME) ) {
  760. ip->i_d.di_ctime.t_sec = vap->va_ctime.tv_sec;
  761. ip->i_d.di_ctime.t_nsec = vap->va_ctime.tv_nsec;
  762. ip->i_update_core = 1;
  763. timeflags &= ~XFS_ICHGTIME_CHG;
  764. }
  765. /*
  766. * Send out timestamp changes that need to be set to the
  767. * current time. Not done when called by a DMI function.
  768. */
  769. if (timeflags && !(flags & ATTR_DMI))
  770. xfs_ichgtime(ip, timeflags);
  771. XFS_STATS_INC(xs_ig_attrchg);
  772. /*
  773. * If this is a synchronous mount, make sure that the
  774. * transaction goes to disk before returning to the user.
  775. * This is slightly sub-optimal in that truncates require
  776. * two sync transactions instead of one for wsync filesystems.
  777. * One for the truncate and one for the timestamps since we
  778. * don't want to change the timestamps unless we're sure the
  779. * truncate worked. Truncates are less than 1% of the laddis
  780. * mix so this probably isn't worth the trouble to optimize.
  781. */
  782. code = 0;
  783. if (tp) {
  784. if (mp->m_flags & XFS_MOUNT_WSYNC)
  785. xfs_trans_set_sync(tp);
  786. code = xfs_trans_commit(tp, commit_flags);
  787. }
  788. /*
  789. * If the (regular) file's mandatory locking mode changed, then
  790. * notify the vnode. We do this under the inode lock to prevent
  791. * racing calls to vop_vnode_change.
  792. */
  793. mandlock_after = MANDLOCK(vp, ip->i_d.di_mode);
  794. xfs_iunlock(ip, lock_flags);
  795. /*
  796. * Release any dquot(s) the inode had kept before chown.
  797. */
  798. XFS_QM_DQRELE(mp, olddquot1);
  799. XFS_QM_DQRELE(mp, olddquot2);
  800. XFS_QM_DQRELE(mp, udqp);
  801. XFS_QM_DQRELE(mp, gdqp);
  802. if (code) {
  803. return code;
  804. }
  805. if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE) &&
  806. !(flags & ATTR_DMI)) {
  807. (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, vp, DM_RIGHT_NULL,
  808. NULL, DM_RIGHT_NULL, NULL, NULL,
  809. 0, 0, AT_DELAY_FLAG(flags));
  810. }
  811. return 0;
  812. abort_return:
  813. commit_flags |= XFS_TRANS_ABORT;
  814. /* FALLTHROUGH */
  815. error_return:
  816. XFS_QM_DQRELE(mp, udqp);
  817. XFS_QM_DQRELE(mp, gdqp);
  818. if (tp) {
  819. xfs_trans_cancel(tp, commit_flags);
  820. }
  821. if (lock_flags != 0) {
  822. xfs_iunlock(ip, lock_flags);
  823. }
  824. return code;
  825. }
  826. /*
  827. * xfs_access
  828. * Null conversion from vnode mode bits to inode mode bits, as in efs.
  829. */
  830. int
  831. xfs_access(
  832. xfs_inode_t *ip,
  833. int mode,
  834. cred_t *credp)
  835. {
  836. int error;
  837. xfs_itrace_entry(ip);
  838. xfs_ilock(ip, XFS_ILOCK_SHARED);
  839. error = xfs_iaccess(ip, mode, credp);
  840. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  841. return error;
  842. }
  843. /*
  844. * The maximum pathlen is 1024 bytes. Since the minimum file system
  845. * blocksize is 512 bytes, we can get a max of 2 extents back from
  846. * bmapi.
  847. */
  848. #define SYMLINK_MAPS 2
  849. STATIC int
  850. xfs_readlink_bmap(
  851. xfs_inode_t *ip,
  852. char *link)
  853. {
  854. xfs_mount_t *mp = ip->i_mount;
  855. int pathlen = ip->i_d.di_size;
  856. int nmaps = SYMLINK_MAPS;
  857. xfs_bmbt_irec_t mval[SYMLINK_MAPS];
  858. xfs_daddr_t d;
  859. int byte_cnt;
  860. int n;
  861. xfs_buf_t *bp;
  862. int error = 0;
  863. error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
  864. mval, &nmaps, NULL, NULL);
  865. if (error)
  866. goto out;
  867. for (n = 0; n < nmaps; n++) {
  868. d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
  869. byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
  870. bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0);
  871. error = XFS_BUF_GETERROR(bp);
  872. if (error) {
  873. xfs_ioerror_alert("xfs_readlink",
  874. ip->i_mount, bp, XFS_BUF_ADDR(bp));
  875. xfs_buf_relse(bp);
  876. goto out;
  877. }
  878. if (pathlen < byte_cnt)
  879. byte_cnt = pathlen;
  880. pathlen -= byte_cnt;
  881. memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
  882. xfs_buf_relse(bp);
  883. }
  884. link[ip->i_d.di_size] = '\0';
  885. error = 0;
  886. out:
  887. return error;
  888. }
  889. int
  890. xfs_readlink(
  891. xfs_inode_t *ip,
  892. char *link)
  893. {
  894. xfs_mount_t *mp = ip->i_mount;
  895. int pathlen;
  896. int error = 0;
  897. xfs_itrace_entry(ip);
  898. if (XFS_FORCED_SHUTDOWN(mp))
  899. return XFS_ERROR(EIO);
  900. xfs_ilock(ip, XFS_ILOCK_SHARED);
  901. ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
  902. ASSERT(ip->i_d.di_size <= MAXPATHLEN);
  903. pathlen = ip->i_d.di_size;
  904. if (!pathlen)
  905. goto out;
  906. if (ip->i_df.if_flags & XFS_IFINLINE) {
  907. memcpy(link, ip->i_df.if_u1.if_data, pathlen);
  908. link[pathlen] = '\0';
  909. } else {
  910. error = xfs_readlink_bmap(ip, link);
  911. }
  912. out:
  913. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  914. return error;
  915. }
  916. /*
  917. * xfs_fsync
  918. *
  919. * This is called to sync the inode and its data out to disk.
  920. * We need to hold the I/O lock while flushing the data, and
  921. * the inode lock while flushing the inode. The inode lock CANNOT
  922. * be held while flushing the data, so acquire after we're done
  923. * with that.
  924. */
  925. int
  926. xfs_fsync(
  927. xfs_inode_t *ip,
  928. int flag,
  929. xfs_off_t start,
  930. xfs_off_t stop)
  931. {
  932. xfs_trans_t *tp;
  933. int error;
  934. int log_flushed = 0, changed = 1;
  935. xfs_itrace_entry(ip);
  936. ASSERT(start >= 0 && stop >= -1);
  937. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  938. return XFS_ERROR(EIO);
  939. if (flag & FSYNC_DATA)
  940. filemap_fdatawait(vn_to_inode(XFS_ITOV(ip))->i_mapping);
  941. /*
  942. * We always need to make sure that the required inode state
  943. * is safe on disk. The vnode might be clean but because
  944. * of committed transactions that haven't hit the disk yet.
  945. * Likewise, there could be unflushed non-transactional
  946. * changes to the inode core that have to go to disk.
  947. *
  948. * The following code depends on one assumption: that
  949. * any transaction that changes an inode logs the core
  950. * because it has to change some field in the inode core
  951. * (typically nextents or nblocks). That assumption
  952. * implies that any transactions against an inode will
  953. * catch any non-transactional updates. If inode-altering
  954. * transactions exist that violate this assumption, the
  955. * code breaks. Right now, it figures that if the involved
  956. * update_* field is clear and the inode is unpinned, the
  957. * inode is clean. Either it's been flushed or it's been
  958. * committed and the commit has hit the disk unpinning the inode.
  959. * (Note that xfs_inode_item_format() called at commit clears
  960. * the update_* fields.)
  961. */
  962. xfs_ilock(ip, XFS_ILOCK_SHARED);
  963. /* If we are flushing data then we care about update_size
  964. * being set, otherwise we care about update_core
  965. */
  966. if ((flag & FSYNC_DATA) ?
  967. (ip->i_update_size == 0) :
  968. (ip->i_update_core == 0)) {
  969. /*
  970. * Timestamps/size haven't changed since last inode
  971. * flush or inode transaction commit. That means
  972. * either nothing got written or a transaction
  973. * committed which caught the updates. If the
  974. * latter happened and the transaction hasn't
  975. * hit the disk yet, the inode will be still
  976. * be pinned. If it is, force the log.
  977. */
  978. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  979. if (xfs_ipincount(ip)) {
  980. _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
  981. XFS_LOG_FORCE |
  982. ((flag & FSYNC_WAIT)
  983. ? XFS_LOG_SYNC : 0),
  984. &log_flushed);
  985. } else {
  986. /*
  987. * If the inode is not pinned and nothing
  988. * has changed we don't need to flush the
  989. * cache.
  990. */
  991. changed = 0;
  992. }
  993. error = 0;
  994. } else {
  995. /*
  996. * Kick off a transaction to log the inode
  997. * core to get the updates. Make it
  998. * sync if FSYNC_WAIT is passed in (which
  999. * is done by everybody but specfs). The
  1000. * sync transaction will also force the log.
  1001. */
  1002. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  1003. tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
  1004. if ((error = xfs_trans_reserve(tp, 0,
  1005. XFS_FSYNC_TS_LOG_RES(ip->i_mount),
  1006. 0, 0, 0))) {
  1007. xfs_trans_cancel(tp, 0);
  1008. return error;
  1009. }
  1010. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1011. /*
  1012. * Note - it's possible that we might have pushed
  1013. * ourselves out of the way during trans_reserve
  1014. * which would flush the inode. But there's no
  1015. * guarantee that the inode buffer has actually
  1016. * gone out yet (it's delwri). Plus the buffer
  1017. * could be pinned anyway if it's part of an
  1018. * inode in another recent transaction. So we
  1019. * play it safe and fire off the transaction anyway.
  1020. */
  1021. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  1022. xfs_trans_ihold(tp, ip);
  1023. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1024. if (flag & FSYNC_WAIT)
  1025. xfs_trans_set_sync(tp);
  1026. error = _xfs_trans_commit(tp, 0, &log_flushed);
  1027. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1028. }
  1029. if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
  1030. /*
  1031. * If the log write didn't issue an ordered tag we need
  1032. * to flush the disk cache for the data device now.
  1033. */
  1034. if (!log_flushed)
  1035. xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
  1036. /*
  1037. * If this inode is on the RT dev we need to flush that
  1038. * cache as well.
  1039. */
  1040. if (XFS_IS_REALTIME_INODE(ip))
  1041. xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
  1042. }
  1043. return error;
  1044. }
  1045. /*
  1046. * This is called by xfs_inactive to free any blocks beyond eof
  1047. * when the link count isn't zero and by xfs_dm_punch_hole() when
  1048. * punching a hole to EOF.
  1049. */
  1050. int
  1051. xfs_free_eofblocks(
  1052. xfs_mount_t *mp,
  1053. xfs_inode_t *ip,
  1054. int flags)
  1055. {
  1056. xfs_trans_t *tp;
  1057. int error;
  1058. xfs_fileoff_t end_fsb;
  1059. xfs_fileoff_t last_fsb;
  1060. xfs_filblks_t map_len;
  1061. int nimaps;
  1062. xfs_bmbt_irec_t imap;
  1063. int use_iolock = (flags & XFS_FREE_EOF_LOCK);
  1064. /*
  1065. * Figure out if there are any blocks beyond the end
  1066. * of the file. If not, then there is nothing to do.
  1067. */
  1068. end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
  1069. last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
  1070. map_len = last_fsb - end_fsb;
  1071. if (map_len <= 0)
  1072. return 0;
  1073. nimaps = 1;
  1074. xfs_ilock(ip, XFS_ILOCK_SHARED);
  1075. error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
  1076. NULL, 0, &imap, &nimaps, NULL, NULL);
  1077. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  1078. if (!error && (nimaps != 0) &&
  1079. (imap.br_startblock != HOLESTARTBLOCK ||
  1080. ip->i_delayed_blks)) {
  1081. /*
  1082. * Attach the dquots to the inode up front.
  1083. */
  1084. if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
  1085. return error;
  1086. /*
  1087. * There are blocks after the end of file.
  1088. * Free them up now by truncating the file to
  1089. * its current size.
  1090. */
  1091. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  1092. /*
  1093. * Do the xfs_itruncate_start() call before
  1094. * reserving any log space because
  1095. * itruncate_start will call into the buffer
  1096. * cache and we can't
  1097. * do that within a transaction.
  1098. */
  1099. if (use_iolock)
  1100. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  1101. error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
  1102. ip->i_size);
  1103. if (error) {
  1104. xfs_trans_cancel(tp, 0);
  1105. if (use_iolock)
  1106. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1107. return error;
  1108. }
  1109. error = xfs_trans_reserve(tp, 0,
  1110. XFS_ITRUNCATE_LOG_RES(mp),
  1111. 0, XFS_TRANS_PERM_LOG_RES,
  1112. XFS_ITRUNCATE_LOG_COUNT);
  1113. if (error) {
  1114. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1115. xfs_trans_cancel(tp, 0);
  1116. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1117. return error;
  1118. }
  1119. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1120. xfs_trans_ijoin(tp, ip,
  1121. XFS_IOLOCK_EXCL |
  1122. XFS_ILOCK_EXCL);
  1123. xfs_trans_ihold(tp, ip);
  1124. error = xfs_itruncate_finish(&tp, ip,
  1125. ip->i_size,
  1126. XFS_DATA_FORK,
  1127. 0);
  1128. /*
  1129. * If we get an error at this point we
  1130. * simply don't bother truncating the file.
  1131. */
  1132. if (error) {
  1133. xfs_trans_cancel(tp,
  1134. (XFS_TRANS_RELEASE_LOG_RES |
  1135. XFS_TRANS_ABORT));
  1136. } else {
  1137. error = xfs_trans_commit(tp,
  1138. XFS_TRANS_RELEASE_LOG_RES);
  1139. }
  1140. xfs_iunlock(ip, (use_iolock ? (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)
  1141. : XFS_ILOCK_EXCL));
  1142. }
  1143. return error;
  1144. }
  1145. /*
  1146. * Free a symlink that has blocks associated with it.
  1147. */
  1148. STATIC int
  1149. xfs_inactive_symlink_rmt(
  1150. xfs_inode_t *ip,
  1151. xfs_trans_t **tpp)
  1152. {
  1153. xfs_buf_t *bp;
  1154. int committed;
  1155. int done;
  1156. int error;
  1157. xfs_fsblock_t first_block;
  1158. xfs_bmap_free_t free_list;
  1159. int i;
  1160. xfs_mount_t *mp;
  1161. xfs_bmbt_irec_t mval[SYMLINK_MAPS];
  1162. int nmaps;
  1163. xfs_trans_t *ntp;
  1164. int size;
  1165. xfs_trans_t *tp;
  1166. tp = *tpp;
  1167. mp = ip->i_mount;
  1168. ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
  1169. /*
  1170. * We're freeing a symlink that has some
  1171. * blocks allocated to it. Free the
  1172. * blocks here. We know that we've got
  1173. * either 1 or 2 extents and that we can
  1174. * free them all in one bunmapi call.
  1175. */
  1176. ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
  1177. if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
  1178. XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
  1179. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1180. xfs_trans_cancel(tp, 0);
  1181. *tpp = NULL;
  1182. return error;
  1183. }
  1184. /*
  1185. * Lock the inode, fix the size, and join it to the transaction.
  1186. * Hold it so in the normal path, we still have it locked for
  1187. * the second transaction. In the error paths we need it
  1188. * held so the cancel won't rele it, see below.
  1189. */
  1190. xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1191. size = (int)ip->i_d.di_size;
  1192. ip->i_d.di_size = 0;
  1193. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  1194. xfs_trans_ihold(tp, ip);
  1195. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1196. /*
  1197. * Find the block(s) so we can inval and unmap them.
  1198. */
  1199. done = 0;
  1200. XFS_BMAP_INIT(&free_list, &first_block);
  1201. nmaps = ARRAY_SIZE(mval);
  1202. if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
  1203. XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
  1204. &free_list, NULL)))
  1205. goto error0;
  1206. /*
  1207. * Invalidate the block(s).
  1208. */
  1209. for (i = 0; i < nmaps; i++) {
  1210. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
  1211. XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
  1212. XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
  1213. xfs_trans_binval(tp, bp);
  1214. }
  1215. /*
  1216. * Unmap the dead block(s) to the free_list.
  1217. */
  1218. if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
  1219. &first_block, &free_list, NULL, &done)))
  1220. goto error1;
  1221. ASSERT(done);
  1222. /*
  1223. * Commit the first transaction. This logs the EFI and the inode.
  1224. */
  1225. if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
  1226. goto error1;
  1227. /*
  1228. * The transaction must have been committed, since there were
  1229. * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
  1230. * The new tp has the extent freeing and EFDs.
  1231. */
  1232. ASSERT(committed);
  1233. /*
  1234. * The first xact was committed, so add the inode to the new one.
  1235. * Mark it dirty so it will be logged and moved forward in the log as
  1236. * part of every commit.
  1237. */
  1238. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  1239. xfs_trans_ihold(tp, ip);
  1240. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1241. /*
  1242. * Get a new, empty transaction to return to our caller.
  1243. */
  1244. ntp = xfs_trans_dup(tp);
  1245. /*
  1246. * Commit the transaction containing extent freeing and EFDs.
  1247. * If we get an error on the commit here or on the reserve below,
  1248. * we need to unlock the inode since the new transaction doesn't
  1249. * have the inode attached.
  1250. */
  1251. error = xfs_trans_commit(tp, 0);
  1252. tp = ntp;
  1253. if (error) {
  1254. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1255. goto error0;
  1256. }
  1257. /*
  1258. * Remove the memory for extent descriptions (just bookkeeping).
  1259. */
  1260. if (ip->i_df.if_bytes)
  1261. xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
  1262. ASSERT(ip->i_df.if_bytes == 0);
  1263. /*
  1264. * Put an itruncate log reservation in the new transaction
  1265. * for our caller.
  1266. */
  1267. if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
  1268. XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
  1269. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1270. goto error0;
  1271. }
  1272. /*
  1273. * Return with the inode locked but not joined to the transaction.
  1274. */
  1275. *tpp = tp;
  1276. return 0;
  1277. error1:
  1278. xfs_bmap_cancel(&free_list);
  1279. error0:
  1280. /*
  1281. * Have to come here with the inode locked and either
  1282. * (held and in the transaction) or (not in the transaction).
  1283. * If the inode isn't held then cancel would iput it, but
  1284. * that's wrong since this is inactive and the vnode ref
  1285. * count is 0 already.
  1286. * Cancel won't do anything to the inode if held, but it still
  1287. * needs to be locked until the cancel is done, if it was
  1288. * joined to the transaction.
  1289. */
  1290. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  1291. xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1292. *tpp = NULL;
  1293. return error;
  1294. }
  1295. STATIC int
  1296. xfs_inactive_symlink_local(
  1297. xfs_inode_t *ip,
  1298. xfs_trans_t **tpp)
  1299. {
  1300. int error;
  1301. ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
  1302. /*
  1303. * We're freeing a symlink which fit into
  1304. * the inode. Just free the memory used
  1305. * to hold the old symlink.
  1306. */
  1307. error = xfs_trans_reserve(*tpp, 0,
  1308. XFS_ITRUNCATE_LOG_RES(ip->i_mount),
  1309. 0, XFS_TRANS_PERM_LOG_RES,
  1310. XFS_ITRUNCATE_LOG_COUNT);
  1311. if (error) {
  1312. xfs_trans_cancel(*tpp, 0);
  1313. *tpp = NULL;
  1314. return error;
  1315. }
  1316. xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  1317. /*
  1318. * Zero length symlinks _can_ exist.
  1319. */
  1320. if (ip->i_df.if_bytes > 0) {
  1321. xfs_idata_realloc(ip,
  1322. -(ip->i_df.if_bytes),
  1323. XFS_DATA_FORK);
  1324. ASSERT(ip->i_df.if_bytes == 0);
  1325. }
  1326. return 0;
  1327. }
  1328. STATIC int
  1329. xfs_inactive_attrs(
  1330. xfs_inode_t *ip,
  1331. xfs_trans_t **tpp)
  1332. {
  1333. xfs_trans_t *tp;
  1334. int error;
  1335. xfs_mount_t *mp;
  1336. ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
  1337. tp = *tpp;
  1338. mp = ip->i_mount;
  1339. ASSERT(ip->i_d.di_forkoff != 0);
  1340. xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1341. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1342. error = xfs_attr_inactive(ip);
  1343. if (error) {
  1344. *tpp = NULL;
  1345. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1346. return error; /* goto out */
  1347. }
  1348. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  1349. error = xfs_trans_reserve(tp, 0,
  1350. XFS_IFREE_LOG_RES(mp),
  1351. 0, XFS_TRANS_PERM_LOG_RES,
  1352. XFS_INACTIVE_LOG_COUNT);
  1353. if (error) {
  1354. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1355. xfs_trans_cancel(tp, 0);
  1356. *tpp = NULL;
  1357. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1358. return error;
  1359. }
  1360. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1361. xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1362. xfs_trans_ihold(tp, ip);
  1363. xfs_idestroy_fork(ip, XFS_ATTR_FORK);
  1364. ASSERT(ip->i_d.di_anextents == 0);
  1365. *tpp = tp;
  1366. return 0;
  1367. }
  1368. int
  1369. xfs_release(
  1370. xfs_inode_t *ip)
  1371. {
  1372. bhv_vnode_t *vp = XFS_ITOV(ip);
  1373. xfs_mount_t *mp = ip->i_mount;
  1374. int error;
  1375. if (!VN_ISREG(vp) || (ip->i_d.di_mode == 0))
  1376. return 0;
  1377. /* If this is a read-only mount, don't do this (would generate I/O) */
  1378. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1379. return 0;
  1380. if (!XFS_FORCED_SHUTDOWN(mp)) {
  1381. int truncated;
  1382. /*
  1383. * If we are using filestreams, and we have an unlinked
  1384. * file that we are processing the last close on, then nothing
  1385. * will be able to reopen and write to this file. Purge this
  1386. * inode from the filestreams cache so that it doesn't delay
  1387. * teardown of the inode.
  1388. */
  1389. if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
  1390. xfs_filestream_deassociate(ip);
  1391. /*
  1392. * If we previously truncated this file and removed old data
  1393. * in the process, we want to initiate "early" writeout on
  1394. * the last close. This is an attempt to combat the notorious
  1395. * NULL files problem which is particularly noticable from a
  1396. * truncate down, buffered (re-)write (delalloc), followed by
  1397. * a crash. What we are effectively doing here is
  1398. * significantly reducing the time window where we'd otherwise
  1399. * be exposed to that problem.
  1400. */
  1401. truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
  1402. if (truncated && VN_DIRTY(vp) && ip->i_delayed_blks > 0)
  1403. xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE);
  1404. }
  1405. #ifdef HAVE_REFCACHE
  1406. /* If we are in the NFS reference cache then don't do this now */
  1407. if (ip->i_refcache)
  1408. return 0;
  1409. #endif
  1410. if (ip->i_d.di_nlink != 0) {
  1411. if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
  1412. ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
  1413. ip->i_delayed_blks > 0)) &&
  1414. (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
  1415. (!(ip->i_d.di_flags &
  1416. (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
  1417. error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
  1418. if (error)
  1419. return error;
  1420. /* Update linux inode block count after free above */
  1421. vn_to_inode(vp)->i_blocks = XFS_FSB_TO_BB(mp,
  1422. ip->i_d.di_nblocks + ip->i_delayed_blks);
  1423. }
  1424. }
  1425. return 0;
  1426. }
  1427. /*
  1428. * xfs_inactive
  1429. *
  1430. * This is called when the vnode reference count for the vnode
  1431. * goes to zero. If the file has been unlinked, then it must
  1432. * now be truncated. Also, we clear all of the read-ahead state
  1433. * kept for the inode here since the file is now closed.
  1434. */
  1435. int
  1436. xfs_inactive(
  1437. xfs_inode_t *ip)
  1438. {
  1439. bhv_vnode_t *vp = XFS_ITOV(ip);
  1440. xfs_bmap_free_t free_list;
  1441. xfs_fsblock_t first_block;
  1442. int committed;
  1443. xfs_trans_t *tp;
  1444. xfs_mount_t *mp;
  1445. int error;
  1446. int truncate;
  1447. xfs_itrace_entry(ip);
  1448. /*
  1449. * If the inode is already free, then there can be nothing
  1450. * to clean up here.
  1451. */
  1452. if (ip->i_d.di_mode == 0 || VN_BAD(vp)) {
  1453. ASSERT(ip->i_df.if_real_bytes == 0);
  1454. ASSERT(ip->i_df.if_broot_bytes == 0);
  1455. return VN_INACTIVE_CACHE;
  1456. }
  1457. /*
  1458. * Only do a truncate if it's a regular file with
  1459. * some actual space in it. It's OK to look at the
  1460. * inode's fields without the lock because we're the
  1461. * only one with a reference to the inode.
  1462. */
  1463. truncate = ((ip->i_d.di_nlink == 0) &&
  1464. ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
  1465. (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
  1466. ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
  1467. mp = ip->i_mount;
  1468. if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY)) {
  1469. (void) XFS_SEND_DESTROY(mp, vp, DM_RIGHT_NULL);
  1470. }
  1471. error = 0;
  1472. /* If this is a read-only mount, don't do this (would generate I/O) */
  1473. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1474. goto out;
  1475. if (ip->i_d.di_nlink != 0) {
  1476. if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
  1477. ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
  1478. ip->i_delayed_blks > 0)) &&
  1479. (ip->i_df.if_flags & XFS_IFEXTENTS) &&
  1480. (!(ip->i_d.di_flags &
  1481. (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
  1482. (ip->i_delayed_blks != 0)))) {
  1483. error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
  1484. if (error)
  1485. return VN_INACTIVE_CACHE;
  1486. /* Update linux inode block count after free above */
  1487. vn_to_inode(vp)->i_blocks = XFS_FSB_TO_BB(mp,
  1488. ip->i_d.di_nblocks + ip->i_delayed_blks);
  1489. }
  1490. goto out;
  1491. }
  1492. ASSERT(ip->i_d.di_nlink == 0);
  1493. if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
  1494. return VN_INACTIVE_CACHE;
  1495. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  1496. if (truncate) {
  1497. /*
  1498. * Do the xfs_itruncate_start() call before
  1499. * reserving any log space because itruncate_start
  1500. * will call into the buffer cache and we can't
  1501. * do that within a transaction.
  1502. */
  1503. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  1504. error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
  1505. if (error) {
  1506. xfs_trans_cancel(tp, 0);
  1507. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1508. return VN_INACTIVE_CACHE;
  1509. }
  1510. error = xfs_trans_reserve(tp, 0,
  1511. XFS_ITRUNCATE_LOG_RES(mp),
  1512. 0, XFS_TRANS_PERM_LOG_RES,
  1513. XFS_ITRUNCATE_LOG_COUNT);
  1514. if (error) {
  1515. /* Don't call itruncate_cleanup */
  1516. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1517. xfs_trans_cancel(tp, 0);
  1518. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1519. return VN_INACTIVE_CACHE;
  1520. }
  1521. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1522. xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1523. xfs_trans_ihold(tp, ip);
  1524. /*
  1525. * normally, we have to run xfs_itruncate_finish sync.
  1526. * But if filesystem is wsync and we're in the inactive
  1527. * path, then we know that nlink == 0, and that the
  1528. * xaction that made nlink == 0 is permanently committed
  1529. * since xfs_remove runs as a synchronous transaction.
  1530. */
  1531. error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
  1532. (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
  1533. if (error) {
  1534. xfs_trans_cancel(tp,
  1535. XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  1536. xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1537. return VN_INACTIVE_CACHE;
  1538. }
  1539. } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
  1540. /*
  1541. * If we get an error while cleaning up a
  1542. * symlink we bail out.
  1543. */
  1544. error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
  1545. xfs_inactive_symlink_rmt(ip, &tp) :
  1546. xfs_inactive_symlink_local(ip, &tp);
  1547. if (error) {
  1548. ASSERT(tp == NULL);
  1549. return VN_INACTIVE_CACHE;
  1550. }
  1551. xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1552. xfs_trans_ihold(tp, ip);
  1553. } else {
  1554. error = xfs_trans_reserve(tp, 0,
  1555. XFS_IFREE_LOG_RES(mp),
  1556. 0, XFS_TRANS_PERM_LOG_RES,
  1557. XFS_INACTIVE_LOG_COUNT);
  1558. if (error) {
  1559. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1560. xfs_trans_cancel(tp, 0);
  1561. return VN_INACTIVE_CACHE;
  1562. }
  1563. xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  1564. xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1565. xfs_trans_ihold(tp, ip);
  1566. }
  1567. /*
  1568. * If there are attributes associated with the file
  1569. * then blow them away now. The code calls a routine
  1570. * that recursively deconstructs the attribute fork.
  1571. * We need to just commit the current transaction
  1572. * because we can't use it for xfs_attr_inactive().
  1573. */
  1574. if (ip->i_d.di_anextents > 0) {
  1575. error = xfs_inactive_attrs(ip, &tp);
  1576. /*
  1577. * If we got an error, the transaction is already
  1578. * cancelled, and the inode is unlocked. Just get out.
  1579. */
  1580. if (error)
  1581. return VN_INACTIVE_CACHE;
  1582. } else if (ip->i_afp) {
  1583. xfs_idestroy_fork(ip, XFS_ATTR_FORK);
  1584. }
  1585. /*
  1586. * Free the inode.
  1587. */
  1588. XFS_BMAP_INIT(&free_list, &first_block);
  1589. error = xfs_ifree(tp, ip, &free_list);
  1590. if (error) {
  1591. /*
  1592. * If we fail to free the inode, shut down. The cancel
  1593. * might do that, we need to make sure. Otherwise the
  1594. * inode might be lost for a long time or forever.
  1595. */
  1596. if (!XFS_FORCED_SHUTDOWN(mp)) {
  1597. cmn_err(CE_NOTE,
  1598. "xfs_inactive: xfs_ifree() returned an error = %d on %s",
  1599. error, mp->m_fsname);
  1600. xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
  1601. }
  1602. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  1603. } else {
  1604. /*
  1605. * Credit the quota account(s). The inode is gone.
  1606. */
  1607. XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
  1608. /*
  1609. * Just ignore errors at this point. There is
  1610. * nothing we can do except to try to keep going.
  1611. */
  1612. (void) xfs_bmap_finish(&tp, &free_list, &committed);
  1613. (void) xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1614. }
  1615. /*
  1616. * Release the dquots held by inode, if any.
  1617. */
  1618. XFS_QM_DQDETACH(mp, ip);
  1619. xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1620. out:
  1621. return VN_INACTIVE_CACHE;
  1622. }
  1623. int
  1624. xfs_lookup(
  1625. xfs_inode_t *dp,
  1626. bhv_vname_t *dentry,
  1627. bhv_vnode_t **vpp)
  1628. {
  1629. xfs_inode_t *ip;
  1630. xfs_ino_t e_inum;
  1631. int error;
  1632. uint lock_mode;
  1633. xfs_itrace_entry(dp);
  1634. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  1635. return XFS_ERROR(EIO);
  1636. lock_mode = xfs_ilock_map_shared(dp);
  1637. error = xfs_dir_lookup_int(dp, lock_mode, dentry, &e_inum, &ip);
  1638. if (!error) {
  1639. *vpp = XFS_ITOV(ip);
  1640. xfs_itrace_ref(ip);
  1641. }
  1642. xfs_iunlock_map_shared(dp, lock_mode);
  1643. return error;
  1644. }
  1645. int
  1646. xfs_create(
  1647. xfs_inode_t *dp,
  1648. bhv_vname_t *dentry,
  1649. mode_t mode,
  1650. xfs_dev_t rdev,
  1651. bhv_vnode_t **vpp,
  1652. cred_t *credp)
  1653. {
  1654. char *name = VNAME(dentry);
  1655. xfs_mount_t *mp = dp->i_mount;
  1656. bhv_vnode_t *dir_vp = XFS_ITOV(dp);
  1657. xfs_inode_t *ip;
  1658. bhv_vnode_t *vp = NULL;
  1659. xfs_trans_t *tp;
  1660. int error;
  1661. xfs_bmap_free_t free_list;
  1662. xfs_fsblock_t first_block;
  1663. boolean_t unlock_dp_on_error = B_FALSE;
  1664. int dm_event_sent = 0;
  1665. uint cancel_flags;
  1666. int committed;
  1667. xfs_prid_t prid;
  1668. struct xfs_dquot *udqp, *gdqp;
  1669. uint resblks;
  1670. int namelen;
  1671. ASSERT(!*vpp);
  1672. xfs_itrace_entry(dp);
  1673. namelen = VNAMELEN(dentry);
  1674. if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
  1675. error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
  1676. dir_vp, DM_RIGHT_NULL, NULL,
  1677. DM_RIGHT_NULL, name, NULL,
  1678. mode, 0, 0);
  1679. if (error)
  1680. return error;
  1681. dm_event_sent = 1;
  1682. }
  1683. if (XFS_FORCED_SHUTDOWN(mp))
  1684. return XFS_ERROR(EIO);
  1685. /* Return through std_return after this point. */
  1686. udqp = gdqp = NULL;
  1687. if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  1688. prid = dp->i_d.di_projid;
  1689. else
  1690. prid = (xfs_prid_t)dfltprid;
  1691. /*
  1692. * Make sure that we have allocated dquot(s) on disk.
  1693. */
  1694. error = XFS_QM_DQVOPALLOC(mp, dp,
  1695. current_fsuid(credp), current_fsgid(credp), prid,
  1696. XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
  1697. if (error)
  1698. goto std_return;
  1699. ip = NULL;
  1700. tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
  1701. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  1702. resblks = XFS_CREATE_SPACE_RES(mp, namelen);
  1703. /*
  1704. * Initially assume that the file does not exist and
  1705. * reserve the resources for that case. If that is not
  1706. * the case we'll drop the one we have and get a more
  1707. * appropriate transaction later.
  1708. */
  1709. error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
  1710. XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
  1711. if (error == ENOSPC) {
  1712. resblks = 0;
  1713. error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
  1714. XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
  1715. }
  1716. if (error) {
  1717. cancel_flags = 0;
  1718. goto error_return;
  1719. }
  1720. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  1721. unlock_dp_on_error = B_TRUE;
  1722. XFS_BMAP_INIT(&free_list, &first_block);
  1723. ASSERT(ip == NULL);
  1724. /*
  1725. * Reserve disk quota and the inode.
  1726. */
  1727. error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
  1728. if (error)
  1729. goto error_return;
  1730. if (resblks == 0 && (error = xfs_dir_canenter(tp, dp, name, namelen)))
  1731. goto error_return;
  1732. error = xfs_dir_ialloc(&tp, dp, mode, 1,
  1733. rdev, credp, prid, resblks > 0,
  1734. &ip, &committed);
  1735. if (error) {
  1736. if (error == ENOSPC)
  1737. goto error_return;
  1738. goto abort_return;
  1739. }
  1740. xfs_itrace_ref(ip);
  1741. /*
  1742. * At this point, we've gotten a newly allocated inode.
  1743. * It is locked (and joined to the transaction).
  1744. */
  1745. ASSERT(ismrlocked (&ip->i_lock, MR_UPDATE));
  1746. /*
  1747. * Now we join the directory inode to the transaction. We do not do it
  1748. * earlier because xfs_dir_ialloc might commit the previous transaction
  1749. * (and release all the locks). An error from here on will result in
  1750. * the transaction cancel unlocking dp so don't do it explicitly in the
  1751. * error path.
  1752. */
  1753. VN_HOLD(dir_vp);
  1754. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  1755. unlock_dp_on_error = B_FALSE;
  1756. error = xfs_dir_createname(tp, dp, name, namelen, ip->i_ino,
  1757. &first_block, &free_list, resblks ?
  1758. resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
  1759. if (error) {
  1760. ASSERT(error != ENOSPC);
  1761. goto abort_return;
  1762. }
  1763. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1764. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  1765. /*
  1766. * If this is a synchronous mount, make sure that the
  1767. * create transaction goes to disk before returning to
  1768. * the user.
  1769. */
  1770. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  1771. xfs_trans_set_sync(tp);
  1772. }
  1773. dp->i_gen++;
  1774. /*
  1775. * Attach the dquot(s) to the inodes and modify them incore.
  1776. * These ids of the inode couldn't have changed since the new
  1777. * inode has been locked ever since it was created.
  1778. */
  1779. XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
  1780. /*
  1781. * xfs_trans_commit normally decrements the vnode ref count
  1782. * when it unlocks the inode. Since we want to return the
  1783. * vnode to the caller, we bump the vnode ref count now.
  1784. */
  1785. IHOLD(ip);
  1786. vp = XFS_ITOV(ip);
  1787. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1788. if (error) {
  1789. xfs_bmap_cancel(&free_list);
  1790. goto abort_rele;
  1791. }
  1792. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1793. if (error) {
  1794. IRELE(ip);
  1795. tp = NULL;
  1796. goto error_return;
  1797. }
  1798. XFS_QM_DQRELE(mp, udqp);
  1799. XFS_QM_DQRELE(mp, gdqp);
  1800. *vpp = vp;
  1801. /* Fallthrough to std_return with error = 0 */
  1802. std_return:
  1803. if ((*vpp || (error != 0 && dm_event_sent != 0)) &&
  1804. DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
  1805. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
  1806. dir_vp, DM_RIGHT_NULL,
  1807. *vpp ? vp:NULL,
  1808. DM_RIGHT_NULL, name, NULL,
  1809. mode, error, 0);
  1810. }
  1811. return error;
  1812. abort_return:
  1813. cancel_flags |= XFS_TRANS_ABORT;
  1814. /* FALLTHROUGH */
  1815. error_return:
  1816. if (tp != NULL)
  1817. xfs_trans_cancel(tp, cancel_flags);
  1818. XFS_QM_DQRELE(mp, udqp);
  1819. XFS_QM_DQRELE(mp, gdqp);
  1820. if (unlock_dp_on_error)
  1821. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  1822. goto std_return;
  1823. abort_rele:
  1824. /*
  1825. * Wait until after the current transaction is aborted to
  1826. * release the inode. This prevents recursive transactions
  1827. * and deadlocks from xfs_inactive.
  1828. */
  1829. cancel_flags |= XFS_TRANS_ABORT;
  1830. xfs_trans_cancel(tp, cancel_flags);
  1831. IRELE(ip);
  1832. XFS_QM_DQRELE(mp, udqp);
  1833. XFS_QM_DQRELE(mp, gdqp);
  1834. goto std_return;
  1835. }
  1836. #ifdef DEBUG
  1837. /*
  1838. * Some counters to see if (and how often) we are hitting some deadlock
  1839. * prevention code paths.
  1840. */
  1841. int xfs_rm_locks;
  1842. int xfs_rm_lock_delays;
  1843. int xfs_rm_attempts;
  1844. #endif
  1845. /*
  1846. * The following routine will lock the inodes associated with the
  1847. * directory and the named entry in the directory. The locks are
  1848. * acquired in increasing inode number.
  1849. *
  1850. * If the entry is "..", then only the directory is locked. The
  1851. * vnode ref count will still include that from the .. entry in
  1852. * this case.
  1853. *
  1854. * There is a deadlock we need to worry about. If the locked directory is
  1855. * in the AIL, it might be blocking up the log. The next inode we lock
  1856. * could be already locked by another thread waiting for log space (e.g
  1857. * a permanent log reservation with a long running transaction (see
  1858. * xfs_itruncate_finish)). To solve this, we must check if the directory
  1859. * is in the ail and use lock_nowait. If we can't lock, we need to
  1860. * drop the inode lock on the directory and try again. xfs_iunlock will
  1861. * potentially push the tail if we were holding up the log.
  1862. */
  1863. STATIC int
  1864. xfs_lock_dir_and_entry(
  1865. xfs_inode_t *dp,
  1866. xfs_inode_t *ip) /* inode of entry 'name' */
  1867. {
  1868. int attempts;
  1869. xfs_ino_t e_inum;
  1870. xfs_inode_t *ips[2];
  1871. xfs_log_item_t *lp;
  1872. #ifdef DEBUG
  1873. xfs_rm_locks++;
  1874. #endif
  1875. attempts = 0;
  1876. again:
  1877. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  1878. e_inum = ip->i_ino;
  1879. xfs_itrace_ref(ip);
  1880. /*
  1881. * We want to lock in increasing inum. Since we've already
  1882. * acquired the lock on the directory, we may need to release
  1883. * if if the inum of the entry turns out to be less.
  1884. */
  1885. if (e_inum > dp->i_ino) {
  1886. /*
  1887. * We are already in the right order, so just
  1888. * lock on the inode of the entry.
  1889. * We need to use nowait if dp is in the AIL.
  1890. */
  1891. lp = (xfs_log_item_t *)dp->i_itemp;
  1892. if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
  1893. if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
  1894. attempts++;
  1895. #ifdef DEBUG
  1896. xfs_rm_attempts++;
  1897. #endif
  1898. /*
  1899. * Unlock dp and try again.
  1900. * xfs_iunlock will try to push the tail
  1901. * if the inode is in the AIL.
  1902. */
  1903. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  1904. if ((attempts % 5) == 0) {
  1905. delay(1); /* Don't just spin the CPU */
  1906. #ifdef DEBUG
  1907. xfs_rm_lock_delays++;
  1908. #endif
  1909. }
  1910. goto again;
  1911. }
  1912. } else {
  1913. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1914. }
  1915. } else if (e_inum < dp->i_ino) {
  1916. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  1917. ips[0] = ip;
  1918. ips[1] = dp;
  1919. xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
  1920. }
  1921. /* else e_inum == dp->i_ino */
  1922. /* This can happen if we're asked to lock /x/..
  1923. * the entry is "..", which is also the parent directory.
  1924. */
  1925. return 0;
  1926. }
  1927. #ifdef DEBUG
  1928. int xfs_locked_n;
  1929. int xfs_small_retries;
  1930. int xfs_middle_retries;
  1931. int xfs_lots_retries;
  1932. int xfs_lock_delays;
  1933. #endif
  1934. /*
  1935. * Bump the subclass so xfs_lock_inodes() acquires each lock with
  1936. * a different value
  1937. */
  1938. static inline int
  1939. xfs_lock_inumorder(int lock_mode, int subclass)
  1940. {
  1941. if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
  1942. lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
  1943. if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
  1944. lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
  1945. return lock_mode;
  1946. }
  1947. /*
  1948. * The following routine will lock n inodes in exclusive mode.
  1949. * We assume the caller calls us with the inodes in i_ino order.
  1950. *
  1951. * We need to detect deadlock where an inode that we lock
  1952. * is in the AIL and we start waiting for another inode that is locked
  1953. * by a thread in a long running transaction (such as truncate). This can
  1954. * result in deadlock since the long running trans might need to wait
  1955. * for the inode we just locked in order to push the tail and free space
  1956. * in the log.
  1957. */
  1958. void
  1959. xfs_lock_inodes(
  1960. xfs_inode_t **ips,
  1961. int inodes,
  1962. int first_locked,
  1963. uint lock_mode)
  1964. {
  1965. int attempts = 0, i, j, try_lock;
  1966. xfs_log_item_t *lp;
  1967. ASSERT(ips && (inodes >= 2)); /* we need at least two */
  1968. if (first_locked) {
  1969. try_lock = 1;
  1970. i = 1;
  1971. } else {
  1972. try_lock = 0;
  1973. i = 0;
  1974. }
  1975. again:
  1976. for (; i < inodes; i++) {
  1977. ASSERT(ips[i]);
  1978. if (i && (ips[i] == ips[i-1])) /* Already locked */
  1979. continue;
  1980. /*
  1981. * If try_lock is not set yet, make sure all locked inodes
  1982. * are not in the AIL.
  1983. * If any are, set try_lock to be used later.
  1984. */
  1985. if (!try_lock) {
  1986. for (j = (i - 1); j >= 0 && !try_lock; j--) {
  1987. lp = (xfs_log_item_t *)ips[j]->i_itemp;
  1988. if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
  1989. try_lock++;
  1990. }
  1991. }
  1992. }
  1993. /*
  1994. * If any of the previous locks we have locked is in the AIL,
  1995. * we must TRY to get the second and subsequent locks. If
  1996. * we can't get any, we must release all we have
  1997. * and try again.
  1998. */
  1999. if (try_lock) {
  2000. /* try_lock must be 0 if i is 0. */
  2001. /*
  2002. * try_lock means we have an inode locked
  2003. * that is in the AIL.
  2004. */
  2005. ASSERT(i != 0);
  2006. if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
  2007. attempts++;
  2008. /*
  2009. * Unlock all previous guys and try again.
  2010. * xfs_iunlock will try to push the tail
  2011. * if the inode is in the AIL.
  2012. */
  2013. for(j = i - 1; j >= 0; j--) {
  2014. /*
  2015. * Check to see if we've already
  2016. * unlocked this one.
  2017. * Not the first one going back,
  2018. * and the inode ptr is the same.
  2019. */
  2020. if ((j != (i - 1)) && ips[j] ==
  2021. ips[j+1])
  2022. continue;
  2023. xfs_iunlock(ips[j], lock_mode);
  2024. }
  2025. if ((attempts % 5) == 0) {
  2026. delay(1); /* Don't just spin the CPU */
  2027. #ifdef DEBUG
  2028. xfs_lock_delays++;
  2029. #endif
  2030. }
  2031. i = 0;
  2032. try_lock = 0;
  2033. goto again;
  2034. }
  2035. } else {
  2036. xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
  2037. }
  2038. }
  2039. #ifdef DEBUG
  2040. if (attempts) {
  2041. if (attempts < 5) xfs_small_retries++;
  2042. else if (attempts < 100) xfs_middle_retries++;
  2043. else xfs_lots_retries++;
  2044. } else {
  2045. xfs_locked_n++;
  2046. }
  2047. #endif
  2048. }
  2049. #ifdef DEBUG
  2050. #define REMOVE_DEBUG_TRACE(x) {remove_which_error_return = (x);}
  2051. int remove_which_error_return = 0;
  2052. #else /* ! DEBUG */
  2053. #define REMOVE_DEBUG_TRACE(x)
  2054. #endif /* ! DEBUG */
  2055. int
  2056. xfs_remove(
  2057. xfs_inode_t *dp,
  2058. bhv_vname_t *dentry)
  2059. {
  2060. bhv_vnode_t *dir_vp = XFS_ITOV(dp);
  2061. char *name = VNAME(dentry);
  2062. xfs_mount_t *mp = dp->i_mount;
  2063. xfs_inode_t *ip;
  2064. xfs_trans_t *tp = NULL;
  2065. int error = 0;
  2066. xfs_bmap_free_t free_list;
  2067. xfs_fsblock_t first_block;
  2068. int cancel_flags;
  2069. int committed;
  2070. int dm_di_mode = 0;
  2071. int link_zero;
  2072. uint resblks;
  2073. int namelen;
  2074. xfs_itrace_entry(dp);
  2075. if (XFS_FORCED_SHUTDOWN(mp))
  2076. return XFS_ERROR(EIO);
  2077. namelen = VNAMELEN(dentry);
  2078. if (!xfs_get_dir_entry(dentry, &ip)) {
  2079. dm_di_mode = ip->i_d.di_mode;
  2080. IRELE(ip);
  2081. }
  2082. if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
  2083. error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dir_vp,
  2084. DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
  2085. name, NULL, dm_di_mode, 0, 0);
  2086. if (error)
  2087. return error;
  2088. }
  2089. /* From this point on, return through std_return */
  2090. ip = NULL;
  2091. /*
  2092. * We need to get a reference to ip before we get our log
  2093. * reservation. The reason for this is that we cannot call
  2094. * xfs_iget for an inode for which we do not have a reference
  2095. * once we've acquired a log reservation. This is because the
  2096. * inode we are trying to get might be in xfs_inactive going
  2097. * for a log reservation. Since we'll have to wait for the
  2098. * inactive code to complete before returning from xfs_iget,
  2099. * we need to make sure that we don't have log space reserved
  2100. * when we call xfs_iget. Instead we get an unlocked reference
  2101. * to the inode before getting our log reservation.
  2102. */
  2103. error = xfs_get_dir_entry(dentry, &ip);
  2104. if (error) {
  2105. REMOVE_DEBUG_TRACE(__LINE__);
  2106. goto std_return;
  2107. }
  2108. dm_di_mode = ip->i_d.di_mode;
  2109. xfs_itrace_entry(ip);
  2110. xfs_itrace_ref(ip);
  2111. error = XFS_QM_DQATTACH(mp, dp, 0);
  2112. if (!error && dp != ip)
  2113. error = XFS_QM_DQATTACH(mp, ip, 0);
  2114. if (error) {
  2115. REMOVE_DEBUG_TRACE(__LINE__);
  2116. IRELE(ip);
  2117. goto std_return;
  2118. }
  2119. tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
  2120. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2121. /*
  2122. * We try to get the real space reservation first,
  2123. * allowing for directory btree deletion(s) implying
  2124. * possible bmap insert(s). If we can't get the space
  2125. * reservation then we use 0 instead, and avoid the bmap
  2126. * btree insert(s) in the directory code by, if the bmap
  2127. * insert tries to happen, instead trimming the LAST
  2128. * block from the directory.
  2129. */
  2130. resblks = XFS_REMOVE_SPACE_RES(mp);
  2131. error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
  2132. XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
  2133. if (error == ENOSPC) {
  2134. resblks = 0;
  2135. error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
  2136. XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
  2137. }
  2138. if (error) {
  2139. ASSERT(error != ENOSPC);
  2140. REMOVE_DEBUG_TRACE(__LINE__);
  2141. xfs_trans_cancel(tp, 0);
  2142. IRELE(ip);
  2143. return error;
  2144. }
  2145. error = xfs_lock_dir_and_entry(dp, ip);
  2146. if (error) {
  2147. REMOVE_DEBUG_TRACE(__LINE__);
  2148. xfs_trans_cancel(tp, cancel_flags);
  2149. IRELE(ip);
  2150. goto std_return;
  2151. }
  2152. /*
  2153. * At this point, we've gotten both the directory and the entry
  2154. * inodes locked.
  2155. */
  2156. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  2157. if (dp != ip) {
  2158. /*
  2159. * Increment vnode ref count only in this case since
  2160. * there's an extra vnode reference in the case where
  2161. * dp == ip.
  2162. */
  2163. IHOLD(dp);
  2164. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  2165. }
  2166. /*
  2167. * Entry must exist since we did a lookup in xfs_lock_dir_and_entry.
  2168. */
  2169. XFS_BMAP_INIT(&free_list, &first_block);
  2170. error = xfs_dir_removename(tp, dp, name, namelen, ip->i_ino,
  2171. &first_block, &free_list, 0);
  2172. if (error) {
  2173. ASSERT(error != ENOENT);
  2174. REMOVE_DEBUG_TRACE(__LINE__);
  2175. goto error1;
  2176. }
  2177. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2178. dp->i_gen++;
  2179. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  2180. error = xfs_droplink(tp, ip);
  2181. if (error) {
  2182. REMOVE_DEBUG_TRACE(__LINE__);
  2183. goto error1;
  2184. }
  2185. /* Determine if this is the last link while
  2186. * we are in the transaction.
  2187. */
  2188. link_zero = (ip)->i_d.di_nlink==0;
  2189. /*
  2190. * Take an extra ref on the inode so that it doesn't
  2191. * go to xfs_inactive() from within the commit.
  2192. */
  2193. IHOLD(ip);
  2194. /*
  2195. * If this is a synchronous mount, make sure that the
  2196. * remove transaction goes to disk before returning to
  2197. * the user.
  2198. */
  2199. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  2200. xfs_trans_set_sync(tp);
  2201. }
  2202. error = xfs_bmap_finish(&tp, &free_list, &committed);
  2203. if (error) {
  2204. REMOVE_DEBUG_TRACE(__LINE__);
  2205. goto error_rele;
  2206. }
  2207. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2208. if (error) {
  2209. IRELE(ip);
  2210. goto std_return;
  2211. }
  2212. /*
  2213. * Before we drop our extra reference to the inode, purge it
  2214. * from the refcache if it is there. By waiting until afterwards
  2215. * to do the IRELE, we ensure that we won't go inactive in the
  2216. * xfs_refcache_purge_ip routine (although that would be OK).
  2217. */
  2218. xfs_refcache_purge_ip(ip);
  2219. /*
  2220. * If we are using filestreams, kill the stream association.
  2221. * If the file is still open it may get a new one but that
  2222. * will get killed on last close in xfs_close() so we don't
  2223. * have to worry about that.
  2224. */
  2225. if (link_zero && xfs_inode_is_filestream(ip))
  2226. xfs_filestream_deassociate(ip);
  2227. xfs_itrace_exit(ip);
  2228. IRELE(ip);
  2229. /* Fall through to std_return with error = 0 */
  2230. std_return:
  2231. if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
  2232. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
  2233. dir_vp, DM_RIGHT_NULL,
  2234. NULL, DM_RIGHT_NULL,
  2235. name, NULL, dm_di_mode, error, 0);
  2236. }
  2237. return error;
  2238. error1:
  2239. xfs_bmap_cancel(&free_list);
  2240. cancel_flags |= XFS_TRANS_ABORT;
  2241. xfs_trans_cancel(tp, cancel_flags);
  2242. goto std_return;
  2243. error_rele:
  2244. /*
  2245. * In this case make sure to not release the inode until after
  2246. * the current transaction is aborted. Releasing it beforehand
  2247. * can cause us to go to xfs_inactive and start a recursive
  2248. * transaction which can easily deadlock with the current one.
  2249. */
  2250. xfs_bmap_cancel(&free_list);
  2251. cancel_flags |= XFS_TRANS_ABORT;
  2252. xfs_trans_cancel(tp, cancel_flags);
  2253. /*
  2254. * Before we drop our extra reference to the inode, purge it
  2255. * from the refcache if it is there. By waiting until afterwards
  2256. * to do the IRELE, we ensure that we won't go inactive in the
  2257. * xfs_refcache_purge_ip routine (although that would be OK).
  2258. */
  2259. xfs_refcache_purge_ip(ip);
  2260. IRELE(ip);
  2261. goto std_return;
  2262. }
  2263. int
  2264. xfs_link(
  2265. xfs_inode_t *tdp,
  2266. bhv_vnode_t *src_vp,
  2267. bhv_vname_t *dentry)
  2268. {
  2269. bhv_vnode_t *target_dir_vp = XFS_ITOV(tdp);
  2270. xfs_mount_t *mp = tdp->i_mount;
  2271. xfs_inode_t *sip = xfs_vtoi(src_vp);
  2272. xfs_trans_t *tp;
  2273. xfs_inode_t *ips[2];
  2274. int error;
  2275. xfs_bmap_free_t free_list;
  2276. xfs_fsblock_t first_block;
  2277. int cancel_flags;
  2278. int committed;
  2279. int resblks;
  2280. char *target_name = VNAME(dentry);
  2281. int target_namelen;
  2282. xfs_itrace_entry(tdp);
  2283. xfs_itrace_entry(xfs_vtoi(src_vp));
  2284. target_namelen = VNAMELEN(dentry);
  2285. ASSERT(!VN_ISDIR(src_vp));
  2286. if (XFS_FORCED_SHUTDOWN(mp))
  2287. return XFS_ERROR(EIO);
  2288. if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
  2289. error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
  2290. target_dir_vp, DM_RIGHT_NULL,
  2291. src_vp, DM_RIGHT_NULL,
  2292. target_name, NULL, 0, 0, 0);
  2293. if (error)
  2294. return error;
  2295. }
  2296. /* Return through std_return after this point. */
  2297. error = XFS_QM_DQATTACH(mp, sip, 0);
  2298. if (!error && sip != tdp)
  2299. error = XFS_QM_DQATTACH(mp, tdp, 0);
  2300. if (error)
  2301. goto std_return;
  2302. tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
  2303. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2304. resblks = XFS_LINK_SPACE_RES(mp, target_namelen);
  2305. error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
  2306. XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
  2307. if (error == ENOSPC) {
  2308. resblks = 0;
  2309. error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
  2310. XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
  2311. }
  2312. if (error) {
  2313. cancel_flags = 0;
  2314. goto error_return;
  2315. }
  2316. if (sip->i_ino < tdp->i_ino) {
  2317. ips[0] = sip;
  2318. ips[1] = tdp;
  2319. } else {
  2320. ips[0] = tdp;
  2321. ips[1] = sip;
  2322. }
  2323. xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
  2324. /*
  2325. * Increment vnode ref counts since xfs_trans_commit &
  2326. * xfs_trans_cancel will both unlock the inodes and
  2327. * decrement the associated ref counts.
  2328. */
  2329. VN_HOLD(src_vp);
  2330. VN_HOLD(target_dir_vp);
  2331. xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
  2332. xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
  2333. /*
  2334. * If the source has too many links, we can't make any more to it.
  2335. */
  2336. if (sip->i_d.di_nlink >= XFS_MAXLINK) {
  2337. error = XFS_ERROR(EMLINK);
  2338. goto error_return;
  2339. }
  2340. /*
  2341. * If we are using project inheritance, we only allow hard link
  2342. * creation in our tree when the project IDs are the same; else
  2343. * the tree quota mechanism could be circumvented.
  2344. */
  2345. if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
  2346. (tdp->i_d.di_projid != sip->i_d.di_projid))) {
  2347. error = XFS_ERROR(EXDEV);
  2348. goto error_return;
  2349. }
  2350. if (resblks == 0 &&
  2351. (error = xfs_dir_canenter(tp, tdp, target_name, target_namelen)))
  2352. goto error_return;
  2353. XFS_BMAP_INIT(&free_list, &first_block);
  2354. error = xfs_dir_createname(tp, tdp, target_name, target_namelen,
  2355. sip->i_ino, &first_block, &free_list,
  2356. resblks);
  2357. if (error)
  2358. goto abort_return;
  2359. xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2360. tdp->i_gen++;
  2361. xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
  2362. error = xfs_bumplink(tp, sip);
  2363. if (error)
  2364. goto abort_return;
  2365. /*
  2366. * If this is a synchronous mount, make sure that the
  2367. * link transaction goes to disk before returning to
  2368. * the user.
  2369. */
  2370. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  2371. xfs_trans_set_sync(tp);
  2372. }
  2373. error = xfs_bmap_finish (&tp, &free_list, &committed);
  2374. if (error) {
  2375. xfs_bmap_cancel(&free_list);
  2376. goto abort_return;
  2377. }
  2378. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2379. if (error)
  2380. goto std_return;
  2381. /* Fall through to std_return with error = 0. */
  2382. std_return:
  2383. if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
  2384. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
  2385. target_dir_vp, DM_RIGHT_NULL,
  2386. src_vp, DM_RIGHT_NULL,
  2387. target_name, NULL, 0, error, 0);
  2388. }
  2389. return error;
  2390. abort_return:
  2391. cancel_flags |= XFS_TRANS_ABORT;
  2392. /* FALLTHROUGH */
  2393. error_return:
  2394. xfs_trans_cancel(tp, cancel_flags);
  2395. goto std_return;
  2396. }
  2397. int
  2398. xfs_mkdir(
  2399. xfs_inode_t *dp,
  2400. bhv_vname_t *dentry,
  2401. mode_t mode,
  2402. bhv_vnode_t **vpp,
  2403. cred_t *credp)
  2404. {
  2405. bhv_vnode_t *dir_vp = XFS_ITOV(dp);
  2406. char *dir_name = VNAME(dentry);
  2407. int dir_namelen = VNAMELEN(dentry);
  2408. xfs_mount_t *mp = dp->i_mount;
  2409. xfs_inode_t *cdp; /* inode of created dir */
  2410. bhv_vnode_t *cvp; /* vnode of created dir */
  2411. xfs_trans_t *tp;
  2412. int cancel_flags;
  2413. int error;
  2414. int committed;
  2415. xfs_bmap_free_t free_list;
  2416. xfs_fsblock_t first_block;
  2417. boolean_t unlock_dp_on_error = B_FALSE;
  2418. boolean_t created = B_FALSE;
  2419. int dm_event_sent = 0;
  2420. xfs_prid_t prid;
  2421. struct xfs_dquot *udqp, *gdqp;
  2422. uint resblks;
  2423. if (XFS_FORCED_SHUTDOWN(mp))
  2424. return XFS_ERROR(EIO);
  2425. tp = NULL;
  2426. if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
  2427. error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
  2428. dir_vp, DM_RIGHT_NULL, NULL,
  2429. DM_RIGHT_NULL, dir_name, NULL,
  2430. mode, 0, 0);
  2431. if (error)
  2432. return error;
  2433. dm_event_sent = 1;
  2434. }
  2435. /* Return through std_return after this point. */
  2436. xfs_itrace_entry(dp);
  2437. mp = dp->i_mount;
  2438. udqp = gdqp = NULL;
  2439. if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  2440. prid = dp->i_d.di_projid;
  2441. else
  2442. prid = (xfs_prid_t)dfltprid;
  2443. /*
  2444. * Make sure that we have allocated dquot(s) on disk.
  2445. */
  2446. error = XFS_QM_DQVOPALLOC(mp, dp,
  2447. current_fsuid(credp), current_fsgid(credp), prid,
  2448. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
  2449. if (error)
  2450. goto std_return;
  2451. tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
  2452. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2453. resblks = XFS_MKDIR_SPACE_RES(mp, dir_namelen);
  2454. error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
  2455. XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
  2456. if (error == ENOSPC) {
  2457. resblks = 0;
  2458. error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
  2459. XFS_TRANS_PERM_LOG_RES,
  2460. XFS_MKDIR_LOG_COUNT);
  2461. }
  2462. if (error) {
  2463. cancel_flags = 0;
  2464. goto error_return;
  2465. }
  2466. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  2467. unlock_dp_on_error = B_TRUE;
  2468. /*
  2469. * Check for directory link count overflow.
  2470. */
  2471. if (dp->i_d.di_nlink >= XFS_MAXLINK) {
  2472. error = XFS_ERROR(EMLINK);
  2473. goto error_return;
  2474. }
  2475. /*
  2476. * Reserve disk quota and the inode.
  2477. */
  2478. error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
  2479. if (error)
  2480. goto error_return;
  2481. if (resblks == 0 &&
  2482. (error = xfs_dir_canenter(tp, dp, dir_name, dir_namelen)))
  2483. goto error_return;
  2484. /*
  2485. * create the directory inode.
  2486. */
  2487. error = xfs_dir_ialloc(&tp, dp, mode, 2,
  2488. 0, credp, prid, resblks > 0,
  2489. &cdp, NULL);
  2490. if (error) {
  2491. if (error == ENOSPC)
  2492. goto error_return;
  2493. goto abort_return;
  2494. }
  2495. xfs_itrace_ref(cdp);
  2496. /*
  2497. * Now we add the directory inode to the transaction.
  2498. * We waited until now since xfs_dir_ialloc might start
  2499. * a new transaction. Had we joined the transaction
  2500. * earlier, the locks might have gotten released. An error
  2501. * from here on will result in the transaction cancel
  2502. * unlocking dp so don't do it explicitly in the error path.
  2503. */
  2504. VN_HOLD(dir_vp);
  2505. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  2506. unlock_dp_on_error = B_FALSE;
  2507. XFS_BMAP_INIT(&free_list, &first_block);
  2508. error = xfs_dir_createname(tp, dp, dir_name, dir_namelen, cdp->i_ino,
  2509. &first_block, &free_list, resblks ?
  2510. resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
  2511. if (error) {
  2512. ASSERT(error != ENOSPC);
  2513. goto error1;
  2514. }
  2515. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2516. /*
  2517. * Bump the in memory version number of the parent directory
  2518. * so that other processes accessing it will recognize that
  2519. * the directory has changed.
  2520. */
  2521. dp->i_gen++;
  2522. error = xfs_dir_init(tp, cdp, dp);
  2523. if (error)
  2524. goto error2;
  2525. cdp->i_gen = 1;
  2526. error = xfs_bumplink(tp, dp);
  2527. if (error)
  2528. goto error2;
  2529. cvp = XFS_ITOV(cdp);
  2530. created = B_TRUE;
  2531. *vpp = cvp;
  2532. IHOLD(cdp);
  2533. /*
  2534. * Attach the dquots to the new inode and modify the icount incore.
  2535. */
  2536. XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
  2537. /*
  2538. * If this is a synchronous mount, make sure that the
  2539. * mkdir transaction goes to disk before returning to
  2540. * the user.
  2541. */
  2542. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  2543. xfs_trans_set_sync(tp);
  2544. }
  2545. error = xfs_bmap_finish(&tp, &free_list, &committed);
  2546. if (error) {
  2547. IRELE(cdp);
  2548. goto error2;
  2549. }
  2550. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2551. XFS_QM_DQRELE(mp, udqp);
  2552. XFS_QM_DQRELE(mp, gdqp);
  2553. if (error) {
  2554. IRELE(cdp);
  2555. }
  2556. /* Fall through to std_return with error = 0 or errno from
  2557. * xfs_trans_commit. */
  2558. std_return:
  2559. if ((created || (error != 0 && dm_event_sent != 0)) &&
  2560. DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
  2561. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
  2562. dir_vp, DM_RIGHT_NULL,
  2563. created ? XFS_ITOV(cdp):NULL,
  2564. DM_RIGHT_NULL,
  2565. dir_name, NULL,
  2566. mode, error, 0);
  2567. }
  2568. return error;
  2569. error2:
  2570. error1:
  2571. xfs_bmap_cancel(&free_list);
  2572. abort_return:
  2573. cancel_flags |= XFS_TRANS_ABORT;
  2574. error_return:
  2575. xfs_trans_cancel(tp, cancel_flags);
  2576. XFS_QM_DQRELE(mp, udqp);
  2577. XFS_QM_DQRELE(mp, gdqp);
  2578. if (unlock_dp_on_error)
  2579. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  2580. goto std_return;
  2581. }
  2582. int
  2583. xfs_rmdir(
  2584. xfs_inode_t *dp,
  2585. bhv_vname_t *dentry)
  2586. {
  2587. bhv_vnode_t *dir_vp = XFS_ITOV(dp);
  2588. char *name = VNAME(dentry);
  2589. int namelen = VNAMELEN(dentry);
  2590. xfs_mount_t *mp = dp->i_mount;
  2591. xfs_inode_t *cdp; /* child directory */
  2592. xfs_trans_t *tp;
  2593. int error;
  2594. xfs_bmap_free_t free_list;
  2595. xfs_fsblock_t first_block;
  2596. int cancel_flags;
  2597. int committed;
  2598. int dm_di_mode = S_IFDIR;
  2599. int last_cdp_link;
  2600. uint resblks;
  2601. xfs_itrace_entry(dp);
  2602. if (XFS_FORCED_SHUTDOWN(mp))
  2603. return XFS_ERROR(EIO);
  2604. if (!xfs_get_dir_entry(dentry, &cdp)) {
  2605. dm_di_mode = cdp->i_d.di_mode;
  2606. IRELE(cdp);
  2607. }
  2608. if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
  2609. error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE,
  2610. dir_vp, DM_RIGHT_NULL,
  2611. NULL, DM_RIGHT_NULL,
  2612. name, NULL, dm_di_mode, 0, 0);
  2613. if (error)
  2614. return XFS_ERROR(error);
  2615. }
  2616. /* Return through std_return after this point. */
  2617. cdp = NULL;
  2618. /*
  2619. * We need to get a reference to cdp before we get our log
  2620. * reservation. The reason for this is that we cannot call
  2621. * xfs_iget for an inode for which we do not have a reference
  2622. * once we've acquired a log reservation. This is because the
  2623. * inode we are trying to get might be in xfs_inactive going
  2624. * for a log reservation. Since we'll have to wait for the
  2625. * inactive code to complete before returning from xfs_iget,
  2626. * we need to make sure that we don't have log space reserved
  2627. * when we call xfs_iget. Instead we get an unlocked reference
  2628. * to the inode before getting our log reservation.
  2629. */
  2630. error = xfs_get_dir_entry(dentry, &cdp);
  2631. if (error) {
  2632. REMOVE_DEBUG_TRACE(__LINE__);
  2633. goto std_return;
  2634. }
  2635. mp = dp->i_mount;
  2636. dm_di_mode = cdp->i_d.di_mode;
  2637. /*
  2638. * Get the dquots for the inodes.
  2639. */
  2640. error = XFS_QM_DQATTACH(mp, dp, 0);
  2641. if (!error && dp != cdp)
  2642. error = XFS_QM_DQATTACH(mp, cdp, 0);
  2643. if (error) {
  2644. IRELE(cdp);
  2645. REMOVE_DEBUG_TRACE(__LINE__);
  2646. goto std_return;
  2647. }
  2648. tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
  2649. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2650. /*
  2651. * We try to get the real space reservation first,
  2652. * allowing for directory btree deletion(s) implying
  2653. * possible bmap insert(s). If we can't get the space
  2654. * reservation then we use 0 instead, and avoid the bmap
  2655. * btree insert(s) in the directory code by, if the bmap
  2656. * insert tries to happen, instead trimming the LAST
  2657. * block from the directory.
  2658. */
  2659. resblks = XFS_REMOVE_SPACE_RES(mp);
  2660. error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
  2661. XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
  2662. if (error == ENOSPC) {
  2663. resblks = 0;
  2664. error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
  2665. XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
  2666. }
  2667. if (error) {
  2668. ASSERT(error != ENOSPC);
  2669. cancel_flags = 0;
  2670. IRELE(cdp);
  2671. goto error_return;
  2672. }
  2673. XFS_BMAP_INIT(&free_list, &first_block);
  2674. /*
  2675. * Now lock the child directory inode and the parent directory
  2676. * inode in the proper order. This will take care of validating
  2677. * that the directory entry for the child directory inode has
  2678. * not changed while we were obtaining a log reservation.
  2679. */
  2680. error = xfs_lock_dir_and_entry(dp, cdp);
  2681. if (error) {
  2682. xfs_trans_cancel(tp, cancel_flags);
  2683. IRELE(cdp);
  2684. goto std_return;
  2685. }
  2686. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  2687. if (dp != cdp) {
  2688. /*
  2689. * Only increment the parent directory vnode count if
  2690. * we didn't bump it in looking up cdp. The only time
  2691. * we don't bump it is when we're looking up ".".
  2692. */
  2693. VN_HOLD(dir_vp);
  2694. }
  2695. xfs_itrace_ref(cdp);
  2696. xfs_trans_ijoin(tp, cdp, XFS_ILOCK_EXCL);
  2697. ASSERT(cdp->i_d.di_nlink >= 2);
  2698. if (cdp->i_d.di_nlink != 2) {
  2699. error = XFS_ERROR(ENOTEMPTY);
  2700. goto error_return;
  2701. }
  2702. if (!xfs_dir_isempty(cdp)) {
  2703. error = XFS_ERROR(ENOTEMPTY);
  2704. goto error_return;
  2705. }
  2706. error = xfs_dir_removename(tp, dp, name, namelen, cdp->i_ino,
  2707. &first_block, &free_list, resblks);
  2708. if (error)
  2709. goto error1;
  2710. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2711. /*
  2712. * Bump the in memory generation count on the parent
  2713. * directory so that other can know that it has changed.
  2714. */
  2715. dp->i_gen++;
  2716. /*
  2717. * Drop the link from cdp's "..".
  2718. */
  2719. error = xfs_droplink(tp, dp);
  2720. if (error) {
  2721. goto error1;
  2722. }
  2723. /*
  2724. * Drop the link from dp to cdp.
  2725. */
  2726. error = xfs_droplink(tp, cdp);
  2727. if (error) {
  2728. goto error1;
  2729. }
  2730. /*
  2731. * Drop the "." link from cdp to self.
  2732. */
  2733. error = xfs_droplink(tp, cdp);
  2734. if (error) {
  2735. goto error1;
  2736. }
  2737. /* Determine these before committing transaction */
  2738. last_cdp_link = (cdp)->i_d.di_nlink==0;
  2739. /*
  2740. * Take an extra ref on the child vnode so that it
  2741. * does not go to xfs_inactive() from within the commit.
  2742. */
  2743. IHOLD(cdp);
  2744. /*
  2745. * If this is a synchronous mount, make sure that the
  2746. * rmdir transaction goes to disk before returning to
  2747. * the user.
  2748. */
  2749. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  2750. xfs_trans_set_sync(tp);
  2751. }
  2752. error = xfs_bmap_finish (&tp, &free_list, &committed);
  2753. if (error) {
  2754. xfs_bmap_cancel(&free_list);
  2755. xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
  2756. XFS_TRANS_ABORT));
  2757. IRELE(cdp);
  2758. goto std_return;
  2759. }
  2760. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2761. if (error) {
  2762. IRELE(cdp);
  2763. goto std_return;
  2764. }
  2765. IRELE(cdp);
  2766. /* Fall through to std_return with error = 0 or the errno
  2767. * from xfs_trans_commit. */
  2768. std_return:
  2769. if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
  2770. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
  2771. dir_vp, DM_RIGHT_NULL,
  2772. NULL, DM_RIGHT_NULL,
  2773. name, NULL, dm_di_mode,
  2774. error, 0);
  2775. }
  2776. return error;
  2777. error1:
  2778. xfs_bmap_cancel(&free_list);
  2779. cancel_flags |= XFS_TRANS_ABORT;
  2780. /* FALLTHROUGH */
  2781. error_return:
  2782. xfs_trans_cancel(tp, cancel_flags);
  2783. goto std_return;
  2784. }
  2785. int
  2786. xfs_symlink(
  2787. xfs_inode_t *dp,
  2788. bhv_vname_t *dentry,
  2789. char *target_path,
  2790. mode_t mode,
  2791. bhv_vnode_t **vpp,
  2792. cred_t *credp)
  2793. {
  2794. bhv_vnode_t *dir_vp = XFS_ITOV(dp);
  2795. xfs_mount_t *mp = dp->i_mount;
  2796. xfs_trans_t *tp;
  2797. xfs_inode_t *ip;
  2798. int error;
  2799. int pathlen;
  2800. xfs_bmap_free_t free_list;
  2801. xfs_fsblock_t first_block;
  2802. boolean_t unlock_dp_on_error = B_FALSE;
  2803. uint cancel_flags;
  2804. int committed;
  2805. xfs_fileoff_t first_fsb;
  2806. xfs_filblks_t fs_blocks;
  2807. int nmaps;
  2808. xfs_bmbt_irec_t mval[SYMLINK_MAPS];
  2809. xfs_daddr_t d;
  2810. char *cur_chunk;
  2811. int byte_cnt;
  2812. int n;
  2813. xfs_buf_t *bp;
  2814. xfs_prid_t prid;
  2815. struct xfs_dquot *udqp, *gdqp;
  2816. uint resblks;
  2817. char *link_name = VNAME(dentry);
  2818. int link_namelen;
  2819. *vpp = NULL;
  2820. error = 0;
  2821. ip = NULL;
  2822. tp = NULL;
  2823. xfs_itrace_entry(dp);
  2824. if (XFS_FORCED_SHUTDOWN(mp))
  2825. return XFS_ERROR(EIO);
  2826. link_namelen = VNAMELEN(dentry);
  2827. /*
  2828. * Check component lengths of the target path name.
  2829. */
  2830. pathlen = strlen(target_path);
  2831. if (pathlen >= MAXPATHLEN) /* total string too long */
  2832. return XFS_ERROR(ENAMETOOLONG);
  2833. if (pathlen >= MAXNAMELEN) { /* is any component too long? */
  2834. int len, total;
  2835. char *path;
  2836. for (total = 0, path = target_path; total < pathlen;) {
  2837. /*
  2838. * Skip any slashes.
  2839. */
  2840. while(*path == '/') {
  2841. total++;
  2842. path++;
  2843. }
  2844. /*
  2845. * Count up to the next slash or end of path.
  2846. * Error out if the component is bigger than MAXNAMELEN.
  2847. */
  2848. for(len = 0; *path != '/' && total < pathlen;total++, path++) {
  2849. if (++len >= MAXNAMELEN) {
  2850. error = ENAMETOOLONG;
  2851. return error;
  2852. }
  2853. }
  2854. }
  2855. }
  2856. if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
  2857. error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dir_vp,
  2858. DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
  2859. link_name, target_path, 0, 0, 0);
  2860. if (error)
  2861. return error;
  2862. }
  2863. /* Return through std_return after this point. */
  2864. udqp = gdqp = NULL;
  2865. if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  2866. prid = dp->i_d.di_projid;
  2867. else
  2868. prid = (xfs_prid_t)dfltprid;
  2869. /*
  2870. * Make sure that we have allocated dquot(s) on disk.
  2871. */
  2872. error = XFS_QM_DQVOPALLOC(mp, dp,
  2873. current_fsuid(credp), current_fsgid(credp), prid,
  2874. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
  2875. if (error)
  2876. goto std_return;
  2877. tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
  2878. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2879. /*
  2880. * The symlink will fit into the inode data fork?
  2881. * There can't be any attributes so we get the whole variable part.
  2882. */
  2883. if (pathlen <= XFS_LITINO(mp))
  2884. fs_blocks = 0;
  2885. else
  2886. fs_blocks = XFS_B_TO_FSB(mp, pathlen);
  2887. resblks = XFS_SYMLINK_SPACE_RES(mp, link_namelen, fs_blocks);
  2888. error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
  2889. XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
  2890. if (error == ENOSPC && fs_blocks == 0) {
  2891. resblks = 0;
  2892. error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
  2893. XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
  2894. }
  2895. if (error) {
  2896. cancel_flags = 0;
  2897. goto error_return;
  2898. }
  2899. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  2900. unlock_dp_on_error = B_TRUE;
  2901. /*
  2902. * Check whether the directory allows new symlinks or not.
  2903. */
  2904. if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
  2905. error = XFS_ERROR(EPERM);
  2906. goto error_return;
  2907. }
  2908. /*
  2909. * Reserve disk quota : blocks and inode.
  2910. */
  2911. error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
  2912. if (error)
  2913. goto error_return;
  2914. /*
  2915. * Check for ability to enter directory entry, if no space reserved.
  2916. */
  2917. if (resblks == 0 &&
  2918. (error = xfs_dir_canenter(tp, dp, link_name, link_namelen)))
  2919. goto error_return;
  2920. /*
  2921. * Initialize the bmap freelist prior to calling either
  2922. * bmapi or the directory create code.
  2923. */
  2924. XFS_BMAP_INIT(&free_list, &first_block);
  2925. /*
  2926. * Allocate an inode for the symlink.
  2927. */
  2928. error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
  2929. 1, 0, credp, prid, resblks > 0, &ip, NULL);
  2930. if (error) {
  2931. if (error == ENOSPC)
  2932. goto error_return;
  2933. goto error1;
  2934. }
  2935. xfs_itrace_ref(ip);
  2936. /*
  2937. * An error after we've joined dp to the transaction will result in the
  2938. * transaction cancel unlocking dp so don't do it explicitly in the
  2939. * error path.
  2940. */
  2941. VN_HOLD(dir_vp);
  2942. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  2943. unlock_dp_on_error = B_FALSE;
  2944. /*
  2945. * Also attach the dquot(s) to it, if applicable.
  2946. */
  2947. XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
  2948. if (resblks)
  2949. resblks -= XFS_IALLOC_SPACE_RES(mp);
  2950. /*
  2951. * If the symlink will fit into the inode, write it inline.
  2952. */
  2953. if (pathlen <= XFS_IFORK_DSIZE(ip)) {
  2954. xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
  2955. memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
  2956. ip->i_d.di_size = pathlen;
  2957. /*
  2958. * The inode was initially created in extent format.
  2959. */
  2960. ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
  2961. ip->i_df.if_flags |= XFS_IFINLINE;
  2962. ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
  2963. xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
  2964. } else {
  2965. first_fsb = 0;
  2966. nmaps = SYMLINK_MAPS;
  2967. error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
  2968. XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
  2969. &first_block, resblks, mval, &nmaps,
  2970. &free_list, NULL);
  2971. if (error) {
  2972. goto error1;
  2973. }
  2974. if (resblks)
  2975. resblks -= fs_blocks;
  2976. ip->i_d.di_size = pathlen;
  2977. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  2978. cur_chunk = target_path;
  2979. for (n = 0; n < nmaps; n++) {
  2980. d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
  2981. byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
  2982. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
  2983. BTOBB(byte_cnt), 0);
  2984. ASSERT(bp && !XFS_BUF_GETERROR(bp));
  2985. if (pathlen < byte_cnt) {
  2986. byte_cnt = pathlen;
  2987. }
  2988. pathlen -= byte_cnt;
  2989. memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
  2990. cur_chunk += byte_cnt;
  2991. xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
  2992. }
  2993. }
  2994. /*
  2995. * Create the directory entry for the symlink.
  2996. */
  2997. error = xfs_dir_createname(tp, dp, link_name, link_namelen, ip->i_ino,
  2998. &first_block, &free_list, resblks);
  2999. if (error)
  3000. goto error1;
  3001. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  3002. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  3003. /*
  3004. * Bump the in memory version number of the parent directory
  3005. * so that other processes accessing it will recognize that
  3006. * the directory has changed.
  3007. */
  3008. dp->i_gen++;
  3009. /*
  3010. * If this is a synchronous mount, make sure that the
  3011. * symlink transaction goes to disk before returning to
  3012. * the user.
  3013. */
  3014. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  3015. xfs_trans_set_sync(tp);
  3016. }
  3017. /*
  3018. * xfs_trans_commit normally decrements the vnode ref count
  3019. * when it unlocks the inode. Since we want to return the
  3020. * vnode to the caller, we bump the vnode ref count now.
  3021. */
  3022. IHOLD(ip);
  3023. error = xfs_bmap_finish(&tp, &free_list, &committed);
  3024. if (error) {
  3025. goto error2;
  3026. }
  3027. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  3028. XFS_QM_DQRELE(mp, udqp);
  3029. XFS_QM_DQRELE(mp, gdqp);
  3030. /* Fall through to std_return with error = 0 or errno from
  3031. * xfs_trans_commit */
  3032. std_return:
  3033. if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
  3034. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
  3035. dir_vp, DM_RIGHT_NULL,
  3036. error ? NULL : XFS_ITOV(ip),
  3037. DM_RIGHT_NULL, link_name, target_path,
  3038. 0, error, 0);
  3039. }
  3040. if (!error) {
  3041. bhv_vnode_t *vp;
  3042. ASSERT(ip);
  3043. vp = XFS_ITOV(ip);
  3044. *vpp = vp;
  3045. }
  3046. return error;
  3047. error2:
  3048. IRELE(ip);
  3049. error1:
  3050. xfs_bmap_cancel(&free_list);
  3051. cancel_flags |= XFS_TRANS_ABORT;
  3052. error_return:
  3053. xfs_trans_cancel(tp, cancel_flags);
  3054. XFS_QM_DQRELE(mp, udqp);
  3055. XFS_QM_DQRELE(mp, gdqp);
  3056. if (unlock_dp_on_error)
  3057. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  3058. goto std_return;
  3059. }
  3060. int
  3061. xfs_fid2(
  3062. xfs_inode_t *ip,
  3063. xfs_fid_t *xfid)
  3064. {
  3065. xfs_itrace_entry(ip);
  3066. xfid->fid_len = sizeof(xfs_fid_t) - sizeof(xfid->fid_len);
  3067. xfid->fid_pad = 0;
  3068. /*
  3069. * use memcpy because the inode is a long long and there's no
  3070. * assurance that xfid->fid_ino is properly aligned.
  3071. */
  3072. memcpy(&xfid->fid_ino, &ip->i_ino, sizeof(xfid->fid_ino));
  3073. xfid->fid_gen = ip->i_d.di_gen;
  3074. return 0;
  3075. }
  3076. int
  3077. xfs_rwlock(
  3078. xfs_inode_t *ip,
  3079. bhv_vrwlock_t locktype)
  3080. {
  3081. if (S_ISDIR(ip->i_d.di_mode))
  3082. return 1;
  3083. if (locktype == VRWLOCK_WRITE) {
  3084. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  3085. } else if (locktype == VRWLOCK_TRY_READ) {
  3086. return xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED);
  3087. } else if (locktype == VRWLOCK_TRY_WRITE) {
  3088. return xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL);
  3089. } else {
  3090. ASSERT((locktype == VRWLOCK_READ) ||
  3091. (locktype == VRWLOCK_WRITE_DIRECT));
  3092. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  3093. }
  3094. return 1;
  3095. }
  3096. void
  3097. xfs_rwunlock(
  3098. xfs_inode_t *ip,
  3099. bhv_vrwlock_t locktype)
  3100. {
  3101. if (S_ISDIR(ip->i_d.di_mode))
  3102. return;
  3103. if (locktype == VRWLOCK_WRITE) {
  3104. /*
  3105. * In the write case, we may have added a new entry to
  3106. * the reference cache. This might store a pointer to
  3107. * an inode to be released in this inode. If it is there,
  3108. * clear the pointer and release the inode after unlocking
  3109. * this one.
  3110. */
  3111. xfs_refcache_iunlock(ip, XFS_IOLOCK_EXCL);
  3112. } else {
  3113. ASSERT((locktype == VRWLOCK_READ) ||
  3114. (locktype == VRWLOCK_WRITE_DIRECT));
  3115. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  3116. }
  3117. return;
  3118. }
  3119. int
  3120. xfs_inode_flush(
  3121. xfs_inode_t *ip,
  3122. int flags)
  3123. {
  3124. xfs_mount_t *mp = ip->i_mount;
  3125. xfs_inode_log_item_t *iip = ip->i_itemp;
  3126. int error = 0;
  3127. if (XFS_FORCED_SHUTDOWN(mp))
  3128. return XFS_ERROR(EIO);
  3129. /*
  3130. * Bypass inodes which have already been cleaned by
  3131. * the inode flush clustering code inside xfs_iflush
  3132. */
  3133. if ((ip->i_update_core == 0) &&
  3134. ((iip == NULL) || !(iip->ili_format.ilf_fields & XFS_ILOG_ALL)))
  3135. return 0;
  3136. if (flags & FLUSH_LOG) {
  3137. if (iip && iip->ili_last_lsn) {
  3138. xlog_t *log = mp->m_log;
  3139. xfs_lsn_t sync_lsn;
  3140. int log_flags = XFS_LOG_FORCE;
  3141. spin_lock(&log->l_grant_lock);
  3142. sync_lsn = log->l_last_sync_lsn;
  3143. spin_unlock(&log->l_grant_lock);
  3144. if ((XFS_LSN_CMP(iip->ili_last_lsn, sync_lsn) > 0)) {
  3145. if (flags & FLUSH_SYNC)
  3146. log_flags |= XFS_LOG_SYNC;
  3147. error = xfs_log_force(mp, iip->ili_last_lsn, log_flags);
  3148. if (error)
  3149. return error;
  3150. }
  3151. if (ip->i_update_core == 0)
  3152. return 0;
  3153. }
  3154. }
  3155. /*
  3156. * We make this non-blocking if the inode is contended,
  3157. * return EAGAIN to indicate to the caller that they
  3158. * did not succeed. This prevents the flush path from
  3159. * blocking on inodes inside another operation right
  3160. * now, they get caught later by xfs_sync.
  3161. */
  3162. if (flags & FLUSH_INODE) {
  3163. int flush_flags;
  3164. if (flags & FLUSH_SYNC) {
  3165. xfs_ilock(ip, XFS_ILOCK_SHARED);
  3166. xfs_iflock(ip);
  3167. } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
  3168. if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
  3169. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  3170. return EAGAIN;
  3171. }
  3172. } else {
  3173. return EAGAIN;
  3174. }
  3175. if (flags & FLUSH_SYNC)
  3176. flush_flags = XFS_IFLUSH_SYNC;
  3177. else
  3178. flush_flags = XFS_IFLUSH_ASYNC;
  3179. error = xfs_iflush(ip, flush_flags);
  3180. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  3181. }
  3182. return error;
  3183. }
  3184. int
  3185. xfs_set_dmattrs(
  3186. xfs_inode_t *ip,
  3187. u_int evmask,
  3188. u_int16_t state)
  3189. {
  3190. xfs_mount_t *mp = ip->i_mount;
  3191. xfs_trans_t *tp;
  3192. int error;
  3193. if (!capable(CAP_SYS_ADMIN))
  3194. return XFS_ERROR(EPERM);
  3195. if (XFS_FORCED_SHUTDOWN(mp))
  3196. return XFS_ERROR(EIO);
  3197. tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
  3198. error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
  3199. if (error) {
  3200. xfs_trans_cancel(tp, 0);
  3201. return error;
  3202. }
  3203. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3204. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  3205. ip->i_d.di_dmevmask = evmask;
  3206. ip->i_d.di_dmstate = state;
  3207. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  3208. IHOLD(ip);
  3209. error = xfs_trans_commit(tp, 0);
  3210. return error;
  3211. }
  3212. int
  3213. xfs_reclaim(
  3214. xfs_inode_t *ip)
  3215. {
  3216. bhv_vnode_t *vp = XFS_ITOV(ip);
  3217. xfs_itrace_entry(ip);
  3218. ASSERT(!VN_MAPPED(vp));
  3219. /* bad inode, get out here ASAP */
  3220. if (VN_BAD(vp)) {
  3221. xfs_ireclaim(ip);
  3222. return 0;
  3223. }
  3224. vn_iowait(ip);
  3225. ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
  3226. /*
  3227. * Make sure the atime in the XFS inode is correct before freeing the
  3228. * Linux inode.
  3229. */
  3230. xfs_synchronize_atime(ip);
  3231. /*
  3232. * If we have nothing to flush with this inode then complete the
  3233. * teardown now, otherwise break the link between the xfs inode and the
  3234. * linux inode and clean up the xfs inode later. This avoids flushing
  3235. * the inode to disk during the delete operation itself.
  3236. *
  3237. * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
  3238. * first to ensure that xfs_iunpin() will never see an xfs inode
  3239. * that has a linux inode being reclaimed. Synchronisation is provided
  3240. * by the i_flags_lock.
  3241. */
  3242. if (!ip->i_update_core && (ip->i_itemp == NULL)) {
  3243. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3244. xfs_iflock(ip);
  3245. return xfs_finish_reclaim(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
  3246. } else {
  3247. xfs_mount_t *mp = ip->i_mount;
  3248. /* Protect sync and unpin from us */
  3249. XFS_MOUNT_ILOCK(mp);
  3250. spin_lock(&ip->i_flags_lock);
  3251. __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
  3252. vn_to_inode(vp)->i_private = NULL;
  3253. ip->i_vnode = NULL;
  3254. spin_unlock(&ip->i_flags_lock);
  3255. list_add_tail(&ip->i_reclaim, &mp->m_del_inodes);
  3256. XFS_MOUNT_IUNLOCK(mp);
  3257. }
  3258. return 0;
  3259. }
  3260. int
  3261. xfs_finish_reclaim(
  3262. xfs_inode_t *ip,
  3263. int locked,
  3264. int sync_mode)
  3265. {
  3266. xfs_perag_t *pag = xfs_get_perag(ip->i_mount, ip->i_ino);
  3267. bhv_vnode_t *vp = XFS_ITOV_NULL(ip);
  3268. int error;
  3269. if (vp && VN_BAD(vp))
  3270. goto reclaim;
  3271. /* The hash lock here protects a thread in xfs_iget_core from
  3272. * racing with us on linking the inode back with a vnode.
  3273. * Once we have the XFS_IRECLAIM flag set it will not touch
  3274. * us.
  3275. */
  3276. write_lock(&pag->pag_ici_lock);
  3277. spin_lock(&ip->i_flags_lock);
  3278. if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
  3279. (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) && vp == NULL)) {
  3280. spin_unlock(&ip->i_flags_lock);
  3281. write_unlock(&pag->pag_ici_lock);
  3282. if (locked) {
  3283. xfs_ifunlock(ip);
  3284. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3285. }
  3286. return 1;
  3287. }
  3288. __xfs_iflags_set(ip, XFS_IRECLAIM);
  3289. spin_unlock(&ip->i_flags_lock);
  3290. write_unlock(&pag->pag_ici_lock);
  3291. xfs_put_perag(ip->i_mount, pag);
  3292. /*
  3293. * If the inode is still dirty, then flush it out. If the inode
  3294. * is not in the AIL, then it will be OK to flush it delwri as
  3295. * long as xfs_iflush() does not keep any references to the inode.
  3296. * We leave that decision up to xfs_iflush() since it has the
  3297. * knowledge of whether it's OK to simply do a delwri flush of
  3298. * the inode or whether we need to wait until the inode is
  3299. * pulled from the AIL.
  3300. * We get the flush lock regardless, though, just to make sure
  3301. * we don't free it while it is being flushed.
  3302. */
  3303. if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  3304. if (!locked) {
  3305. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3306. xfs_iflock(ip);
  3307. }
  3308. if (ip->i_update_core ||
  3309. ((ip->i_itemp != NULL) &&
  3310. (ip->i_itemp->ili_format.ilf_fields != 0))) {
  3311. error = xfs_iflush(ip, sync_mode);
  3312. /*
  3313. * If we hit an error, typically because of filesystem
  3314. * shutdown, we don't need to let vn_reclaim to know
  3315. * because we're gonna reclaim the inode anyway.
  3316. */
  3317. if (error) {
  3318. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3319. goto reclaim;
  3320. }
  3321. xfs_iflock(ip); /* synchronize with xfs_iflush_done */
  3322. }
  3323. ASSERT(ip->i_update_core == 0);
  3324. ASSERT(ip->i_itemp == NULL ||
  3325. ip->i_itemp->ili_format.ilf_fields == 0);
  3326. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3327. } else if (locked) {
  3328. /*
  3329. * We are not interested in doing an iflush if we're
  3330. * in the process of shutting down the filesystem forcibly.
  3331. * So, just reclaim the inode.
  3332. */
  3333. xfs_ifunlock(ip);
  3334. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3335. }
  3336. reclaim:
  3337. xfs_ireclaim(ip);
  3338. return 0;
  3339. }
  3340. int
  3341. xfs_finish_reclaim_all(xfs_mount_t *mp, int noblock)
  3342. {
  3343. int purged;
  3344. xfs_inode_t *ip, *n;
  3345. int done = 0;
  3346. while (!done) {
  3347. purged = 0;
  3348. XFS_MOUNT_ILOCK(mp);
  3349. list_for_each_entry_safe(ip, n, &mp->m_del_inodes, i_reclaim) {
  3350. if (noblock) {
  3351. if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0)
  3352. continue;
  3353. if (xfs_ipincount(ip) ||
  3354. !xfs_iflock_nowait(ip)) {
  3355. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3356. continue;
  3357. }
  3358. }
  3359. XFS_MOUNT_IUNLOCK(mp);
  3360. if (xfs_finish_reclaim(ip, noblock,
  3361. XFS_IFLUSH_DELWRI_ELSE_ASYNC))
  3362. delay(1);
  3363. purged = 1;
  3364. break;
  3365. }
  3366. done = !purged;
  3367. }
  3368. XFS_MOUNT_IUNLOCK(mp);
  3369. return 0;
  3370. }
  3371. /*
  3372. * xfs_alloc_file_space()
  3373. * This routine allocates disk space for the given file.
  3374. *
  3375. * If alloc_type == 0, this request is for an ALLOCSP type
  3376. * request which will change the file size. In this case, no
  3377. * DMAPI event will be generated by the call. A TRUNCATE event
  3378. * will be generated later by xfs_setattr.
  3379. *
  3380. * If alloc_type != 0, this request is for a RESVSP type
  3381. * request, and a DMAPI DM_EVENT_WRITE will be generated if the
  3382. * lower block boundary byte address is less than the file's
  3383. * length.
  3384. *
  3385. * RETURNS:
  3386. * 0 on success
  3387. * errno on error
  3388. *
  3389. */
  3390. STATIC int
  3391. xfs_alloc_file_space(
  3392. xfs_inode_t *ip,
  3393. xfs_off_t offset,
  3394. xfs_off_t len,
  3395. int alloc_type,
  3396. int attr_flags)
  3397. {
  3398. xfs_mount_t *mp = ip->i_mount;
  3399. xfs_off_t count;
  3400. xfs_filblks_t allocated_fsb;
  3401. xfs_filblks_t allocatesize_fsb;
  3402. xfs_extlen_t extsz, temp;
  3403. xfs_fileoff_t startoffset_fsb;
  3404. xfs_fsblock_t firstfsb;
  3405. int nimaps;
  3406. int bmapi_flag;
  3407. int quota_flag;
  3408. int rt;
  3409. xfs_trans_t *tp;
  3410. xfs_bmbt_irec_t imaps[1], *imapp;
  3411. xfs_bmap_free_t free_list;
  3412. uint qblocks, resblks, resrtextents;
  3413. int committed;
  3414. int error;
  3415. xfs_itrace_entry(ip);
  3416. if (XFS_FORCED_SHUTDOWN(mp))
  3417. return XFS_ERROR(EIO);
  3418. if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
  3419. return error;
  3420. if (len <= 0)
  3421. return XFS_ERROR(EINVAL);
  3422. rt = XFS_IS_REALTIME_INODE(ip);
  3423. extsz = xfs_get_extsz_hint(ip);
  3424. count = len;
  3425. imapp = &imaps[0];
  3426. nimaps = 1;
  3427. bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
  3428. startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
  3429. allocatesize_fsb = XFS_B_TO_FSB(mp, count);
  3430. /* Generate a DMAPI event if needed. */
  3431. if (alloc_type != 0 && offset < ip->i_size &&
  3432. (attr_flags&ATTR_DMI) == 0 &&
  3433. DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
  3434. xfs_off_t end_dmi_offset;
  3435. end_dmi_offset = offset+len;
  3436. if (end_dmi_offset > ip->i_size)
  3437. end_dmi_offset = ip->i_size;
  3438. error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, XFS_ITOV(ip),
  3439. offset, end_dmi_offset - offset,
  3440. 0, NULL);
  3441. if (error)
  3442. return error;
  3443. }
  3444. /*
  3445. * Allocate file space until done or until there is an error
  3446. */
  3447. retry:
  3448. while (allocatesize_fsb && !error) {
  3449. xfs_fileoff_t s, e;
  3450. /*
  3451. * Determine space reservations for data/realtime.
  3452. */
  3453. if (unlikely(extsz)) {
  3454. s = startoffset_fsb;
  3455. do_div(s, extsz);
  3456. s *= extsz;
  3457. e = startoffset_fsb + allocatesize_fsb;
  3458. if ((temp = do_mod(startoffset_fsb, extsz)))
  3459. e += temp;
  3460. if ((temp = do_mod(e, extsz)))
  3461. e += extsz - temp;
  3462. } else {
  3463. s = 0;
  3464. e = allocatesize_fsb;
  3465. }
  3466. if (unlikely(rt)) {
  3467. resrtextents = qblocks = (uint)(e - s);
  3468. resrtextents /= mp->m_sb.sb_rextsize;
  3469. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  3470. quota_flag = XFS_QMOPT_RES_RTBLKS;
  3471. } else {
  3472. resrtextents = 0;
  3473. resblks = qblocks = \
  3474. XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
  3475. quota_flag = XFS_QMOPT_RES_REGBLKS;
  3476. }
  3477. /*
  3478. * Allocate and setup the transaction.
  3479. */
  3480. tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
  3481. error = xfs_trans_reserve(tp, resblks,
  3482. XFS_WRITE_LOG_RES(mp), resrtextents,
  3483. XFS_TRANS_PERM_LOG_RES,
  3484. XFS_WRITE_LOG_COUNT);
  3485. /*
  3486. * Check for running out of space
  3487. */
  3488. if (error) {
  3489. /*
  3490. * Free the transaction structure.
  3491. */
  3492. ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  3493. xfs_trans_cancel(tp, 0);
  3494. break;
  3495. }
  3496. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3497. error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
  3498. qblocks, 0, quota_flag);
  3499. if (error)
  3500. goto error1;
  3501. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  3502. xfs_trans_ihold(tp, ip);
  3503. /*
  3504. * Issue the xfs_bmapi() call to allocate the blocks
  3505. */
  3506. XFS_BMAP_INIT(&free_list, &firstfsb);
  3507. error = xfs_bmapi(tp, ip, startoffset_fsb,
  3508. allocatesize_fsb, bmapi_flag,
  3509. &firstfsb, 0, imapp, &nimaps,
  3510. &free_list, NULL);
  3511. if (error) {
  3512. goto error0;
  3513. }
  3514. /*
  3515. * Complete the transaction
  3516. */
  3517. error = xfs_bmap_finish(&tp, &free_list, &committed);
  3518. if (error) {
  3519. goto error0;
  3520. }
  3521. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  3522. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3523. if (error) {
  3524. break;
  3525. }
  3526. allocated_fsb = imapp->br_blockcount;
  3527. if (nimaps == 0) {
  3528. error = XFS_ERROR(ENOSPC);
  3529. break;
  3530. }
  3531. startoffset_fsb += allocated_fsb;
  3532. allocatesize_fsb -= allocated_fsb;
  3533. }
  3534. dmapi_enospc_check:
  3535. if (error == ENOSPC && (attr_flags & ATTR_DMI) == 0 &&
  3536. DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
  3537. error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
  3538. XFS_ITOV(ip), DM_RIGHT_NULL,
  3539. XFS_ITOV(ip), DM_RIGHT_NULL,
  3540. NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
  3541. if (error == 0)
  3542. goto retry; /* Maybe DMAPI app. has made space */
  3543. /* else fall through with error from XFS_SEND_DATA */
  3544. }
  3545. return error;
  3546. error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
  3547. xfs_bmap_cancel(&free_list);
  3548. XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
  3549. error1: /* Just cancel transaction */
  3550. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  3551. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3552. goto dmapi_enospc_check;
  3553. }
  3554. /*
  3555. * Zero file bytes between startoff and endoff inclusive.
  3556. * The iolock is held exclusive and no blocks are buffered.
  3557. */
  3558. STATIC int
  3559. xfs_zero_remaining_bytes(
  3560. xfs_inode_t *ip,
  3561. xfs_off_t startoff,
  3562. xfs_off_t endoff)
  3563. {
  3564. xfs_bmbt_irec_t imap;
  3565. xfs_fileoff_t offset_fsb;
  3566. xfs_off_t lastoffset;
  3567. xfs_off_t offset;
  3568. xfs_buf_t *bp;
  3569. xfs_mount_t *mp = ip->i_mount;
  3570. int nimap;
  3571. int error = 0;
  3572. bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
  3573. XFS_IS_REALTIME_INODE(ip) ?
  3574. mp->m_rtdev_targp : mp->m_ddev_targp);
  3575. for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
  3576. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  3577. nimap = 1;
  3578. error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
  3579. NULL, 0, &imap, &nimap, NULL, NULL);
  3580. if (error || nimap < 1)
  3581. break;
  3582. ASSERT(imap.br_blockcount >= 1);
  3583. ASSERT(imap.br_startoff == offset_fsb);
  3584. lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
  3585. if (lastoffset > endoff)
  3586. lastoffset = endoff;
  3587. if (imap.br_startblock == HOLESTARTBLOCK)
  3588. continue;
  3589. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  3590. if (imap.br_state == XFS_EXT_UNWRITTEN)
  3591. continue;
  3592. XFS_BUF_UNDONE(bp);
  3593. XFS_BUF_UNWRITE(bp);
  3594. XFS_BUF_READ(bp);
  3595. XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
  3596. xfsbdstrat(mp, bp);
  3597. if ((error = xfs_iowait(bp))) {
  3598. xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
  3599. mp, bp, XFS_BUF_ADDR(bp));
  3600. break;
  3601. }
  3602. memset(XFS_BUF_PTR(bp) +
  3603. (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
  3604. 0, lastoffset - offset + 1);
  3605. XFS_BUF_UNDONE(bp);
  3606. XFS_BUF_UNREAD(bp);
  3607. XFS_BUF_WRITE(bp);
  3608. xfsbdstrat(mp, bp);
  3609. if ((error = xfs_iowait(bp))) {
  3610. xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
  3611. mp, bp, XFS_BUF_ADDR(bp));
  3612. break;
  3613. }
  3614. }
  3615. xfs_buf_free(bp);
  3616. return error;
  3617. }
  3618. /*
  3619. * xfs_free_file_space()
  3620. * This routine frees disk space for the given file.
  3621. *
  3622. * This routine is only called by xfs_change_file_space
  3623. * for an UNRESVSP type call.
  3624. *
  3625. * RETURNS:
  3626. * 0 on success
  3627. * errno on error
  3628. *
  3629. */
  3630. STATIC int
  3631. xfs_free_file_space(
  3632. xfs_inode_t *ip,
  3633. xfs_off_t offset,
  3634. xfs_off_t len,
  3635. int attr_flags)
  3636. {
  3637. bhv_vnode_t *vp;
  3638. int committed;
  3639. int done;
  3640. xfs_off_t end_dmi_offset;
  3641. xfs_fileoff_t endoffset_fsb;
  3642. int error;
  3643. xfs_fsblock_t firstfsb;
  3644. xfs_bmap_free_t free_list;
  3645. xfs_bmbt_irec_t imap;
  3646. xfs_off_t ioffset;
  3647. xfs_extlen_t mod=0;
  3648. xfs_mount_t *mp;
  3649. int nimap;
  3650. uint resblks;
  3651. uint rounding;
  3652. int rt;
  3653. xfs_fileoff_t startoffset_fsb;
  3654. xfs_trans_t *tp;
  3655. int need_iolock = 1;
  3656. vp = XFS_ITOV(ip);
  3657. mp = ip->i_mount;
  3658. xfs_itrace_entry(ip);
  3659. if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
  3660. return error;
  3661. error = 0;
  3662. if (len <= 0) /* if nothing being freed */
  3663. return error;
  3664. rt = XFS_IS_REALTIME_INODE(ip);
  3665. startoffset_fsb = XFS_B_TO_FSB(mp, offset);
  3666. end_dmi_offset = offset + len;
  3667. endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
  3668. if (offset < ip->i_size && (attr_flags & ATTR_DMI) == 0 &&
  3669. DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
  3670. if (end_dmi_offset > ip->i_size)
  3671. end_dmi_offset = ip->i_size;
  3672. error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, vp,
  3673. offset, end_dmi_offset - offset,
  3674. AT_DELAY_FLAG(attr_flags), NULL);
  3675. if (error)
  3676. return error;
  3677. }
  3678. if (attr_flags & ATTR_NOLOCK)
  3679. need_iolock = 0;
  3680. if (need_iolock) {
  3681. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  3682. vn_iowait(ip); /* wait for the completion of any pending DIOs */
  3683. }
  3684. rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
  3685. ioffset = offset & ~(rounding - 1);
  3686. if (VN_CACHED(vp) != 0) {
  3687. xfs_inval_cached_trace(ip, ioffset, -1, ioffset, -1);
  3688. error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
  3689. if (error)
  3690. goto out_unlock_iolock;
  3691. }
  3692. /*
  3693. * Need to zero the stuff we're not freeing, on disk.
  3694. * If its a realtime file & can't use unwritten extents then we
  3695. * actually need to zero the extent edges. Otherwise xfs_bunmapi
  3696. * will take care of it for us.
  3697. */
  3698. if (rt && !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
  3699. nimap = 1;
  3700. error = xfs_bmapi(NULL, ip, startoffset_fsb,
  3701. 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
  3702. if (error)
  3703. goto out_unlock_iolock;
  3704. ASSERT(nimap == 0 || nimap == 1);
  3705. if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
  3706. xfs_daddr_t block;
  3707. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  3708. block = imap.br_startblock;
  3709. mod = do_div(block, mp->m_sb.sb_rextsize);
  3710. if (mod)
  3711. startoffset_fsb += mp->m_sb.sb_rextsize - mod;
  3712. }
  3713. nimap = 1;
  3714. error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
  3715. 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
  3716. if (error)
  3717. goto out_unlock_iolock;
  3718. ASSERT(nimap == 0 || nimap == 1);
  3719. if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
  3720. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  3721. mod++;
  3722. if (mod && (mod != mp->m_sb.sb_rextsize))
  3723. endoffset_fsb -= mod;
  3724. }
  3725. }
  3726. if ((done = (endoffset_fsb <= startoffset_fsb)))
  3727. /*
  3728. * One contiguous piece to clear
  3729. */
  3730. error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
  3731. else {
  3732. /*
  3733. * Some full blocks, possibly two pieces to clear
  3734. */
  3735. if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
  3736. error = xfs_zero_remaining_bytes(ip, offset,
  3737. XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
  3738. if (!error &&
  3739. XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
  3740. error = xfs_zero_remaining_bytes(ip,
  3741. XFS_FSB_TO_B(mp, endoffset_fsb),
  3742. offset + len - 1);
  3743. }
  3744. /*
  3745. * free file space until done or until there is an error
  3746. */
  3747. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  3748. while (!error && !done) {
  3749. /*
  3750. * allocate and setup the transaction. Allow this
  3751. * transaction to dip into the reserve blocks to ensure
  3752. * the freeing of the space succeeds at ENOSPC.
  3753. */
  3754. tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
  3755. tp->t_flags |= XFS_TRANS_RESERVE;
  3756. error = xfs_trans_reserve(tp,
  3757. resblks,
  3758. XFS_WRITE_LOG_RES(mp),
  3759. 0,
  3760. XFS_TRANS_PERM_LOG_RES,
  3761. XFS_WRITE_LOG_COUNT);
  3762. /*
  3763. * check for running out of space
  3764. */
  3765. if (error) {
  3766. /*
  3767. * Free the transaction structure.
  3768. */
  3769. ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  3770. xfs_trans_cancel(tp, 0);
  3771. break;
  3772. }
  3773. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3774. error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
  3775. ip->i_udquot, ip->i_gdquot, resblks, 0,
  3776. XFS_QMOPT_RES_REGBLKS);
  3777. if (error)
  3778. goto error1;
  3779. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  3780. xfs_trans_ihold(tp, ip);
  3781. /*
  3782. * issue the bunmapi() call to free the blocks
  3783. */
  3784. XFS_BMAP_INIT(&free_list, &firstfsb);
  3785. error = xfs_bunmapi(tp, ip, startoffset_fsb,
  3786. endoffset_fsb - startoffset_fsb,
  3787. 0, 2, &firstfsb, &free_list, NULL, &done);
  3788. if (error) {
  3789. goto error0;
  3790. }
  3791. /*
  3792. * complete the transaction
  3793. */
  3794. error = xfs_bmap_finish(&tp, &free_list, &committed);
  3795. if (error) {
  3796. goto error0;
  3797. }
  3798. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  3799. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3800. }
  3801. out_unlock_iolock:
  3802. if (need_iolock)
  3803. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  3804. return error;
  3805. error0:
  3806. xfs_bmap_cancel(&free_list);
  3807. error1:
  3808. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  3809. xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
  3810. XFS_ILOCK_EXCL);
  3811. return error;
  3812. }
  3813. /*
  3814. * xfs_change_file_space()
  3815. * This routine allocates or frees disk space for the given file.
  3816. * The user specified parameters are checked for alignment and size
  3817. * limitations.
  3818. *
  3819. * RETURNS:
  3820. * 0 on success
  3821. * errno on error
  3822. *
  3823. */
  3824. int
  3825. xfs_change_file_space(
  3826. xfs_inode_t *ip,
  3827. int cmd,
  3828. xfs_flock64_t *bf,
  3829. xfs_off_t offset,
  3830. cred_t *credp,
  3831. int attr_flags)
  3832. {
  3833. xfs_mount_t *mp = ip->i_mount;
  3834. int clrprealloc;
  3835. int error;
  3836. xfs_fsize_t fsize;
  3837. int setprealloc;
  3838. xfs_off_t startoffset;
  3839. xfs_off_t llen;
  3840. xfs_trans_t *tp;
  3841. bhv_vattr_t va;
  3842. xfs_itrace_entry(ip);
  3843. /*
  3844. * must be a regular file and have write permission
  3845. */
  3846. if (!S_ISREG(ip->i_d.di_mode))
  3847. return XFS_ERROR(EINVAL);
  3848. xfs_ilock(ip, XFS_ILOCK_SHARED);
  3849. if ((error = xfs_iaccess(ip, S_IWUSR, credp))) {
  3850. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  3851. return error;
  3852. }
  3853. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  3854. switch (bf->l_whence) {
  3855. case 0: /*SEEK_SET*/
  3856. break;
  3857. case 1: /*SEEK_CUR*/
  3858. bf->l_start += offset;
  3859. break;
  3860. case 2: /*SEEK_END*/
  3861. bf->l_start += ip->i_size;
  3862. break;
  3863. default:
  3864. return XFS_ERROR(EINVAL);
  3865. }
  3866. llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
  3867. if ( (bf->l_start < 0)
  3868. || (bf->l_start > XFS_MAXIOFFSET(mp))
  3869. || (bf->l_start + llen < 0)
  3870. || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
  3871. return XFS_ERROR(EINVAL);
  3872. bf->l_whence = 0;
  3873. startoffset = bf->l_start;
  3874. fsize = ip->i_size;
  3875. /*
  3876. * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
  3877. * file space.
  3878. * These calls do NOT zero the data space allocated to the file,
  3879. * nor do they change the file size.
  3880. *
  3881. * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
  3882. * space.
  3883. * These calls cause the new file data to be zeroed and the file
  3884. * size to be changed.
  3885. */
  3886. setprealloc = clrprealloc = 0;
  3887. switch (cmd) {
  3888. case XFS_IOC_RESVSP:
  3889. case XFS_IOC_RESVSP64:
  3890. error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
  3891. 1, attr_flags);
  3892. if (error)
  3893. return error;
  3894. setprealloc = 1;
  3895. break;
  3896. case XFS_IOC_UNRESVSP:
  3897. case XFS_IOC_UNRESVSP64:
  3898. if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
  3899. attr_flags)))
  3900. return error;
  3901. break;
  3902. case XFS_IOC_ALLOCSP:
  3903. case XFS_IOC_ALLOCSP64:
  3904. case XFS_IOC_FREESP:
  3905. case XFS_IOC_FREESP64:
  3906. if (startoffset > fsize) {
  3907. error = xfs_alloc_file_space(ip, fsize,
  3908. startoffset - fsize, 0, attr_flags);
  3909. if (error)
  3910. break;
  3911. }
  3912. va.va_mask = XFS_AT_SIZE;
  3913. va.va_size = startoffset;
  3914. error = xfs_setattr(ip, &va, attr_flags, credp);
  3915. if (error)
  3916. return error;
  3917. clrprealloc = 1;
  3918. break;
  3919. default:
  3920. ASSERT(0);
  3921. return XFS_ERROR(EINVAL);
  3922. }
  3923. /*
  3924. * update the inode timestamp, mode, and prealloc flag bits
  3925. */
  3926. tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
  3927. if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
  3928. 0, 0, 0))) {
  3929. /* ASSERT(0); */
  3930. xfs_trans_cancel(tp, 0);
  3931. return error;
  3932. }
  3933. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3934. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  3935. xfs_trans_ihold(tp, ip);
  3936. if ((attr_flags & ATTR_DMI) == 0) {
  3937. ip->i_d.di_mode &= ~S_ISUID;
  3938. /*
  3939. * Note that we don't have to worry about mandatory
  3940. * file locking being disabled here because we only
  3941. * clear the S_ISGID bit if the Group execute bit is
  3942. * on, but if it was on then mandatory locking wouldn't
  3943. * have been enabled.
  3944. */
  3945. if (ip->i_d.di_mode & S_IXGRP)
  3946. ip->i_d.di_mode &= ~S_ISGID;
  3947. xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  3948. }
  3949. if (setprealloc)
  3950. ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
  3951. else if (clrprealloc)
  3952. ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
  3953. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  3954. xfs_trans_set_sync(tp);
  3955. error = xfs_trans_commit(tp, 0);
  3956. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3957. return error;
  3958. }