xfs_vnodeops.c 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424
  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. * The maximum pathlen is 1024 bytes. Since the minimum file system
  828. * blocksize is 512 bytes, we can get a max of 2 extents back from
  829. * bmapi.
  830. */
  831. #define SYMLINK_MAPS 2
  832. STATIC int
  833. xfs_readlink_bmap(
  834. xfs_inode_t *ip,
  835. char *link)
  836. {
  837. xfs_mount_t *mp = ip->i_mount;
  838. int pathlen = ip->i_d.di_size;
  839. int nmaps = SYMLINK_MAPS;
  840. xfs_bmbt_irec_t mval[SYMLINK_MAPS];
  841. xfs_daddr_t d;
  842. int byte_cnt;
  843. int n;
  844. xfs_buf_t *bp;
  845. int error = 0;
  846. error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
  847. mval, &nmaps, NULL, NULL);
  848. if (error)
  849. goto out;
  850. for (n = 0; n < nmaps; n++) {
  851. d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
  852. byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
  853. bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0);
  854. error = XFS_BUF_GETERROR(bp);
  855. if (error) {
  856. xfs_ioerror_alert("xfs_readlink",
  857. ip->i_mount, bp, XFS_BUF_ADDR(bp));
  858. xfs_buf_relse(bp);
  859. goto out;
  860. }
  861. if (pathlen < byte_cnt)
  862. byte_cnt = pathlen;
  863. pathlen -= byte_cnt;
  864. memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
  865. xfs_buf_relse(bp);
  866. }
  867. link[ip->i_d.di_size] = '\0';
  868. error = 0;
  869. out:
  870. return error;
  871. }
  872. int
  873. xfs_readlink(
  874. xfs_inode_t *ip,
  875. char *link)
  876. {
  877. xfs_mount_t *mp = ip->i_mount;
  878. int pathlen;
  879. int error = 0;
  880. xfs_itrace_entry(ip);
  881. if (XFS_FORCED_SHUTDOWN(mp))
  882. return XFS_ERROR(EIO);
  883. xfs_ilock(ip, XFS_ILOCK_SHARED);
  884. ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
  885. ASSERT(ip->i_d.di_size <= MAXPATHLEN);
  886. pathlen = ip->i_d.di_size;
  887. if (!pathlen)
  888. goto out;
  889. if (ip->i_df.if_flags & XFS_IFINLINE) {
  890. memcpy(link, ip->i_df.if_u1.if_data, pathlen);
  891. link[pathlen] = '\0';
  892. } else {
  893. error = xfs_readlink_bmap(ip, link);
  894. }
  895. out:
  896. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  897. return error;
  898. }
  899. /*
  900. * xfs_fsync
  901. *
  902. * This is called to sync the inode and its data out to disk.
  903. * We need to hold the I/O lock while flushing the data, and
  904. * the inode lock while flushing the inode. The inode lock CANNOT
  905. * be held while flushing the data, so acquire after we're done
  906. * with that.
  907. */
  908. int
  909. xfs_fsync(
  910. xfs_inode_t *ip,
  911. int flag,
  912. xfs_off_t start,
  913. xfs_off_t stop)
  914. {
  915. xfs_trans_t *tp;
  916. int error;
  917. int log_flushed = 0, changed = 1;
  918. xfs_itrace_entry(ip);
  919. ASSERT(start >= 0 && stop >= -1);
  920. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  921. return XFS_ERROR(EIO);
  922. if (flag & FSYNC_DATA)
  923. filemap_fdatawait(vn_to_inode(XFS_ITOV(ip))->i_mapping);
  924. /*
  925. * We always need to make sure that the required inode state
  926. * is safe on disk. The vnode might be clean but because
  927. * of committed transactions that haven't hit the disk yet.
  928. * Likewise, there could be unflushed non-transactional
  929. * changes to the inode core that have to go to disk.
  930. *
  931. * The following code depends on one assumption: that
  932. * any transaction that changes an inode logs the core
  933. * because it has to change some field in the inode core
  934. * (typically nextents or nblocks). That assumption
  935. * implies that any transactions against an inode will
  936. * catch any non-transactional updates. If inode-altering
  937. * transactions exist that violate this assumption, the
  938. * code breaks. Right now, it figures that if the involved
  939. * update_* field is clear and the inode is unpinned, the
  940. * inode is clean. Either it's been flushed or it's been
  941. * committed and the commit has hit the disk unpinning the inode.
  942. * (Note that xfs_inode_item_format() called at commit clears
  943. * the update_* fields.)
  944. */
  945. xfs_ilock(ip, XFS_ILOCK_SHARED);
  946. /* If we are flushing data then we care about update_size
  947. * being set, otherwise we care about update_core
  948. */
  949. if ((flag & FSYNC_DATA) ?
  950. (ip->i_update_size == 0) :
  951. (ip->i_update_core == 0)) {
  952. /*
  953. * Timestamps/size haven't changed since last inode
  954. * flush or inode transaction commit. That means
  955. * either nothing got written or a transaction
  956. * committed which caught the updates. If the
  957. * latter happened and the transaction hasn't
  958. * hit the disk yet, the inode will be still
  959. * be pinned. If it is, force the log.
  960. */
  961. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  962. if (xfs_ipincount(ip)) {
  963. _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
  964. XFS_LOG_FORCE |
  965. ((flag & FSYNC_WAIT)
  966. ? XFS_LOG_SYNC : 0),
  967. &log_flushed);
  968. } else {
  969. /*
  970. * If the inode is not pinned and nothing
  971. * has changed we don't need to flush the
  972. * cache.
  973. */
  974. changed = 0;
  975. }
  976. error = 0;
  977. } else {
  978. /*
  979. * Kick off a transaction to log the inode
  980. * core to get the updates. Make it
  981. * sync if FSYNC_WAIT is passed in (which
  982. * is done by everybody but specfs). The
  983. * sync transaction will also force the log.
  984. */
  985. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  986. tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
  987. if ((error = xfs_trans_reserve(tp, 0,
  988. XFS_FSYNC_TS_LOG_RES(ip->i_mount),
  989. 0, 0, 0))) {
  990. xfs_trans_cancel(tp, 0);
  991. return error;
  992. }
  993. xfs_ilock(ip, XFS_ILOCK_EXCL);
  994. /*
  995. * Note - it's possible that we might have pushed
  996. * ourselves out of the way during trans_reserve
  997. * which would flush the inode. But there's no
  998. * guarantee that the inode buffer has actually
  999. * gone out yet (it's delwri). Plus the buffer
  1000. * could be pinned anyway if it's part of an
  1001. * inode in another recent transaction. So we
  1002. * play it safe and fire off the transaction anyway.
  1003. */
  1004. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  1005. xfs_trans_ihold(tp, ip);
  1006. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1007. if (flag & FSYNC_WAIT)
  1008. xfs_trans_set_sync(tp);
  1009. error = _xfs_trans_commit(tp, 0, &log_flushed);
  1010. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1011. }
  1012. if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
  1013. /*
  1014. * If the log write didn't issue an ordered tag we need
  1015. * to flush the disk cache for the data device now.
  1016. */
  1017. if (!log_flushed)
  1018. xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
  1019. /*
  1020. * If this inode is on the RT dev we need to flush that
  1021. * cache as well.
  1022. */
  1023. if (XFS_IS_REALTIME_INODE(ip))
  1024. xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
  1025. }
  1026. return error;
  1027. }
  1028. /*
  1029. * This is called by xfs_inactive to free any blocks beyond eof
  1030. * when the link count isn't zero and by xfs_dm_punch_hole() when
  1031. * punching a hole to EOF.
  1032. */
  1033. int
  1034. xfs_free_eofblocks(
  1035. xfs_mount_t *mp,
  1036. xfs_inode_t *ip,
  1037. int flags)
  1038. {
  1039. xfs_trans_t *tp;
  1040. int error;
  1041. xfs_fileoff_t end_fsb;
  1042. xfs_fileoff_t last_fsb;
  1043. xfs_filblks_t map_len;
  1044. int nimaps;
  1045. xfs_bmbt_irec_t imap;
  1046. int use_iolock = (flags & XFS_FREE_EOF_LOCK);
  1047. /*
  1048. * Figure out if there are any blocks beyond the end
  1049. * of the file. If not, then there is nothing to do.
  1050. */
  1051. end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
  1052. last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
  1053. map_len = last_fsb - end_fsb;
  1054. if (map_len <= 0)
  1055. return 0;
  1056. nimaps = 1;
  1057. xfs_ilock(ip, XFS_ILOCK_SHARED);
  1058. error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
  1059. NULL, 0, &imap, &nimaps, NULL, NULL);
  1060. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  1061. if (!error && (nimaps != 0) &&
  1062. (imap.br_startblock != HOLESTARTBLOCK ||
  1063. ip->i_delayed_blks)) {
  1064. /*
  1065. * Attach the dquots to the inode up front.
  1066. */
  1067. if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
  1068. return error;
  1069. /*
  1070. * There are blocks after the end of file.
  1071. * Free them up now by truncating the file to
  1072. * its current size.
  1073. */
  1074. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  1075. /*
  1076. * Do the xfs_itruncate_start() call before
  1077. * reserving any log space because
  1078. * itruncate_start will call into the buffer
  1079. * cache and we can't
  1080. * do that within a transaction.
  1081. */
  1082. if (use_iolock)
  1083. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  1084. error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
  1085. ip->i_size);
  1086. if (error) {
  1087. xfs_trans_cancel(tp, 0);
  1088. if (use_iolock)
  1089. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1090. return error;
  1091. }
  1092. error = xfs_trans_reserve(tp, 0,
  1093. XFS_ITRUNCATE_LOG_RES(mp),
  1094. 0, XFS_TRANS_PERM_LOG_RES,
  1095. XFS_ITRUNCATE_LOG_COUNT);
  1096. if (error) {
  1097. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1098. xfs_trans_cancel(tp, 0);
  1099. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1100. return error;
  1101. }
  1102. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1103. xfs_trans_ijoin(tp, ip,
  1104. XFS_IOLOCK_EXCL |
  1105. XFS_ILOCK_EXCL);
  1106. xfs_trans_ihold(tp, ip);
  1107. error = xfs_itruncate_finish(&tp, ip,
  1108. ip->i_size,
  1109. XFS_DATA_FORK,
  1110. 0);
  1111. /*
  1112. * If we get an error at this point we
  1113. * simply don't bother truncating the file.
  1114. */
  1115. if (error) {
  1116. xfs_trans_cancel(tp,
  1117. (XFS_TRANS_RELEASE_LOG_RES |
  1118. XFS_TRANS_ABORT));
  1119. } else {
  1120. error = xfs_trans_commit(tp,
  1121. XFS_TRANS_RELEASE_LOG_RES);
  1122. }
  1123. xfs_iunlock(ip, (use_iolock ? (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)
  1124. : XFS_ILOCK_EXCL));
  1125. }
  1126. return error;
  1127. }
  1128. /*
  1129. * Free a symlink that has blocks associated with it.
  1130. */
  1131. STATIC int
  1132. xfs_inactive_symlink_rmt(
  1133. xfs_inode_t *ip,
  1134. xfs_trans_t **tpp)
  1135. {
  1136. xfs_buf_t *bp;
  1137. int committed;
  1138. int done;
  1139. int error;
  1140. xfs_fsblock_t first_block;
  1141. xfs_bmap_free_t free_list;
  1142. int i;
  1143. xfs_mount_t *mp;
  1144. xfs_bmbt_irec_t mval[SYMLINK_MAPS];
  1145. int nmaps;
  1146. xfs_trans_t *ntp;
  1147. int size;
  1148. xfs_trans_t *tp;
  1149. tp = *tpp;
  1150. mp = ip->i_mount;
  1151. ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
  1152. /*
  1153. * We're freeing a symlink that has some
  1154. * blocks allocated to it. Free the
  1155. * blocks here. We know that we've got
  1156. * either 1 or 2 extents and that we can
  1157. * free them all in one bunmapi call.
  1158. */
  1159. ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
  1160. if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
  1161. XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
  1162. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1163. xfs_trans_cancel(tp, 0);
  1164. *tpp = NULL;
  1165. return error;
  1166. }
  1167. /*
  1168. * Lock the inode, fix the size, and join it to the transaction.
  1169. * Hold it so in the normal path, we still have it locked for
  1170. * the second transaction. In the error paths we need it
  1171. * held so the cancel won't rele it, see below.
  1172. */
  1173. xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1174. size = (int)ip->i_d.di_size;
  1175. ip->i_d.di_size = 0;
  1176. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  1177. xfs_trans_ihold(tp, ip);
  1178. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1179. /*
  1180. * Find the block(s) so we can inval and unmap them.
  1181. */
  1182. done = 0;
  1183. XFS_BMAP_INIT(&free_list, &first_block);
  1184. nmaps = ARRAY_SIZE(mval);
  1185. if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
  1186. XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
  1187. &free_list, NULL)))
  1188. goto error0;
  1189. /*
  1190. * Invalidate the block(s).
  1191. */
  1192. for (i = 0; i < nmaps; i++) {
  1193. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
  1194. XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
  1195. XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
  1196. xfs_trans_binval(tp, bp);
  1197. }
  1198. /*
  1199. * Unmap the dead block(s) to the free_list.
  1200. */
  1201. if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
  1202. &first_block, &free_list, NULL, &done)))
  1203. goto error1;
  1204. ASSERT(done);
  1205. /*
  1206. * Commit the first transaction. This logs the EFI and the inode.
  1207. */
  1208. if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
  1209. goto error1;
  1210. /*
  1211. * The transaction must have been committed, since there were
  1212. * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
  1213. * The new tp has the extent freeing and EFDs.
  1214. */
  1215. ASSERT(committed);
  1216. /*
  1217. * The first xact was committed, so add the inode to the new one.
  1218. * Mark it dirty so it will be logged and moved forward in the log as
  1219. * part of every commit.
  1220. */
  1221. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  1222. xfs_trans_ihold(tp, ip);
  1223. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1224. /*
  1225. * Get a new, empty transaction to return to our caller.
  1226. */
  1227. ntp = xfs_trans_dup(tp);
  1228. /*
  1229. * Commit the transaction containing extent freeing and EFDs.
  1230. * If we get an error on the commit here or on the reserve below,
  1231. * we need to unlock the inode since the new transaction doesn't
  1232. * have the inode attached.
  1233. */
  1234. error = xfs_trans_commit(tp, 0);
  1235. tp = ntp;
  1236. if (error) {
  1237. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1238. goto error0;
  1239. }
  1240. /*
  1241. * Remove the memory for extent descriptions (just bookkeeping).
  1242. */
  1243. if (ip->i_df.if_bytes)
  1244. xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
  1245. ASSERT(ip->i_df.if_bytes == 0);
  1246. /*
  1247. * Put an itruncate log reservation in the new transaction
  1248. * for our caller.
  1249. */
  1250. if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
  1251. XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
  1252. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1253. goto error0;
  1254. }
  1255. /*
  1256. * Return with the inode locked but not joined to the transaction.
  1257. */
  1258. *tpp = tp;
  1259. return 0;
  1260. error1:
  1261. xfs_bmap_cancel(&free_list);
  1262. error0:
  1263. /*
  1264. * Have to come here with the inode locked and either
  1265. * (held and in the transaction) or (not in the transaction).
  1266. * If the inode isn't held then cancel would iput it, but
  1267. * that's wrong since this is inactive and the vnode ref
  1268. * count is 0 already.
  1269. * Cancel won't do anything to the inode if held, but it still
  1270. * needs to be locked until the cancel is done, if it was
  1271. * joined to the transaction.
  1272. */
  1273. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  1274. xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1275. *tpp = NULL;
  1276. return error;
  1277. }
  1278. STATIC int
  1279. xfs_inactive_symlink_local(
  1280. xfs_inode_t *ip,
  1281. xfs_trans_t **tpp)
  1282. {
  1283. int error;
  1284. ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
  1285. /*
  1286. * We're freeing a symlink which fit into
  1287. * the inode. Just free the memory used
  1288. * to hold the old symlink.
  1289. */
  1290. error = xfs_trans_reserve(*tpp, 0,
  1291. XFS_ITRUNCATE_LOG_RES(ip->i_mount),
  1292. 0, XFS_TRANS_PERM_LOG_RES,
  1293. XFS_ITRUNCATE_LOG_COUNT);
  1294. if (error) {
  1295. xfs_trans_cancel(*tpp, 0);
  1296. *tpp = NULL;
  1297. return error;
  1298. }
  1299. xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  1300. /*
  1301. * Zero length symlinks _can_ exist.
  1302. */
  1303. if (ip->i_df.if_bytes > 0) {
  1304. xfs_idata_realloc(ip,
  1305. -(ip->i_df.if_bytes),
  1306. XFS_DATA_FORK);
  1307. ASSERT(ip->i_df.if_bytes == 0);
  1308. }
  1309. return 0;
  1310. }
  1311. STATIC int
  1312. xfs_inactive_attrs(
  1313. xfs_inode_t *ip,
  1314. xfs_trans_t **tpp)
  1315. {
  1316. xfs_trans_t *tp;
  1317. int error;
  1318. xfs_mount_t *mp;
  1319. ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
  1320. tp = *tpp;
  1321. mp = ip->i_mount;
  1322. ASSERT(ip->i_d.di_forkoff != 0);
  1323. xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1324. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1325. error = xfs_attr_inactive(ip);
  1326. if (error) {
  1327. *tpp = NULL;
  1328. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1329. return error; /* goto out */
  1330. }
  1331. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  1332. error = xfs_trans_reserve(tp, 0,
  1333. XFS_IFREE_LOG_RES(mp),
  1334. 0, XFS_TRANS_PERM_LOG_RES,
  1335. XFS_INACTIVE_LOG_COUNT);
  1336. if (error) {
  1337. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1338. xfs_trans_cancel(tp, 0);
  1339. *tpp = NULL;
  1340. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1341. return error;
  1342. }
  1343. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1344. xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1345. xfs_trans_ihold(tp, ip);
  1346. xfs_idestroy_fork(ip, XFS_ATTR_FORK);
  1347. ASSERT(ip->i_d.di_anextents == 0);
  1348. *tpp = tp;
  1349. return 0;
  1350. }
  1351. int
  1352. xfs_release(
  1353. xfs_inode_t *ip)
  1354. {
  1355. bhv_vnode_t *vp = XFS_ITOV(ip);
  1356. xfs_mount_t *mp = ip->i_mount;
  1357. int error;
  1358. if (!VN_ISREG(vp) || (ip->i_d.di_mode == 0))
  1359. return 0;
  1360. /* If this is a read-only mount, don't do this (would generate I/O) */
  1361. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1362. return 0;
  1363. if (!XFS_FORCED_SHUTDOWN(mp)) {
  1364. int truncated;
  1365. /*
  1366. * If we are using filestreams, and we have an unlinked
  1367. * file that we are processing the last close on, then nothing
  1368. * will be able to reopen and write to this file. Purge this
  1369. * inode from the filestreams cache so that it doesn't delay
  1370. * teardown of the inode.
  1371. */
  1372. if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
  1373. xfs_filestream_deassociate(ip);
  1374. /*
  1375. * If we previously truncated this file and removed old data
  1376. * in the process, we want to initiate "early" writeout on
  1377. * the last close. This is an attempt to combat the notorious
  1378. * NULL files problem which is particularly noticable from a
  1379. * truncate down, buffered (re-)write (delalloc), followed by
  1380. * a crash. What we are effectively doing here is
  1381. * significantly reducing the time window where we'd otherwise
  1382. * be exposed to that problem.
  1383. */
  1384. truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
  1385. if (truncated && VN_DIRTY(vp) && ip->i_delayed_blks > 0)
  1386. xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE);
  1387. }
  1388. #ifdef HAVE_REFCACHE
  1389. /* If we are in the NFS reference cache then don't do this now */
  1390. if (ip->i_refcache)
  1391. return 0;
  1392. #endif
  1393. if (ip->i_d.di_nlink != 0) {
  1394. if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
  1395. ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
  1396. ip->i_delayed_blks > 0)) &&
  1397. (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
  1398. (!(ip->i_d.di_flags &
  1399. (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
  1400. error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
  1401. if (error)
  1402. return error;
  1403. }
  1404. }
  1405. return 0;
  1406. }
  1407. /*
  1408. * xfs_inactive
  1409. *
  1410. * This is called when the vnode reference count for the vnode
  1411. * goes to zero. If the file has been unlinked, then it must
  1412. * now be truncated. Also, we clear all of the read-ahead state
  1413. * kept for the inode here since the file is now closed.
  1414. */
  1415. int
  1416. xfs_inactive(
  1417. xfs_inode_t *ip)
  1418. {
  1419. bhv_vnode_t *vp = XFS_ITOV(ip);
  1420. xfs_bmap_free_t free_list;
  1421. xfs_fsblock_t first_block;
  1422. int committed;
  1423. xfs_trans_t *tp;
  1424. xfs_mount_t *mp;
  1425. int error;
  1426. int truncate;
  1427. xfs_itrace_entry(ip);
  1428. /*
  1429. * If the inode is already free, then there can be nothing
  1430. * to clean up here.
  1431. */
  1432. if (ip->i_d.di_mode == 0 || VN_BAD(vp)) {
  1433. ASSERT(ip->i_df.if_real_bytes == 0);
  1434. ASSERT(ip->i_df.if_broot_bytes == 0);
  1435. return VN_INACTIVE_CACHE;
  1436. }
  1437. /*
  1438. * Only do a truncate if it's a regular file with
  1439. * some actual space in it. It's OK to look at the
  1440. * inode's fields without the lock because we're the
  1441. * only one with a reference to the inode.
  1442. */
  1443. truncate = ((ip->i_d.di_nlink == 0) &&
  1444. ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
  1445. (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
  1446. ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
  1447. mp = ip->i_mount;
  1448. if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY)) {
  1449. (void) XFS_SEND_DESTROY(mp, vp, DM_RIGHT_NULL);
  1450. }
  1451. error = 0;
  1452. /* If this is a read-only mount, don't do this (would generate I/O) */
  1453. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1454. goto out;
  1455. if (ip->i_d.di_nlink != 0) {
  1456. if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
  1457. ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
  1458. ip->i_delayed_blks > 0)) &&
  1459. (ip->i_df.if_flags & XFS_IFEXTENTS) &&
  1460. (!(ip->i_d.di_flags &
  1461. (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
  1462. (ip->i_delayed_blks != 0)))) {
  1463. error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
  1464. if (error)
  1465. return VN_INACTIVE_CACHE;
  1466. }
  1467. goto out;
  1468. }
  1469. ASSERT(ip->i_d.di_nlink == 0);
  1470. if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
  1471. return VN_INACTIVE_CACHE;
  1472. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  1473. if (truncate) {
  1474. /*
  1475. * Do the xfs_itruncate_start() call before
  1476. * reserving any log space because itruncate_start
  1477. * will call into the buffer cache and we can't
  1478. * do that within a transaction.
  1479. */
  1480. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  1481. error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
  1482. if (error) {
  1483. xfs_trans_cancel(tp, 0);
  1484. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1485. return VN_INACTIVE_CACHE;
  1486. }
  1487. error = xfs_trans_reserve(tp, 0,
  1488. XFS_ITRUNCATE_LOG_RES(mp),
  1489. 0, XFS_TRANS_PERM_LOG_RES,
  1490. XFS_ITRUNCATE_LOG_COUNT);
  1491. if (error) {
  1492. /* Don't call itruncate_cleanup */
  1493. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1494. xfs_trans_cancel(tp, 0);
  1495. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1496. return VN_INACTIVE_CACHE;
  1497. }
  1498. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1499. xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1500. xfs_trans_ihold(tp, ip);
  1501. /*
  1502. * normally, we have to run xfs_itruncate_finish sync.
  1503. * But if filesystem is wsync and we're in the inactive
  1504. * path, then we know that nlink == 0, and that the
  1505. * xaction that made nlink == 0 is permanently committed
  1506. * since xfs_remove runs as a synchronous transaction.
  1507. */
  1508. error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
  1509. (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
  1510. if (error) {
  1511. xfs_trans_cancel(tp,
  1512. XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  1513. xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1514. return VN_INACTIVE_CACHE;
  1515. }
  1516. } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
  1517. /*
  1518. * If we get an error while cleaning up a
  1519. * symlink we bail out.
  1520. */
  1521. error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
  1522. xfs_inactive_symlink_rmt(ip, &tp) :
  1523. xfs_inactive_symlink_local(ip, &tp);
  1524. if (error) {
  1525. ASSERT(tp == NULL);
  1526. return VN_INACTIVE_CACHE;
  1527. }
  1528. xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1529. xfs_trans_ihold(tp, ip);
  1530. } else {
  1531. error = xfs_trans_reserve(tp, 0,
  1532. XFS_IFREE_LOG_RES(mp),
  1533. 0, XFS_TRANS_PERM_LOG_RES,
  1534. XFS_INACTIVE_LOG_COUNT);
  1535. if (error) {
  1536. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1537. xfs_trans_cancel(tp, 0);
  1538. return VN_INACTIVE_CACHE;
  1539. }
  1540. xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  1541. xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1542. xfs_trans_ihold(tp, ip);
  1543. }
  1544. /*
  1545. * If there are attributes associated with the file
  1546. * then blow them away now. The code calls a routine
  1547. * that recursively deconstructs the attribute fork.
  1548. * We need to just commit the current transaction
  1549. * because we can't use it for xfs_attr_inactive().
  1550. */
  1551. if (ip->i_d.di_anextents > 0) {
  1552. error = xfs_inactive_attrs(ip, &tp);
  1553. /*
  1554. * If we got an error, the transaction is already
  1555. * cancelled, and the inode is unlocked. Just get out.
  1556. */
  1557. if (error)
  1558. return VN_INACTIVE_CACHE;
  1559. } else if (ip->i_afp) {
  1560. xfs_idestroy_fork(ip, XFS_ATTR_FORK);
  1561. }
  1562. /*
  1563. * Free the inode.
  1564. */
  1565. XFS_BMAP_INIT(&free_list, &first_block);
  1566. error = xfs_ifree(tp, ip, &free_list);
  1567. if (error) {
  1568. /*
  1569. * If we fail to free the inode, shut down. The cancel
  1570. * might do that, we need to make sure. Otherwise the
  1571. * inode might be lost for a long time or forever.
  1572. */
  1573. if (!XFS_FORCED_SHUTDOWN(mp)) {
  1574. cmn_err(CE_NOTE,
  1575. "xfs_inactive: xfs_ifree() returned an error = %d on %s",
  1576. error, mp->m_fsname);
  1577. xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
  1578. }
  1579. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  1580. } else {
  1581. /*
  1582. * Credit the quota account(s). The inode is gone.
  1583. */
  1584. XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
  1585. /*
  1586. * Just ignore errors at this point. There is
  1587. * nothing we can do except to try to keep going.
  1588. */
  1589. (void) xfs_bmap_finish(&tp, &free_list, &committed);
  1590. (void) xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1591. }
  1592. /*
  1593. * Release the dquots held by inode, if any.
  1594. */
  1595. XFS_QM_DQDETACH(mp, ip);
  1596. xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1597. out:
  1598. return VN_INACTIVE_CACHE;
  1599. }
  1600. int
  1601. xfs_lookup(
  1602. xfs_inode_t *dp,
  1603. bhv_vname_t *dentry,
  1604. bhv_vnode_t **vpp)
  1605. {
  1606. xfs_inode_t *ip;
  1607. xfs_ino_t e_inum;
  1608. int error;
  1609. uint lock_mode;
  1610. xfs_itrace_entry(dp);
  1611. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  1612. return XFS_ERROR(EIO);
  1613. lock_mode = xfs_ilock_map_shared(dp);
  1614. error = xfs_dir_lookup_int(dp, lock_mode, dentry, &e_inum, &ip);
  1615. if (!error) {
  1616. *vpp = XFS_ITOV(ip);
  1617. xfs_itrace_ref(ip);
  1618. }
  1619. xfs_iunlock_map_shared(dp, lock_mode);
  1620. return error;
  1621. }
  1622. int
  1623. xfs_create(
  1624. xfs_inode_t *dp,
  1625. bhv_vname_t *dentry,
  1626. mode_t mode,
  1627. xfs_dev_t rdev,
  1628. bhv_vnode_t **vpp,
  1629. cred_t *credp)
  1630. {
  1631. char *name = VNAME(dentry);
  1632. xfs_mount_t *mp = dp->i_mount;
  1633. bhv_vnode_t *dir_vp = XFS_ITOV(dp);
  1634. xfs_inode_t *ip;
  1635. bhv_vnode_t *vp = NULL;
  1636. xfs_trans_t *tp;
  1637. int error;
  1638. xfs_bmap_free_t free_list;
  1639. xfs_fsblock_t first_block;
  1640. boolean_t unlock_dp_on_error = B_FALSE;
  1641. int dm_event_sent = 0;
  1642. uint cancel_flags;
  1643. int committed;
  1644. xfs_prid_t prid;
  1645. struct xfs_dquot *udqp, *gdqp;
  1646. uint resblks;
  1647. int namelen;
  1648. ASSERT(!*vpp);
  1649. xfs_itrace_entry(dp);
  1650. namelen = VNAMELEN(dentry);
  1651. if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
  1652. error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
  1653. dir_vp, DM_RIGHT_NULL, NULL,
  1654. DM_RIGHT_NULL, name, NULL,
  1655. mode, 0, 0);
  1656. if (error)
  1657. return error;
  1658. dm_event_sent = 1;
  1659. }
  1660. if (XFS_FORCED_SHUTDOWN(mp))
  1661. return XFS_ERROR(EIO);
  1662. /* Return through std_return after this point. */
  1663. udqp = gdqp = NULL;
  1664. if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  1665. prid = dp->i_d.di_projid;
  1666. else
  1667. prid = (xfs_prid_t)dfltprid;
  1668. /*
  1669. * Make sure that we have allocated dquot(s) on disk.
  1670. */
  1671. error = XFS_QM_DQVOPALLOC(mp, dp,
  1672. current_fsuid(credp), current_fsgid(credp), prid,
  1673. XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
  1674. if (error)
  1675. goto std_return;
  1676. ip = NULL;
  1677. tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
  1678. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  1679. resblks = XFS_CREATE_SPACE_RES(mp, namelen);
  1680. /*
  1681. * Initially assume that the file does not exist and
  1682. * reserve the resources for that case. If that is not
  1683. * the case we'll drop the one we have and get a more
  1684. * appropriate transaction later.
  1685. */
  1686. error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
  1687. XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
  1688. if (error == ENOSPC) {
  1689. resblks = 0;
  1690. error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
  1691. XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
  1692. }
  1693. if (error) {
  1694. cancel_flags = 0;
  1695. goto error_return;
  1696. }
  1697. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  1698. unlock_dp_on_error = B_TRUE;
  1699. XFS_BMAP_INIT(&free_list, &first_block);
  1700. ASSERT(ip == NULL);
  1701. /*
  1702. * Reserve disk quota and the inode.
  1703. */
  1704. error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
  1705. if (error)
  1706. goto error_return;
  1707. if (resblks == 0 && (error = xfs_dir_canenter(tp, dp, name, namelen)))
  1708. goto error_return;
  1709. error = xfs_dir_ialloc(&tp, dp, mode, 1,
  1710. rdev, credp, prid, resblks > 0,
  1711. &ip, &committed);
  1712. if (error) {
  1713. if (error == ENOSPC)
  1714. goto error_return;
  1715. goto abort_return;
  1716. }
  1717. xfs_itrace_ref(ip);
  1718. /*
  1719. * At this point, we've gotten a newly allocated inode.
  1720. * It is locked (and joined to the transaction).
  1721. */
  1722. ASSERT(ismrlocked (&ip->i_lock, MR_UPDATE));
  1723. /*
  1724. * Now we join the directory inode to the transaction. We do not do it
  1725. * earlier because xfs_dir_ialloc might commit the previous transaction
  1726. * (and release all the locks). An error from here on will result in
  1727. * the transaction cancel unlocking dp so don't do it explicitly in the
  1728. * error path.
  1729. */
  1730. VN_HOLD(dir_vp);
  1731. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  1732. unlock_dp_on_error = B_FALSE;
  1733. error = xfs_dir_createname(tp, dp, name, namelen, ip->i_ino,
  1734. &first_block, &free_list, resblks ?
  1735. resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
  1736. if (error) {
  1737. ASSERT(error != ENOSPC);
  1738. goto abort_return;
  1739. }
  1740. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1741. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  1742. /*
  1743. * If this is a synchronous mount, make sure that the
  1744. * create transaction goes to disk before returning to
  1745. * the user.
  1746. */
  1747. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  1748. xfs_trans_set_sync(tp);
  1749. }
  1750. dp->i_gen++;
  1751. /*
  1752. * Attach the dquot(s) to the inodes and modify them incore.
  1753. * These ids of the inode couldn't have changed since the new
  1754. * inode has been locked ever since it was created.
  1755. */
  1756. XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
  1757. /*
  1758. * xfs_trans_commit normally decrements the vnode ref count
  1759. * when it unlocks the inode. Since we want to return the
  1760. * vnode to the caller, we bump the vnode ref count now.
  1761. */
  1762. IHOLD(ip);
  1763. vp = XFS_ITOV(ip);
  1764. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1765. if (error) {
  1766. xfs_bmap_cancel(&free_list);
  1767. goto abort_rele;
  1768. }
  1769. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1770. if (error) {
  1771. IRELE(ip);
  1772. tp = NULL;
  1773. goto error_return;
  1774. }
  1775. XFS_QM_DQRELE(mp, udqp);
  1776. XFS_QM_DQRELE(mp, gdqp);
  1777. *vpp = vp;
  1778. /* Fallthrough to std_return with error = 0 */
  1779. std_return:
  1780. if ((*vpp || (error != 0 && dm_event_sent != 0)) &&
  1781. DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
  1782. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
  1783. dir_vp, DM_RIGHT_NULL,
  1784. *vpp ? vp:NULL,
  1785. DM_RIGHT_NULL, name, NULL,
  1786. mode, error, 0);
  1787. }
  1788. return error;
  1789. abort_return:
  1790. cancel_flags |= XFS_TRANS_ABORT;
  1791. /* FALLTHROUGH */
  1792. error_return:
  1793. if (tp != NULL)
  1794. xfs_trans_cancel(tp, cancel_flags);
  1795. XFS_QM_DQRELE(mp, udqp);
  1796. XFS_QM_DQRELE(mp, gdqp);
  1797. if (unlock_dp_on_error)
  1798. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  1799. goto std_return;
  1800. abort_rele:
  1801. /*
  1802. * Wait until after the current transaction is aborted to
  1803. * release the inode. This prevents recursive transactions
  1804. * and deadlocks from xfs_inactive.
  1805. */
  1806. cancel_flags |= XFS_TRANS_ABORT;
  1807. xfs_trans_cancel(tp, cancel_flags);
  1808. IRELE(ip);
  1809. XFS_QM_DQRELE(mp, udqp);
  1810. XFS_QM_DQRELE(mp, gdqp);
  1811. goto std_return;
  1812. }
  1813. #ifdef DEBUG
  1814. /*
  1815. * Some counters to see if (and how often) we are hitting some deadlock
  1816. * prevention code paths.
  1817. */
  1818. int xfs_rm_locks;
  1819. int xfs_rm_lock_delays;
  1820. int xfs_rm_attempts;
  1821. #endif
  1822. /*
  1823. * The following routine will lock the inodes associated with the
  1824. * directory and the named entry in the directory. The locks are
  1825. * acquired in increasing inode number.
  1826. *
  1827. * If the entry is "..", then only the directory is locked. The
  1828. * vnode ref count will still include that from the .. entry in
  1829. * this case.
  1830. *
  1831. * There is a deadlock we need to worry about. If the locked directory is
  1832. * in the AIL, it might be blocking up the log. The next inode we lock
  1833. * could be already locked by another thread waiting for log space (e.g
  1834. * a permanent log reservation with a long running transaction (see
  1835. * xfs_itruncate_finish)). To solve this, we must check if the directory
  1836. * is in the ail and use lock_nowait. If we can't lock, we need to
  1837. * drop the inode lock on the directory and try again. xfs_iunlock will
  1838. * potentially push the tail if we were holding up the log.
  1839. */
  1840. STATIC int
  1841. xfs_lock_dir_and_entry(
  1842. xfs_inode_t *dp,
  1843. xfs_inode_t *ip) /* inode of entry 'name' */
  1844. {
  1845. int attempts;
  1846. xfs_ino_t e_inum;
  1847. xfs_inode_t *ips[2];
  1848. xfs_log_item_t *lp;
  1849. #ifdef DEBUG
  1850. xfs_rm_locks++;
  1851. #endif
  1852. attempts = 0;
  1853. again:
  1854. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  1855. e_inum = ip->i_ino;
  1856. xfs_itrace_ref(ip);
  1857. /*
  1858. * We want to lock in increasing inum. Since we've already
  1859. * acquired the lock on the directory, we may need to release
  1860. * if if the inum of the entry turns out to be less.
  1861. */
  1862. if (e_inum > dp->i_ino) {
  1863. /*
  1864. * We are already in the right order, so just
  1865. * lock on the inode of the entry.
  1866. * We need to use nowait if dp is in the AIL.
  1867. */
  1868. lp = (xfs_log_item_t *)dp->i_itemp;
  1869. if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
  1870. if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
  1871. attempts++;
  1872. #ifdef DEBUG
  1873. xfs_rm_attempts++;
  1874. #endif
  1875. /*
  1876. * Unlock dp and try again.
  1877. * xfs_iunlock will try to push the tail
  1878. * if the inode is in the AIL.
  1879. */
  1880. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  1881. if ((attempts % 5) == 0) {
  1882. delay(1); /* Don't just spin the CPU */
  1883. #ifdef DEBUG
  1884. xfs_rm_lock_delays++;
  1885. #endif
  1886. }
  1887. goto again;
  1888. }
  1889. } else {
  1890. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1891. }
  1892. } else if (e_inum < dp->i_ino) {
  1893. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  1894. ips[0] = ip;
  1895. ips[1] = dp;
  1896. xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
  1897. }
  1898. /* else e_inum == dp->i_ino */
  1899. /* This can happen if we're asked to lock /x/..
  1900. * the entry is "..", which is also the parent directory.
  1901. */
  1902. return 0;
  1903. }
  1904. #ifdef DEBUG
  1905. int xfs_locked_n;
  1906. int xfs_small_retries;
  1907. int xfs_middle_retries;
  1908. int xfs_lots_retries;
  1909. int xfs_lock_delays;
  1910. #endif
  1911. /*
  1912. * Bump the subclass so xfs_lock_inodes() acquires each lock with
  1913. * a different value
  1914. */
  1915. static inline int
  1916. xfs_lock_inumorder(int lock_mode, int subclass)
  1917. {
  1918. if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
  1919. lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
  1920. if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
  1921. lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
  1922. return lock_mode;
  1923. }
  1924. /*
  1925. * The following routine will lock n inodes in exclusive mode.
  1926. * We assume the caller calls us with the inodes in i_ino order.
  1927. *
  1928. * We need to detect deadlock where an inode that we lock
  1929. * is in the AIL and we start waiting for another inode that is locked
  1930. * by a thread in a long running transaction (such as truncate). This can
  1931. * result in deadlock since the long running trans might need to wait
  1932. * for the inode we just locked in order to push the tail and free space
  1933. * in the log.
  1934. */
  1935. void
  1936. xfs_lock_inodes(
  1937. xfs_inode_t **ips,
  1938. int inodes,
  1939. int first_locked,
  1940. uint lock_mode)
  1941. {
  1942. int attempts = 0, i, j, try_lock;
  1943. xfs_log_item_t *lp;
  1944. ASSERT(ips && (inodes >= 2)); /* we need at least two */
  1945. if (first_locked) {
  1946. try_lock = 1;
  1947. i = 1;
  1948. } else {
  1949. try_lock = 0;
  1950. i = 0;
  1951. }
  1952. again:
  1953. for (; i < inodes; i++) {
  1954. ASSERT(ips[i]);
  1955. if (i && (ips[i] == ips[i-1])) /* Already locked */
  1956. continue;
  1957. /*
  1958. * If try_lock is not set yet, make sure all locked inodes
  1959. * are not in the AIL.
  1960. * If any are, set try_lock to be used later.
  1961. */
  1962. if (!try_lock) {
  1963. for (j = (i - 1); j >= 0 && !try_lock; j--) {
  1964. lp = (xfs_log_item_t *)ips[j]->i_itemp;
  1965. if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
  1966. try_lock++;
  1967. }
  1968. }
  1969. }
  1970. /*
  1971. * If any of the previous locks we have locked is in the AIL,
  1972. * we must TRY to get the second and subsequent locks. If
  1973. * we can't get any, we must release all we have
  1974. * and try again.
  1975. */
  1976. if (try_lock) {
  1977. /* try_lock must be 0 if i is 0. */
  1978. /*
  1979. * try_lock means we have an inode locked
  1980. * that is in the AIL.
  1981. */
  1982. ASSERT(i != 0);
  1983. if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
  1984. attempts++;
  1985. /*
  1986. * Unlock all previous guys and try again.
  1987. * xfs_iunlock will try to push the tail
  1988. * if the inode is in the AIL.
  1989. */
  1990. for(j = i - 1; j >= 0; j--) {
  1991. /*
  1992. * Check to see if we've already
  1993. * unlocked this one.
  1994. * Not the first one going back,
  1995. * and the inode ptr is the same.
  1996. */
  1997. if ((j != (i - 1)) && ips[j] ==
  1998. ips[j+1])
  1999. continue;
  2000. xfs_iunlock(ips[j], lock_mode);
  2001. }
  2002. if ((attempts % 5) == 0) {
  2003. delay(1); /* Don't just spin the CPU */
  2004. #ifdef DEBUG
  2005. xfs_lock_delays++;
  2006. #endif
  2007. }
  2008. i = 0;
  2009. try_lock = 0;
  2010. goto again;
  2011. }
  2012. } else {
  2013. xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
  2014. }
  2015. }
  2016. #ifdef DEBUG
  2017. if (attempts) {
  2018. if (attempts < 5) xfs_small_retries++;
  2019. else if (attempts < 100) xfs_middle_retries++;
  2020. else xfs_lots_retries++;
  2021. } else {
  2022. xfs_locked_n++;
  2023. }
  2024. #endif
  2025. }
  2026. #ifdef DEBUG
  2027. #define REMOVE_DEBUG_TRACE(x) {remove_which_error_return = (x);}
  2028. int remove_which_error_return = 0;
  2029. #else /* ! DEBUG */
  2030. #define REMOVE_DEBUG_TRACE(x)
  2031. #endif /* ! DEBUG */
  2032. int
  2033. xfs_remove(
  2034. xfs_inode_t *dp,
  2035. bhv_vname_t *dentry)
  2036. {
  2037. bhv_vnode_t *dir_vp = XFS_ITOV(dp);
  2038. char *name = VNAME(dentry);
  2039. xfs_mount_t *mp = dp->i_mount;
  2040. xfs_inode_t *ip;
  2041. xfs_trans_t *tp = NULL;
  2042. int error = 0;
  2043. xfs_bmap_free_t free_list;
  2044. xfs_fsblock_t first_block;
  2045. int cancel_flags;
  2046. int committed;
  2047. int dm_di_mode = 0;
  2048. int link_zero;
  2049. uint resblks;
  2050. int namelen;
  2051. xfs_itrace_entry(dp);
  2052. if (XFS_FORCED_SHUTDOWN(mp))
  2053. return XFS_ERROR(EIO);
  2054. namelen = VNAMELEN(dentry);
  2055. if (!xfs_get_dir_entry(dentry, &ip)) {
  2056. dm_di_mode = ip->i_d.di_mode;
  2057. IRELE(ip);
  2058. }
  2059. if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
  2060. error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dir_vp,
  2061. DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
  2062. name, NULL, dm_di_mode, 0, 0);
  2063. if (error)
  2064. return error;
  2065. }
  2066. /* From this point on, return through std_return */
  2067. ip = NULL;
  2068. /*
  2069. * We need to get a reference to ip before we get our log
  2070. * reservation. The reason for this is that we cannot call
  2071. * xfs_iget for an inode for which we do not have a reference
  2072. * once we've acquired a log reservation. This is because the
  2073. * inode we are trying to get might be in xfs_inactive going
  2074. * for a log reservation. Since we'll have to wait for the
  2075. * inactive code to complete before returning from xfs_iget,
  2076. * we need to make sure that we don't have log space reserved
  2077. * when we call xfs_iget. Instead we get an unlocked reference
  2078. * to the inode before getting our log reservation.
  2079. */
  2080. error = xfs_get_dir_entry(dentry, &ip);
  2081. if (error) {
  2082. REMOVE_DEBUG_TRACE(__LINE__);
  2083. goto std_return;
  2084. }
  2085. dm_di_mode = ip->i_d.di_mode;
  2086. xfs_itrace_entry(ip);
  2087. xfs_itrace_ref(ip);
  2088. error = XFS_QM_DQATTACH(mp, dp, 0);
  2089. if (!error && dp != ip)
  2090. error = XFS_QM_DQATTACH(mp, ip, 0);
  2091. if (error) {
  2092. REMOVE_DEBUG_TRACE(__LINE__);
  2093. IRELE(ip);
  2094. goto std_return;
  2095. }
  2096. tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
  2097. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2098. /*
  2099. * We try to get the real space reservation first,
  2100. * allowing for directory btree deletion(s) implying
  2101. * possible bmap insert(s). If we can't get the space
  2102. * reservation then we use 0 instead, and avoid the bmap
  2103. * btree insert(s) in the directory code by, if the bmap
  2104. * insert tries to happen, instead trimming the LAST
  2105. * block from the directory.
  2106. */
  2107. resblks = XFS_REMOVE_SPACE_RES(mp);
  2108. error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
  2109. XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
  2110. if (error == ENOSPC) {
  2111. resblks = 0;
  2112. error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
  2113. XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
  2114. }
  2115. if (error) {
  2116. ASSERT(error != ENOSPC);
  2117. REMOVE_DEBUG_TRACE(__LINE__);
  2118. xfs_trans_cancel(tp, 0);
  2119. IRELE(ip);
  2120. return error;
  2121. }
  2122. error = xfs_lock_dir_and_entry(dp, ip);
  2123. if (error) {
  2124. REMOVE_DEBUG_TRACE(__LINE__);
  2125. xfs_trans_cancel(tp, cancel_flags);
  2126. IRELE(ip);
  2127. goto std_return;
  2128. }
  2129. /*
  2130. * At this point, we've gotten both the directory and the entry
  2131. * inodes locked.
  2132. */
  2133. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  2134. if (dp != ip) {
  2135. /*
  2136. * Increment vnode ref count only in this case since
  2137. * there's an extra vnode reference in the case where
  2138. * dp == ip.
  2139. */
  2140. IHOLD(dp);
  2141. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  2142. }
  2143. /*
  2144. * Entry must exist since we did a lookup in xfs_lock_dir_and_entry.
  2145. */
  2146. XFS_BMAP_INIT(&free_list, &first_block);
  2147. error = xfs_dir_removename(tp, dp, name, namelen, ip->i_ino,
  2148. &first_block, &free_list, 0);
  2149. if (error) {
  2150. ASSERT(error != ENOENT);
  2151. REMOVE_DEBUG_TRACE(__LINE__);
  2152. goto error1;
  2153. }
  2154. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2155. dp->i_gen++;
  2156. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  2157. error = xfs_droplink(tp, ip);
  2158. if (error) {
  2159. REMOVE_DEBUG_TRACE(__LINE__);
  2160. goto error1;
  2161. }
  2162. /* Determine if this is the last link while
  2163. * we are in the transaction.
  2164. */
  2165. link_zero = (ip)->i_d.di_nlink==0;
  2166. /*
  2167. * Take an extra ref on the inode so that it doesn't
  2168. * go to xfs_inactive() from within the commit.
  2169. */
  2170. IHOLD(ip);
  2171. /*
  2172. * If this is a synchronous mount, make sure that the
  2173. * remove transaction goes to disk before returning to
  2174. * the user.
  2175. */
  2176. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  2177. xfs_trans_set_sync(tp);
  2178. }
  2179. error = xfs_bmap_finish(&tp, &free_list, &committed);
  2180. if (error) {
  2181. REMOVE_DEBUG_TRACE(__LINE__);
  2182. goto error_rele;
  2183. }
  2184. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2185. if (error) {
  2186. IRELE(ip);
  2187. goto std_return;
  2188. }
  2189. /*
  2190. * Before we drop our extra reference to the inode, purge it
  2191. * from the refcache if it is there. By waiting until afterwards
  2192. * to do the IRELE, we ensure that we won't go inactive in the
  2193. * xfs_refcache_purge_ip routine (although that would be OK).
  2194. */
  2195. xfs_refcache_purge_ip(ip);
  2196. /*
  2197. * If we are using filestreams, kill the stream association.
  2198. * If the file is still open it may get a new one but that
  2199. * will get killed on last close in xfs_close() so we don't
  2200. * have to worry about that.
  2201. */
  2202. if (link_zero && xfs_inode_is_filestream(ip))
  2203. xfs_filestream_deassociate(ip);
  2204. xfs_itrace_exit(ip);
  2205. IRELE(ip);
  2206. /* Fall through to std_return with error = 0 */
  2207. std_return:
  2208. if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
  2209. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
  2210. dir_vp, DM_RIGHT_NULL,
  2211. NULL, DM_RIGHT_NULL,
  2212. name, NULL, dm_di_mode, error, 0);
  2213. }
  2214. return error;
  2215. error1:
  2216. xfs_bmap_cancel(&free_list);
  2217. cancel_flags |= XFS_TRANS_ABORT;
  2218. xfs_trans_cancel(tp, cancel_flags);
  2219. goto std_return;
  2220. error_rele:
  2221. /*
  2222. * In this case make sure to not release the inode until after
  2223. * the current transaction is aborted. Releasing it beforehand
  2224. * can cause us to go to xfs_inactive and start a recursive
  2225. * transaction which can easily deadlock with the current one.
  2226. */
  2227. xfs_bmap_cancel(&free_list);
  2228. cancel_flags |= XFS_TRANS_ABORT;
  2229. xfs_trans_cancel(tp, cancel_flags);
  2230. /*
  2231. * Before we drop our extra reference to the inode, purge it
  2232. * from the refcache if it is there. By waiting until afterwards
  2233. * to do the IRELE, we ensure that we won't go inactive in the
  2234. * xfs_refcache_purge_ip routine (although that would be OK).
  2235. */
  2236. xfs_refcache_purge_ip(ip);
  2237. IRELE(ip);
  2238. goto std_return;
  2239. }
  2240. int
  2241. xfs_link(
  2242. xfs_inode_t *tdp,
  2243. bhv_vnode_t *src_vp,
  2244. bhv_vname_t *dentry)
  2245. {
  2246. bhv_vnode_t *target_dir_vp = XFS_ITOV(tdp);
  2247. xfs_mount_t *mp = tdp->i_mount;
  2248. xfs_inode_t *sip = xfs_vtoi(src_vp);
  2249. xfs_trans_t *tp;
  2250. xfs_inode_t *ips[2];
  2251. int error;
  2252. xfs_bmap_free_t free_list;
  2253. xfs_fsblock_t first_block;
  2254. int cancel_flags;
  2255. int committed;
  2256. int resblks;
  2257. char *target_name = VNAME(dentry);
  2258. int target_namelen;
  2259. xfs_itrace_entry(tdp);
  2260. xfs_itrace_entry(xfs_vtoi(src_vp));
  2261. target_namelen = VNAMELEN(dentry);
  2262. ASSERT(!VN_ISDIR(src_vp));
  2263. if (XFS_FORCED_SHUTDOWN(mp))
  2264. return XFS_ERROR(EIO);
  2265. if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
  2266. error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
  2267. target_dir_vp, DM_RIGHT_NULL,
  2268. src_vp, DM_RIGHT_NULL,
  2269. target_name, NULL, 0, 0, 0);
  2270. if (error)
  2271. return error;
  2272. }
  2273. /* Return through std_return after this point. */
  2274. error = XFS_QM_DQATTACH(mp, sip, 0);
  2275. if (!error && sip != tdp)
  2276. error = XFS_QM_DQATTACH(mp, tdp, 0);
  2277. if (error)
  2278. goto std_return;
  2279. tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
  2280. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2281. resblks = XFS_LINK_SPACE_RES(mp, target_namelen);
  2282. error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
  2283. XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
  2284. if (error == ENOSPC) {
  2285. resblks = 0;
  2286. error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
  2287. XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
  2288. }
  2289. if (error) {
  2290. cancel_flags = 0;
  2291. goto error_return;
  2292. }
  2293. if (sip->i_ino < tdp->i_ino) {
  2294. ips[0] = sip;
  2295. ips[1] = tdp;
  2296. } else {
  2297. ips[0] = tdp;
  2298. ips[1] = sip;
  2299. }
  2300. xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
  2301. /*
  2302. * Increment vnode ref counts since xfs_trans_commit &
  2303. * xfs_trans_cancel will both unlock the inodes and
  2304. * decrement the associated ref counts.
  2305. */
  2306. VN_HOLD(src_vp);
  2307. VN_HOLD(target_dir_vp);
  2308. xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
  2309. xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
  2310. /*
  2311. * If the source has too many links, we can't make any more to it.
  2312. */
  2313. if (sip->i_d.di_nlink >= XFS_MAXLINK) {
  2314. error = XFS_ERROR(EMLINK);
  2315. goto error_return;
  2316. }
  2317. /*
  2318. * If we are using project inheritance, we only allow hard link
  2319. * creation in our tree when the project IDs are the same; else
  2320. * the tree quota mechanism could be circumvented.
  2321. */
  2322. if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
  2323. (tdp->i_d.di_projid != sip->i_d.di_projid))) {
  2324. error = XFS_ERROR(EXDEV);
  2325. goto error_return;
  2326. }
  2327. if (resblks == 0 &&
  2328. (error = xfs_dir_canenter(tp, tdp, target_name, target_namelen)))
  2329. goto error_return;
  2330. XFS_BMAP_INIT(&free_list, &first_block);
  2331. error = xfs_dir_createname(tp, tdp, target_name, target_namelen,
  2332. sip->i_ino, &first_block, &free_list,
  2333. resblks);
  2334. if (error)
  2335. goto abort_return;
  2336. xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2337. tdp->i_gen++;
  2338. xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
  2339. error = xfs_bumplink(tp, sip);
  2340. if (error)
  2341. goto abort_return;
  2342. /*
  2343. * If this is a synchronous mount, make sure that the
  2344. * link transaction goes to disk before returning to
  2345. * the user.
  2346. */
  2347. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  2348. xfs_trans_set_sync(tp);
  2349. }
  2350. error = xfs_bmap_finish (&tp, &free_list, &committed);
  2351. if (error) {
  2352. xfs_bmap_cancel(&free_list);
  2353. goto abort_return;
  2354. }
  2355. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2356. if (error)
  2357. goto std_return;
  2358. /* Fall through to std_return with error = 0. */
  2359. std_return:
  2360. if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
  2361. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
  2362. target_dir_vp, DM_RIGHT_NULL,
  2363. src_vp, DM_RIGHT_NULL,
  2364. target_name, NULL, 0, error, 0);
  2365. }
  2366. return error;
  2367. abort_return:
  2368. cancel_flags |= XFS_TRANS_ABORT;
  2369. /* FALLTHROUGH */
  2370. error_return:
  2371. xfs_trans_cancel(tp, cancel_flags);
  2372. goto std_return;
  2373. }
  2374. int
  2375. xfs_mkdir(
  2376. xfs_inode_t *dp,
  2377. bhv_vname_t *dentry,
  2378. mode_t mode,
  2379. bhv_vnode_t **vpp,
  2380. cred_t *credp)
  2381. {
  2382. bhv_vnode_t *dir_vp = XFS_ITOV(dp);
  2383. char *dir_name = VNAME(dentry);
  2384. int dir_namelen = VNAMELEN(dentry);
  2385. xfs_mount_t *mp = dp->i_mount;
  2386. xfs_inode_t *cdp; /* inode of created dir */
  2387. bhv_vnode_t *cvp; /* vnode of created dir */
  2388. xfs_trans_t *tp;
  2389. int cancel_flags;
  2390. int error;
  2391. int committed;
  2392. xfs_bmap_free_t free_list;
  2393. xfs_fsblock_t first_block;
  2394. boolean_t unlock_dp_on_error = B_FALSE;
  2395. boolean_t created = B_FALSE;
  2396. int dm_event_sent = 0;
  2397. xfs_prid_t prid;
  2398. struct xfs_dquot *udqp, *gdqp;
  2399. uint resblks;
  2400. if (XFS_FORCED_SHUTDOWN(mp))
  2401. return XFS_ERROR(EIO);
  2402. tp = NULL;
  2403. if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
  2404. error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
  2405. dir_vp, DM_RIGHT_NULL, NULL,
  2406. DM_RIGHT_NULL, dir_name, NULL,
  2407. mode, 0, 0);
  2408. if (error)
  2409. return error;
  2410. dm_event_sent = 1;
  2411. }
  2412. /* Return through std_return after this point. */
  2413. xfs_itrace_entry(dp);
  2414. mp = dp->i_mount;
  2415. udqp = gdqp = NULL;
  2416. if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  2417. prid = dp->i_d.di_projid;
  2418. else
  2419. prid = (xfs_prid_t)dfltprid;
  2420. /*
  2421. * Make sure that we have allocated dquot(s) on disk.
  2422. */
  2423. error = XFS_QM_DQVOPALLOC(mp, dp,
  2424. current_fsuid(credp), current_fsgid(credp), prid,
  2425. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
  2426. if (error)
  2427. goto std_return;
  2428. tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
  2429. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2430. resblks = XFS_MKDIR_SPACE_RES(mp, dir_namelen);
  2431. error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
  2432. XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
  2433. if (error == ENOSPC) {
  2434. resblks = 0;
  2435. error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
  2436. XFS_TRANS_PERM_LOG_RES,
  2437. XFS_MKDIR_LOG_COUNT);
  2438. }
  2439. if (error) {
  2440. cancel_flags = 0;
  2441. goto error_return;
  2442. }
  2443. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  2444. unlock_dp_on_error = B_TRUE;
  2445. /*
  2446. * Check for directory link count overflow.
  2447. */
  2448. if (dp->i_d.di_nlink >= XFS_MAXLINK) {
  2449. error = XFS_ERROR(EMLINK);
  2450. goto error_return;
  2451. }
  2452. /*
  2453. * Reserve disk quota and the inode.
  2454. */
  2455. error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
  2456. if (error)
  2457. goto error_return;
  2458. if (resblks == 0 &&
  2459. (error = xfs_dir_canenter(tp, dp, dir_name, dir_namelen)))
  2460. goto error_return;
  2461. /*
  2462. * create the directory inode.
  2463. */
  2464. error = xfs_dir_ialloc(&tp, dp, mode, 2,
  2465. 0, credp, prid, resblks > 0,
  2466. &cdp, NULL);
  2467. if (error) {
  2468. if (error == ENOSPC)
  2469. goto error_return;
  2470. goto abort_return;
  2471. }
  2472. xfs_itrace_ref(cdp);
  2473. /*
  2474. * Now we add the directory inode to the transaction.
  2475. * We waited until now since xfs_dir_ialloc might start
  2476. * a new transaction. Had we joined the transaction
  2477. * earlier, the locks might have gotten released. An error
  2478. * from here on will result in the transaction cancel
  2479. * unlocking dp so don't do it explicitly in the error path.
  2480. */
  2481. VN_HOLD(dir_vp);
  2482. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  2483. unlock_dp_on_error = B_FALSE;
  2484. XFS_BMAP_INIT(&free_list, &first_block);
  2485. error = xfs_dir_createname(tp, dp, dir_name, dir_namelen, cdp->i_ino,
  2486. &first_block, &free_list, resblks ?
  2487. resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
  2488. if (error) {
  2489. ASSERT(error != ENOSPC);
  2490. goto error1;
  2491. }
  2492. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2493. /*
  2494. * Bump the in memory version number of the parent directory
  2495. * so that other processes accessing it will recognize that
  2496. * the directory has changed.
  2497. */
  2498. dp->i_gen++;
  2499. error = xfs_dir_init(tp, cdp, dp);
  2500. if (error)
  2501. goto error2;
  2502. cdp->i_gen = 1;
  2503. error = xfs_bumplink(tp, dp);
  2504. if (error)
  2505. goto error2;
  2506. cvp = XFS_ITOV(cdp);
  2507. created = B_TRUE;
  2508. *vpp = cvp;
  2509. IHOLD(cdp);
  2510. /*
  2511. * Attach the dquots to the new inode and modify the icount incore.
  2512. */
  2513. XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
  2514. /*
  2515. * If this is a synchronous mount, make sure that the
  2516. * mkdir transaction goes to disk before returning to
  2517. * the user.
  2518. */
  2519. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  2520. xfs_trans_set_sync(tp);
  2521. }
  2522. error = xfs_bmap_finish(&tp, &free_list, &committed);
  2523. if (error) {
  2524. IRELE(cdp);
  2525. goto error2;
  2526. }
  2527. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2528. XFS_QM_DQRELE(mp, udqp);
  2529. XFS_QM_DQRELE(mp, gdqp);
  2530. if (error) {
  2531. IRELE(cdp);
  2532. }
  2533. /* Fall through to std_return with error = 0 or errno from
  2534. * xfs_trans_commit. */
  2535. std_return:
  2536. if ((created || (error != 0 && dm_event_sent != 0)) &&
  2537. DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
  2538. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
  2539. dir_vp, DM_RIGHT_NULL,
  2540. created ? XFS_ITOV(cdp):NULL,
  2541. DM_RIGHT_NULL,
  2542. dir_name, NULL,
  2543. mode, error, 0);
  2544. }
  2545. return error;
  2546. error2:
  2547. error1:
  2548. xfs_bmap_cancel(&free_list);
  2549. abort_return:
  2550. cancel_flags |= XFS_TRANS_ABORT;
  2551. error_return:
  2552. xfs_trans_cancel(tp, cancel_flags);
  2553. XFS_QM_DQRELE(mp, udqp);
  2554. XFS_QM_DQRELE(mp, gdqp);
  2555. if (unlock_dp_on_error)
  2556. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  2557. goto std_return;
  2558. }
  2559. int
  2560. xfs_rmdir(
  2561. xfs_inode_t *dp,
  2562. bhv_vname_t *dentry)
  2563. {
  2564. bhv_vnode_t *dir_vp = XFS_ITOV(dp);
  2565. char *name = VNAME(dentry);
  2566. int namelen = VNAMELEN(dentry);
  2567. xfs_mount_t *mp = dp->i_mount;
  2568. xfs_inode_t *cdp; /* child directory */
  2569. xfs_trans_t *tp;
  2570. int error;
  2571. xfs_bmap_free_t free_list;
  2572. xfs_fsblock_t first_block;
  2573. int cancel_flags;
  2574. int committed;
  2575. int dm_di_mode = S_IFDIR;
  2576. int last_cdp_link;
  2577. uint resblks;
  2578. xfs_itrace_entry(dp);
  2579. if (XFS_FORCED_SHUTDOWN(mp))
  2580. return XFS_ERROR(EIO);
  2581. if (!xfs_get_dir_entry(dentry, &cdp)) {
  2582. dm_di_mode = cdp->i_d.di_mode;
  2583. IRELE(cdp);
  2584. }
  2585. if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
  2586. error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE,
  2587. dir_vp, DM_RIGHT_NULL,
  2588. NULL, DM_RIGHT_NULL,
  2589. name, NULL, dm_di_mode, 0, 0);
  2590. if (error)
  2591. return XFS_ERROR(error);
  2592. }
  2593. /* Return through std_return after this point. */
  2594. cdp = NULL;
  2595. /*
  2596. * We need to get a reference to cdp before we get our log
  2597. * reservation. The reason for this is that we cannot call
  2598. * xfs_iget for an inode for which we do not have a reference
  2599. * once we've acquired a log reservation. This is because the
  2600. * inode we are trying to get might be in xfs_inactive going
  2601. * for a log reservation. Since we'll have to wait for the
  2602. * inactive code to complete before returning from xfs_iget,
  2603. * we need to make sure that we don't have log space reserved
  2604. * when we call xfs_iget. Instead we get an unlocked reference
  2605. * to the inode before getting our log reservation.
  2606. */
  2607. error = xfs_get_dir_entry(dentry, &cdp);
  2608. if (error) {
  2609. REMOVE_DEBUG_TRACE(__LINE__);
  2610. goto std_return;
  2611. }
  2612. mp = dp->i_mount;
  2613. dm_di_mode = cdp->i_d.di_mode;
  2614. /*
  2615. * Get the dquots for the inodes.
  2616. */
  2617. error = XFS_QM_DQATTACH(mp, dp, 0);
  2618. if (!error && dp != cdp)
  2619. error = XFS_QM_DQATTACH(mp, cdp, 0);
  2620. if (error) {
  2621. IRELE(cdp);
  2622. REMOVE_DEBUG_TRACE(__LINE__);
  2623. goto std_return;
  2624. }
  2625. tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
  2626. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2627. /*
  2628. * We try to get the real space reservation first,
  2629. * allowing for directory btree deletion(s) implying
  2630. * possible bmap insert(s). If we can't get the space
  2631. * reservation then we use 0 instead, and avoid the bmap
  2632. * btree insert(s) in the directory code by, if the bmap
  2633. * insert tries to happen, instead trimming the LAST
  2634. * block from the directory.
  2635. */
  2636. resblks = XFS_REMOVE_SPACE_RES(mp);
  2637. error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
  2638. XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
  2639. if (error == ENOSPC) {
  2640. resblks = 0;
  2641. error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
  2642. XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
  2643. }
  2644. if (error) {
  2645. ASSERT(error != ENOSPC);
  2646. cancel_flags = 0;
  2647. IRELE(cdp);
  2648. goto error_return;
  2649. }
  2650. XFS_BMAP_INIT(&free_list, &first_block);
  2651. /*
  2652. * Now lock the child directory inode and the parent directory
  2653. * inode in the proper order. This will take care of validating
  2654. * that the directory entry for the child directory inode has
  2655. * not changed while we were obtaining a log reservation.
  2656. */
  2657. error = xfs_lock_dir_and_entry(dp, cdp);
  2658. if (error) {
  2659. xfs_trans_cancel(tp, cancel_flags);
  2660. IRELE(cdp);
  2661. goto std_return;
  2662. }
  2663. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  2664. if (dp != cdp) {
  2665. /*
  2666. * Only increment the parent directory vnode count if
  2667. * we didn't bump it in looking up cdp. The only time
  2668. * we don't bump it is when we're looking up ".".
  2669. */
  2670. VN_HOLD(dir_vp);
  2671. }
  2672. xfs_itrace_ref(cdp);
  2673. xfs_trans_ijoin(tp, cdp, XFS_ILOCK_EXCL);
  2674. ASSERT(cdp->i_d.di_nlink >= 2);
  2675. if (cdp->i_d.di_nlink != 2) {
  2676. error = XFS_ERROR(ENOTEMPTY);
  2677. goto error_return;
  2678. }
  2679. if (!xfs_dir_isempty(cdp)) {
  2680. error = XFS_ERROR(ENOTEMPTY);
  2681. goto error_return;
  2682. }
  2683. error = xfs_dir_removename(tp, dp, name, namelen, cdp->i_ino,
  2684. &first_block, &free_list, resblks);
  2685. if (error)
  2686. goto error1;
  2687. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2688. /*
  2689. * Bump the in memory generation count on the parent
  2690. * directory so that other can know that it has changed.
  2691. */
  2692. dp->i_gen++;
  2693. /*
  2694. * Drop the link from cdp's "..".
  2695. */
  2696. error = xfs_droplink(tp, dp);
  2697. if (error) {
  2698. goto error1;
  2699. }
  2700. /*
  2701. * Drop the link from dp to cdp.
  2702. */
  2703. error = xfs_droplink(tp, cdp);
  2704. if (error) {
  2705. goto error1;
  2706. }
  2707. /*
  2708. * Drop the "." link from cdp to self.
  2709. */
  2710. error = xfs_droplink(tp, cdp);
  2711. if (error) {
  2712. goto error1;
  2713. }
  2714. /* Determine these before committing transaction */
  2715. last_cdp_link = (cdp)->i_d.di_nlink==0;
  2716. /*
  2717. * Take an extra ref on the child vnode so that it
  2718. * does not go to xfs_inactive() from within the commit.
  2719. */
  2720. IHOLD(cdp);
  2721. /*
  2722. * If this is a synchronous mount, make sure that the
  2723. * rmdir transaction goes to disk before returning to
  2724. * the user.
  2725. */
  2726. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  2727. xfs_trans_set_sync(tp);
  2728. }
  2729. error = xfs_bmap_finish (&tp, &free_list, &committed);
  2730. if (error) {
  2731. xfs_bmap_cancel(&free_list);
  2732. xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
  2733. XFS_TRANS_ABORT));
  2734. IRELE(cdp);
  2735. goto std_return;
  2736. }
  2737. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2738. if (error) {
  2739. IRELE(cdp);
  2740. goto std_return;
  2741. }
  2742. IRELE(cdp);
  2743. /* Fall through to std_return with error = 0 or the errno
  2744. * from xfs_trans_commit. */
  2745. std_return:
  2746. if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
  2747. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
  2748. dir_vp, DM_RIGHT_NULL,
  2749. NULL, DM_RIGHT_NULL,
  2750. name, NULL, dm_di_mode,
  2751. error, 0);
  2752. }
  2753. return error;
  2754. error1:
  2755. xfs_bmap_cancel(&free_list);
  2756. cancel_flags |= XFS_TRANS_ABORT;
  2757. /* FALLTHROUGH */
  2758. error_return:
  2759. xfs_trans_cancel(tp, cancel_flags);
  2760. goto std_return;
  2761. }
  2762. int
  2763. xfs_symlink(
  2764. xfs_inode_t *dp,
  2765. bhv_vname_t *dentry,
  2766. char *target_path,
  2767. mode_t mode,
  2768. bhv_vnode_t **vpp,
  2769. cred_t *credp)
  2770. {
  2771. bhv_vnode_t *dir_vp = XFS_ITOV(dp);
  2772. xfs_mount_t *mp = dp->i_mount;
  2773. xfs_trans_t *tp;
  2774. xfs_inode_t *ip;
  2775. int error;
  2776. int pathlen;
  2777. xfs_bmap_free_t free_list;
  2778. xfs_fsblock_t first_block;
  2779. boolean_t unlock_dp_on_error = B_FALSE;
  2780. uint cancel_flags;
  2781. int committed;
  2782. xfs_fileoff_t first_fsb;
  2783. xfs_filblks_t fs_blocks;
  2784. int nmaps;
  2785. xfs_bmbt_irec_t mval[SYMLINK_MAPS];
  2786. xfs_daddr_t d;
  2787. char *cur_chunk;
  2788. int byte_cnt;
  2789. int n;
  2790. xfs_buf_t *bp;
  2791. xfs_prid_t prid;
  2792. struct xfs_dquot *udqp, *gdqp;
  2793. uint resblks;
  2794. char *link_name = VNAME(dentry);
  2795. int link_namelen;
  2796. *vpp = NULL;
  2797. error = 0;
  2798. ip = NULL;
  2799. tp = NULL;
  2800. xfs_itrace_entry(dp);
  2801. if (XFS_FORCED_SHUTDOWN(mp))
  2802. return XFS_ERROR(EIO);
  2803. link_namelen = VNAMELEN(dentry);
  2804. /*
  2805. * Check component lengths of the target path name.
  2806. */
  2807. pathlen = strlen(target_path);
  2808. if (pathlen >= MAXPATHLEN) /* total string too long */
  2809. return XFS_ERROR(ENAMETOOLONG);
  2810. if (pathlen >= MAXNAMELEN) { /* is any component too long? */
  2811. int len, total;
  2812. char *path;
  2813. for (total = 0, path = target_path; total < pathlen;) {
  2814. /*
  2815. * Skip any slashes.
  2816. */
  2817. while(*path == '/') {
  2818. total++;
  2819. path++;
  2820. }
  2821. /*
  2822. * Count up to the next slash or end of path.
  2823. * Error out if the component is bigger than MAXNAMELEN.
  2824. */
  2825. for(len = 0; *path != '/' && total < pathlen;total++, path++) {
  2826. if (++len >= MAXNAMELEN) {
  2827. error = ENAMETOOLONG;
  2828. return error;
  2829. }
  2830. }
  2831. }
  2832. }
  2833. if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
  2834. error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dir_vp,
  2835. DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
  2836. link_name, target_path, 0, 0, 0);
  2837. if (error)
  2838. return error;
  2839. }
  2840. /* Return through std_return after this point. */
  2841. udqp = gdqp = NULL;
  2842. if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  2843. prid = dp->i_d.di_projid;
  2844. else
  2845. prid = (xfs_prid_t)dfltprid;
  2846. /*
  2847. * Make sure that we have allocated dquot(s) on disk.
  2848. */
  2849. error = XFS_QM_DQVOPALLOC(mp, dp,
  2850. current_fsuid(credp), current_fsgid(credp), prid,
  2851. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
  2852. if (error)
  2853. goto std_return;
  2854. tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
  2855. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2856. /*
  2857. * The symlink will fit into the inode data fork?
  2858. * There can't be any attributes so we get the whole variable part.
  2859. */
  2860. if (pathlen <= XFS_LITINO(mp))
  2861. fs_blocks = 0;
  2862. else
  2863. fs_blocks = XFS_B_TO_FSB(mp, pathlen);
  2864. resblks = XFS_SYMLINK_SPACE_RES(mp, link_namelen, fs_blocks);
  2865. error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
  2866. XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
  2867. if (error == ENOSPC && fs_blocks == 0) {
  2868. resblks = 0;
  2869. error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
  2870. XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
  2871. }
  2872. if (error) {
  2873. cancel_flags = 0;
  2874. goto error_return;
  2875. }
  2876. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  2877. unlock_dp_on_error = B_TRUE;
  2878. /*
  2879. * Check whether the directory allows new symlinks or not.
  2880. */
  2881. if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
  2882. error = XFS_ERROR(EPERM);
  2883. goto error_return;
  2884. }
  2885. /*
  2886. * Reserve disk quota : blocks and inode.
  2887. */
  2888. error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
  2889. if (error)
  2890. goto error_return;
  2891. /*
  2892. * Check for ability to enter directory entry, if no space reserved.
  2893. */
  2894. if (resblks == 0 &&
  2895. (error = xfs_dir_canenter(tp, dp, link_name, link_namelen)))
  2896. goto error_return;
  2897. /*
  2898. * Initialize the bmap freelist prior to calling either
  2899. * bmapi or the directory create code.
  2900. */
  2901. XFS_BMAP_INIT(&free_list, &first_block);
  2902. /*
  2903. * Allocate an inode for the symlink.
  2904. */
  2905. error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
  2906. 1, 0, credp, prid, resblks > 0, &ip, NULL);
  2907. if (error) {
  2908. if (error == ENOSPC)
  2909. goto error_return;
  2910. goto error1;
  2911. }
  2912. xfs_itrace_ref(ip);
  2913. /*
  2914. * An error after we've joined dp to the transaction will result in the
  2915. * transaction cancel unlocking dp so don't do it explicitly in the
  2916. * error path.
  2917. */
  2918. VN_HOLD(dir_vp);
  2919. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  2920. unlock_dp_on_error = B_FALSE;
  2921. /*
  2922. * Also attach the dquot(s) to it, if applicable.
  2923. */
  2924. XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
  2925. if (resblks)
  2926. resblks -= XFS_IALLOC_SPACE_RES(mp);
  2927. /*
  2928. * If the symlink will fit into the inode, write it inline.
  2929. */
  2930. if (pathlen <= XFS_IFORK_DSIZE(ip)) {
  2931. xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
  2932. memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
  2933. ip->i_d.di_size = pathlen;
  2934. /*
  2935. * The inode was initially created in extent format.
  2936. */
  2937. ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
  2938. ip->i_df.if_flags |= XFS_IFINLINE;
  2939. ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
  2940. xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
  2941. } else {
  2942. first_fsb = 0;
  2943. nmaps = SYMLINK_MAPS;
  2944. error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
  2945. XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
  2946. &first_block, resblks, mval, &nmaps,
  2947. &free_list, NULL);
  2948. if (error) {
  2949. goto error1;
  2950. }
  2951. if (resblks)
  2952. resblks -= fs_blocks;
  2953. ip->i_d.di_size = pathlen;
  2954. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  2955. cur_chunk = target_path;
  2956. for (n = 0; n < nmaps; n++) {
  2957. d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
  2958. byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
  2959. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
  2960. BTOBB(byte_cnt), 0);
  2961. ASSERT(bp && !XFS_BUF_GETERROR(bp));
  2962. if (pathlen < byte_cnt) {
  2963. byte_cnt = pathlen;
  2964. }
  2965. pathlen -= byte_cnt;
  2966. memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
  2967. cur_chunk += byte_cnt;
  2968. xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
  2969. }
  2970. }
  2971. /*
  2972. * Create the directory entry for the symlink.
  2973. */
  2974. error = xfs_dir_createname(tp, dp, link_name, link_namelen, ip->i_ino,
  2975. &first_block, &free_list, resblks);
  2976. if (error)
  2977. goto error1;
  2978. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2979. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  2980. /*
  2981. * Bump the in memory version number of the parent directory
  2982. * so that other processes accessing it will recognize that
  2983. * the directory has changed.
  2984. */
  2985. dp->i_gen++;
  2986. /*
  2987. * If this is a synchronous mount, make sure that the
  2988. * symlink transaction goes to disk before returning to
  2989. * the user.
  2990. */
  2991. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  2992. xfs_trans_set_sync(tp);
  2993. }
  2994. /*
  2995. * xfs_trans_commit normally decrements the vnode ref count
  2996. * when it unlocks the inode. Since we want to return the
  2997. * vnode to the caller, we bump the vnode ref count now.
  2998. */
  2999. IHOLD(ip);
  3000. error = xfs_bmap_finish(&tp, &free_list, &committed);
  3001. if (error) {
  3002. goto error2;
  3003. }
  3004. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  3005. XFS_QM_DQRELE(mp, udqp);
  3006. XFS_QM_DQRELE(mp, gdqp);
  3007. /* Fall through to std_return with error = 0 or errno from
  3008. * xfs_trans_commit */
  3009. std_return:
  3010. if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
  3011. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
  3012. dir_vp, DM_RIGHT_NULL,
  3013. error ? NULL : XFS_ITOV(ip),
  3014. DM_RIGHT_NULL, link_name, target_path,
  3015. 0, error, 0);
  3016. }
  3017. if (!error) {
  3018. bhv_vnode_t *vp;
  3019. ASSERT(ip);
  3020. vp = XFS_ITOV(ip);
  3021. *vpp = vp;
  3022. }
  3023. return error;
  3024. error2:
  3025. IRELE(ip);
  3026. error1:
  3027. xfs_bmap_cancel(&free_list);
  3028. cancel_flags |= XFS_TRANS_ABORT;
  3029. error_return:
  3030. xfs_trans_cancel(tp, cancel_flags);
  3031. XFS_QM_DQRELE(mp, udqp);
  3032. XFS_QM_DQRELE(mp, gdqp);
  3033. if (unlock_dp_on_error)
  3034. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  3035. goto std_return;
  3036. }
  3037. int
  3038. xfs_rwlock(
  3039. xfs_inode_t *ip,
  3040. bhv_vrwlock_t locktype)
  3041. {
  3042. if (S_ISDIR(ip->i_d.di_mode))
  3043. return 1;
  3044. if (locktype == VRWLOCK_WRITE) {
  3045. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  3046. } else if (locktype == VRWLOCK_TRY_READ) {
  3047. return xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED);
  3048. } else if (locktype == VRWLOCK_TRY_WRITE) {
  3049. return xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL);
  3050. } else {
  3051. ASSERT((locktype == VRWLOCK_READ) ||
  3052. (locktype == VRWLOCK_WRITE_DIRECT));
  3053. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  3054. }
  3055. return 1;
  3056. }
  3057. void
  3058. xfs_rwunlock(
  3059. xfs_inode_t *ip,
  3060. bhv_vrwlock_t locktype)
  3061. {
  3062. if (S_ISDIR(ip->i_d.di_mode))
  3063. return;
  3064. if (locktype == VRWLOCK_WRITE) {
  3065. /*
  3066. * In the write case, we may have added a new entry to
  3067. * the reference cache. This might store a pointer to
  3068. * an inode to be released in this inode. If it is there,
  3069. * clear the pointer and release the inode after unlocking
  3070. * this one.
  3071. */
  3072. xfs_refcache_iunlock(ip, XFS_IOLOCK_EXCL);
  3073. } else {
  3074. ASSERT((locktype == VRWLOCK_READ) ||
  3075. (locktype == VRWLOCK_WRITE_DIRECT));
  3076. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  3077. }
  3078. return;
  3079. }
  3080. int
  3081. xfs_inode_flush(
  3082. xfs_inode_t *ip,
  3083. int flags)
  3084. {
  3085. xfs_mount_t *mp = ip->i_mount;
  3086. xfs_inode_log_item_t *iip = ip->i_itemp;
  3087. int error = 0;
  3088. if (XFS_FORCED_SHUTDOWN(mp))
  3089. return XFS_ERROR(EIO);
  3090. /*
  3091. * Bypass inodes which have already been cleaned by
  3092. * the inode flush clustering code inside xfs_iflush
  3093. */
  3094. if ((ip->i_update_core == 0) &&
  3095. ((iip == NULL) || !(iip->ili_format.ilf_fields & XFS_ILOG_ALL)))
  3096. return 0;
  3097. if (flags & FLUSH_LOG) {
  3098. if (iip && iip->ili_last_lsn) {
  3099. xlog_t *log = mp->m_log;
  3100. xfs_lsn_t sync_lsn;
  3101. int log_flags = XFS_LOG_FORCE;
  3102. spin_lock(&log->l_grant_lock);
  3103. sync_lsn = log->l_last_sync_lsn;
  3104. spin_unlock(&log->l_grant_lock);
  3105. if ((XFS_LSN_CMP(iip->ili_last_lsn, sync_lsn) > 0)) {
  3106. if (flags & FLUSH_SYNC)
  3107. log_flags |= XFS_LOG_SYNC;
  3108. error = xfs_log_force(mp, iip->ili_last_lsn, log_flags);
  3109. if (error)
  3110. return error;
  3111. }
  3112. if (ip->i_update_core == 0)
  3113. return 0;
  3114. }
  3115. }
  3116. /*
  3117. * We make this non-blocking if the inode is contended,
  3118. * return EAGAIN to indicate to the caller that they
  3119. * did not succeed. This prevents the flush path from
  3120. * blocking on inodes inside another operation right
  3121. * now, they get caught later by xfs_sync.
  3122. */
  3123. if (flags & FLUSH_INODE) {
  3124. int flush_flags;
  3125. if (flags & FLUSH_SYNC) {
  3126. xfs_ilock(ip, XFS_ILOCK_SHARED);
  3127. xfs_iflock(ip);
  3128. } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
  3129. if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
  3130. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  3131. return EAGAIN;
  3132. }
  3133. } else {
  3134. return EAGAIN;
  3135. }
  3136. if (flags & FLUSH_SYNC)
  3137. flush_flags = XFS_IFLUSH_SYNC;
  3138. else
  3139. flush_flags = XFS_IFLUSH_ASYNC;
  3140. error = xfs_iflush(ip, flush_flags);
  3141. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  3142. }
  3143. return error;
  3144. }
  3145. int
  3146. xfs_set_dmattrs(
  3147. xfs_inode_t *ip,
  3148. u_int evmask,
  3149. u_int16_t state)
  3150. {
  3151. xfs_mount_t *mp = ip->i_mount;
  3152. xfs_trans_t *tp;
  3153. int error;
  3154. if (!capable(CAP_SYS_ADMIN))
  3155. return XFS_ERROR(EPERM);
  3156. if (XFS_FORCED_SHUTDOWN(mp))
  3157. return XFS_ERROR(EIO);
  3158. tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
  3159. error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
  3160. if (error) {
  3161. xfs_trans_cancel(tp, 0);
  3162. return error;
  3163. }
  3164. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3165. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  3166. ip->i_d.di_dmevmask = evmask;
  3167. ip->i_d.di_dmstate = state;
  3168. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  3169. IHOLD(ip);
  3170. error = xfs_trans_commit(tp, 0);
  3171. return error;
  3172. }
  3173. int
  3174. xfs_reclaim(
  3175. xfs_inode_t *ip)
  3176. {
  3177. bhv_vnode_t *vp = XFS_ITOV(ip);
  3178. xfs_itrace_entry(ip);
  3179. ASSERT(!VN_MAPPED(vp));
  3180. /* bad inode, get out here ASAP */
  3181. if (VN_BAD(vp)) {
  3182. xfs_ireclaim(ip);
  3183. return 0;
  3184. }
  3185. vn_iowait(ip);
  3186. ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
  3187. /*
  3188. * Make sure the atime in the XFS inode is correct before freeing the
  3189. * Linux inode.
  3190. */
  3191. xfs_synchronize_atime(ip);
  3192. /*
  3193. * If we have nothing to flush with this inode then complete the
  3194. * teardown now, otherwise break the link between the xfs inode and the
  3195. * linux inode and clean up the xfs inode later. This avoids flushing
  3196. * the inode to disk during the delete operation itself.
  3197. *
  3198. * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
  3199. * first to ensure that xfs_iunpin() will never see an xfs inode
  3200. * that has a linux inode being reclaimed. Synchronisation is provided
  3201. * by the i_flags_lock.
  3202. */
  3203. if (!ip->i_update_core && (ip->i_itemp == NULL)) {
  3204. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3205. xfs_iflock(ip);
  3206. return xfs_finish_reclaim(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
  3207. } else {
  3208. xfs_mount_t *mp = ip->i_mount;
  3209. /* Protect sync and unpin from us */
  3210. XFS_MOUNT_ILOCK(mp);
  3211. spin_lock(&ip->i_flags_lock);
  3212. __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
  3213. vn_to_inode(vp)->i_private = NULL;
  3214. ip->i_vnode = NULL;
  3215. spin_unlock(&ip->i_flags_lock);
  3216. list_add_tail(&ip->i_reclaim, &mp->m_del_inodes);
  3217. XFS_MOUNT_IUNLOCK(mp);
  3218. }
  3219. return 0;
  3220. }
  3221. int
  3222. xfs_finish_reclaim(
  3223. xfs_inode_t *ip,
  3224. int locked,
  3225. int sync_mode)
  3226. {
  3227. xfs_perag_t *pag = xfs_get_perag(ip->i_mount, ip->i_ino);
  3228. bhv_vnode_t *vp = XFS_ITOV_NULL(ip);
  3229. int error;
  3230. if (vp && VN_BAD(vp))
  3231. goto reclaim;
  3232. /* The hash lock here protects a thread in xfs_iget_core from
  3233. * racing with us on linking the inode back with a vnode.
  3234. * Once we have the XFS_IRECLAIM flag set it will not touch
  3235. * us.
  3236. */
  3237. write_lock(&pag->pag_ici_lock);
  3238. spin_lock(&ip->i_flags_lock);
  3239. if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
  3240. (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) && vp == NULL)) {
  3241. spin_unlock(&ip->i_flags_lock);
  3242. write_unlock(&pag->pag_ici_lock);
  3243. if (locked) {
  3244. xfs_ifunlock(ip);
  3245. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3246. }
  3247. return 1;
  3248. }
  3249. __xfs_iflags_set(ip, XFS_IRECLAIM);
  3250. spin_unlock(&ip->i_flags_lock);
  3251. write_unlock(&pag->pag_ici_lock);
  3252. xfs_put_perag(ip->i_mount, pag);
  3253. /*
  3254. * If the inode is still dirty, then flush it out. If the inode
  3255. * is not in the AIL, then it will be OK to flush it delwri as
  3256. * long as xfs_iflush() does not keep any references to the inode.
  3257. * We leave that decision up to xfs_iflush() since it has the
  3258. * knowledge of whether it's OK to simply do a delwri flush of
  3259. * the inode or whether we need to wait until the inode is
  3260. * pulled from the AIL.
  3261. * We get the flush lock regardless, though, just to make sure
  3262. * we don't free it while it is being flushed.
  3263. */
  3264. if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  3265. if (!locked) {
  3266. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3267. xfs_iflock(ip);
  3268. }
  3269. if (ip->i_update_core ||
  3270. ((ip->i_itemp != NULL) &&
  3271. (ip->i_itemp->ili_format.ilf_fields != 0))) {
  3272. error = xfs_iflush(ip, sync_mode);
  3273. /*
  3274. * If we hit an error, typically because of filesystem
  3275. * shutdown, we don't need to let vn_reclaim to know
  3276. * because we're gonna reclaim the inode anyway.
  3277. */
  3278. if (error) {
  3279. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3280. goto reclaim;
  3281. }
  3282. xfs_iflock(ip); /* synchronize with xfs_iflush_done */
  3283. }
  3284. ASSERT(ip->i_update_core == 0);
  3285. ASSERT(ip->i_itemp == NULL ||
  3286. ip->i_itemp->ili_format.ilf_fields == 0);
  3287. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3288. } else if (locked) {
  3289. /*
  3290. * We are not interested in doing an iflush if we're
  3291. * in the process of shutting down the filesystem forcibly.
  3292. * So, just reclaim the inode.
  3293. */
  3294. xfs_ifunlock(ip);
  3295. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3296. }
  3297. reclaim:
  3298. xfs_ireclaim(ip);
  3299. return 0;
  3300. }
  3301. int
  3302. xfs_finish_reclaim_all(xfs_mount_t *mp, int noblock)
  3303. {
  3304. int purged;
  3305. xfs_inode_t *ip, *n;
  3306. int done = 0;
  3307. while (!done) {
  3308. purged = 0;
  3309. XFS_MOUNT_ILOCK(mp);
  3310. list_for_each_entry_safe(ip, n, &mp->m_del_inodes, i_reclaim) {
  3311. if (noblock) {
  3312. if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0)
  3313. continue;
  3314. if (xfs_ipincount(ip) ||
  3315. !xfs_iflock_nowait(ip)) {
  3316. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3317. continue;
  3318. }
  3319. }
  3320. XFS_MOUNT_IUNLOCK(mp);
  3321. if (xfs_finish_reclaim(ip, noblock,
  3322. XFS_IFLUSH_DELWRI_ELSE_ASYNC))
  3323. delay(1);
  3324. purged = 1;
  3325. break;
  3326. }
  3327. done = !purged;
  3328. }
  3329. XFS_MOUNT_IUNLOCK(mp);
  3330. return 0;
  3331. }
  3332. /*
  3333. * xfs_alloc_file_space()
  3334. * This routine allocates disk space for the given file.
  3335. *
  3336. * If alloc_type == 0, this request is for an ALLOCSP type
  3337. * request which will change the file size. In this case, no
  3338. * DMAPI event will be generated by the call. A TRUNCATE event
  3339. * will be generated later by xfs_setattr.
  3340. *
  3341. * If alloc_type != 0, this request is for a RESVSP type
  3342. * request, and a DMAPI DM_EVENT_WRITE will be generated if the
  3343. * lower block boundary byte address is less than the file's
  3344. * length.
  3345. *
  3346. * RETURNS:
  3347. * 0 on success
  3348. * errno on error
  3349. *
  3350. */
  3351. STATIC int
  3352. xfs_alloc_file_space(
  3353. xfs_inode_t *ip,
  3354. xfs_off_t offset,
  3355. xfs_off_t len,
  3356. int alloc_type,
  3357. int attr_flags)
  3358. {
  3359. xfs_mount_t *mp = ip->i_mount;
  3360. xfs_off_t count;
  3361. xfs_filblks_t allocated_fsb;
  3362. xfs_filblks_t allocatesize_fsb;
  3363. xfs_extlen_t extsz, temp;
  3364. xfs_fileoff_t startoffset_fsb;
  3365. xfs_fsblock_t firstfsb;
  3366. int nimaps;
  3367. int bmapi_flag;
  3368. int quota_flag;
  3369. int rt;
  3370. xfs_trans_t *tp;
  3371. xfs_bmbt_irec_t imaps[1], *imapp;
  3372. xfs_bmap_free_t free_list;
  3373. uint qblocks, resblks, resrtextents;
  3374. int committed;
  3375. int error;
  3376. xfs_itrace_entry(ip);
  3377. if (XFS_FORCED_SHUTDOWN(mp))
  3378. return XFS_ERROR(EIO);
  3379. if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
  3380. return error;
  3381. if (len <= 0)
  3382. return XFS_ERROR(EINVAL);
  3383. rt = XFS_IS_REALTIME_INODE(ip);
  3384. extsz = xfs_get_extsz_hint(ip);
  3385. count = len;
  3386. imapp = &imaps[0];
  3387. nimaps = 1;
  3388. bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
  3389. startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
  3390. allocatesize_fsb = XFS_B_TO_FSB(mp, count);
  3391. /* Generate a DMAPI event if needed. */
  3392. if (alloc_type != 0 && offset < ip->i_size &&
  3393. (attr_flags&ATTR_DMI) == 0 &&
  3394. DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
  3395. xfs_off_t end_dmi_offset;
  3396. end_dmi_offset = offset+len;
  3397. if (end_dmi_offset > ip->i_size)
  3398. end_dmi_offset = ip->i_size;
  3399. error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, XFS_ITOV(ip),
  3400. offset, end_dmi_offset - offset,
  3401. 0, NULL);
  3402. if (error)
  3403. return error;
  3404. }
  3405. /*
  3406. * Allocate file space until done or until there is an error
  3407. */
  3408. retry:
  3409. while (allocatesize_fsb && !error) {
  3410. xfs_fileoff_t s, e;
  3411. /*
  3412. * Determine space reservations for data/realtime.
  3413. */
  3414. if (unlikely(extsz)) {
  3415. s = startoffset_fsb;
  3416. do_div(s, extsz);
  3417. s *= extsz;
  3418. e = startoffset_fsb + allocatesize_fsb;
  3419. if ((temp = do_mod(startoffset_fsb, extsz)))
  3420. e += temp;
  3421. if ((temp = do_mod(e, extsz)))
  3422. e += extsz - temp;
  3423. } else {
  3424. s = 0;
  3425. e = allocatesize_fsb;
  3426. }
  3427. if (unlikely(rt)) {
  3428. resrtextents = qblocks = (uint)(e - s);
  3429. resrtextents /= mp->m_sb.sb_rextsize;
  3430. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  3431. quota_flag = XFS_QMOPT_RES_RTBLKS;
  3432. } else {
  3433. resrtextents = 0;
  3434. resblks = qblocks = \
  3435. XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
  3436. quota_flag = XFS_QMOPT_RES_REGBLKS;
  3437. }
  3438. /*
  3439. * Allocate and setup the transaction.
  3440. */
  3441. tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
  3442. error = xfs_trans_reserve(tp, resblks,
  3443. XFS_WRITE_LOG_RES(mp), resrtextents,
  3444. XFS_TRANS_PERM_LOG_RES,
  3445. XFS_WRITE_LOG_COUNT);
  3446. /*
  3447. * Check for running out of space
  3448. */
  3449. if (error) {
  3450. /*
  3451. * Free the transaction structure.
  3452. */
  3453. ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  3454. xfs_trans_cancel(tp, 0);
  3455. break;
  3456. }
  3457. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3458. error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
  3459. qblocks, 0, quota_flag);
  3460. if (error)
  3461. goto error1;
  3462. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  3463. xfs_trans_ihold(tp, ip);
  3464. /*
  3465. * Issue the xfs_bmapi() call to allocate the blocks
  3466. */
  3467. XFS_BMAP_INIT(&free_list, &firstfsb);
  3468. error = xfs_bmapi(tp, ip, startoffset_fsb,
  3469. allocatesize_fsb, bmapi_flag,
  3470. &firstfsb, 0, imapp, &nimaps,
  3471. &free_list, NULL);
  3472. if (error) {
  3473. goto error0;
  3474. }
  3475. /*
  3476. * Complete the transaction
  3477. */
  3478. error = xfs_bmap_finish(&tp, &free_list, &committed);
  3479. if (error) {
  3480. goto error0;
  3481. }
  3482. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  3483. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3484. if (error) {
  3485. break;
  3486. }
  3487. allocated_fsb = imapp->br_blockcount;
  3488. if (nimaps == 0) {
  3489. error = XFS_ERROR(ENOSPC);
  3490. break;
  3491. }
  3492. startoffset_fsb += allocated_fsb;
  3493. allocatesize_fsb -= allocated_fsb;
  3494. }
  3495. dmapi_enospc_check:
  3496. if (error == ENOSPC && (attr_flags & ATTR_DMI) == 0 &&
  3497. DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
  3498. error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
  3499. XFS_ITOV(ip), DM_RIGHT_NULL,
  3500. XFS_ITOV(ip), DM_RIGHT_NULL,
  3501. NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
  3502. if (error == 0)
  3503. goto retry; /* Maybe DMAPI app. has made space */
  3504. /* else fall through with error from XFS_SEND_DATA */
  3505. }
  3506. return error;
  3507. error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
  3508. xfs_bmap_cancel(&free_list);
  3509. XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
  3510. error1: /* Just cancel transaction */
  3511. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  3512. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3513. goto dmapi_enospc_check;
  3514. }
  3515. /*
  3516. * Zero file bytes between startoff and endoff inclusive.
  3517. * The iolock is held exclusive and no blocks are buffered.
  3518. */
  3519. STATIC int
  3520. xfs_zero_remaining_bytes(
  3521. xfs_inode_t *ip,
  3522. xfs_off_t startoff,
  3523. xfs_off_t endoff)
  3524. {
  3525. xfs_bmbt_irec_t imap;
  3526. xfs_fileoff_t offset_fsb;
  3527. xfs_off_t lastoffset;
  3528. xfs_off_t offset;
  3529. xfs_buf_t *bp;
  3530. xfs_mount_t *mp = ip->i_mount;
  3531. int nimap;
  3532. int error = 0;
  3533. bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
  3534. XFS_IS_REALTIME_INODE(ip) ?
  3535. mp->m_rtdev_targp : mp->m_ddev_targp);
  3536. for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
  3537. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  3538. nimap = 1;
  3539. error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
  3540. NULL, 0, &imap, &nimap, NULL, NULL);
  3541. if (error || nimap < 1)
  3542. break;
  3543. ASSERT(imap.br_blockcount >= 1);
  3544. ASSERT(imap.br_startoff == offset_fsb);
  3545. lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
  3546. if (lastoffset > endoff)
  3547. lastoffset = endoff;
  3548. if (imap.br_startblock == HOLESTARTBLOCK)
  3549. continue;
  3550. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  3551. if (imap.br_state == XFS_EXT_UNWRITTEN)
  3552. continue;
  3553. XFS_BUF_UNDONE(bp);
  3554. XFS_BUF_UNWRITE(bp);
  3555. XFS_BUF_READ(bp);
  3556. XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
  3557. xfsbdstrat(mp, bp);
  3558. if ((error = xfs_iowait(bp))) {
  3559. xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
  3560. mp, bp, XFS_BUF_ADDR(bp));
  3561. break;
  3562. }
  3563. memset(XFS_BUF_PTR(bp) +
  3564. (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
  3565. 0, lastoffset - offset + 1);
  3566. XFS_BUF_UNDONE(bp);
  3567. XFS_BUF_UNREAD(bp);
  3568. XFS_BUF_WRITE(bp);
  3569. xfsbdstrat(mp, bp);
  3570. if ((error = xfs_iowait(bp))) {
  3571. xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
  3572. mp, bp, XFS_BUF_ADDR(bp));
  3573. break;
  3574. }
  3575. }
  3576. xfs_buf_free(bp);
  3577. return error;
  3578. }
  3579. /*
  3580. * xfs_free_file_space()
  3581. * This routine frees disk space for the given file.
  3582. *
  3583. * This routine is only called by xfs_change_file_space
  3584. * for an UNRESVSP type call.
  3585. *
  3586. * RETURNS:
  3587. * 0 on success
  3588. * errno on error
  3589. *
  3590. */
  3591. STATIC int
  3592. xfs_free_file_space(
  3593. xfs_inode_t *ip,
  3594. xfs_off_t offset,
  3595. xfs_off_t len,
  3596. int attr_flags)
  3597. {
  3598. bhv_vnode_t *vp;
  3599. int committed;
  3600. int done;
  3601. xfs_off_t end_dmi_offset;
  3602. xfs_fileoff_t endoffset_fsb;
  3603. int error;
  3604. xfs_fsblock_t firstfsb;
  3605. xfs_bmap_free_t free_list;
  3606. xfs_bmbt_irec_t imap;
  3607. xfs_off_t ioffset;
  3608. xfs_extlen_t mod=0;
  3609. xfs_mount_t *mp;
  3610. int nimap;
  3611. uint resblks;
  3612. uint rounding;
  3613. int rt;
  3614. xfs_fileoff_t startoffset_fsb;
  3615. xfs_trans_t *tp;
  3616. int need_iolock = 1;
  3617. vp = XFS_ITOV(ip);
  3618. mp = ip->i_mount;
  3619. xfs_itrace_entry(ip);
  3620. if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
  3621. return error;
  3622. error = 0;
  3623. if (len <= 0) /* if nothing being freed */
  3624. return error;
  3625. rt = XFS_IS_REALTIME_INODE(ip);
  3626. startoffset_fsb = XFS_B_TO_FSB(mp, offset);
  3627. end_dmi_offset = offset + len;
  3628. endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
  3629. if (offset < ip->i_size && (attr_flags & ATTR_DMI) == 0 &&
  3630. DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
  3631. if (end_dmi_offset > ip->i_size)
  3632. end_dmi_offset = ip->i_size;
  3633. error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, vp,
  3634. offset, end_dmi_offset - offset,
  3635. AT_DELAY_FLAG(attr_flags), NULL);
  3636. if (error)
  3637. return error;
  3638. }
  3639. if (attr_flags & ATTR_NOLOCK)
  3640. need_iolock = 0;
  3641. if (need_iolock) {
  3642. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  3643. vn_iowait(ip); /* wait for the completion of any pending DIOs */
  3644. }
  3645. rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
  3646. ioffset = offset & ~(rounding - 1);
  3647. if (VN_CACHED(vp) != 0) {
  3648. xfs_inval_cached_trace(ip, ioffset, -1, ioffset, -1);
  3649. error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
  3650. if (error)
  3651. goto out_unlock_iolock;
  3652. }
  3653. /*
  3654. * Need to zero the stuff we're not freeing, on disk.
  3655. * If its a realtime file & can't use unwritten extents then we
  3656. * actually need to zero the extent edges. Otherwise xfs_bunmapi
  3657. * will take care of it for us.
  3658. */
  3659. if (rt && !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
  3660. nimap = 1;
  3661. error = xfs_bmapi(NULL, ip, startoffset_fsb,
  3662. 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
  3663. if (error)
  3664. goto out_unlock_iolock;
  3665. ASSERT(nimap == 0 || nimap == 1);
  3666. if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
  3667. xfs_daddr_t block;
  3668. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  3669. block = imap.br_startblock;
  3670. mod = do_div(block, mp->m_sb.sb_rextsize);
  3671. if (mod)
  3672. startoffset_fsb += mp->m_sb.sb_rextsize - mod;
  3673. }
  3674. nimap = 1;
  3675. error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
  3676. 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
  3677. if (error)
  3678. goto out_unlock_iolock;
  3679. ASSERT(nimap == 0 || nimap == 1);
  3680. if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
  3681. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  3682. mod++;
  3683. if (mod && (mod != mp->m_sb.sb_rextsize))
  3684. endoffset_fsb -= mod;
  3685. }
  3686. }
  3687. if ((done = (endoffset_fsb <= startoffset_fsb)))
  3688. /*
  3689. * One contiguous piece to clear
  3690. */
  3691. error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
  3692. else {
  3693. /*
  3694. * Some full blocks, possibly two pieces to clear
  3695. */
  3696. if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
  3697. error = xfs_zero_remaining_bytes(ip, offset,
  3698. XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
  3699. if (!error &&
  3700. XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
  3701. error = xfs_zero_remaining_bytes(ip,
  3702. XFS_FSB_TO_B(mp, endoffset_fsb),
  3703. offset + len - 1);
  3704. }
  3705. /*
  3706. * free file space until done or until there is an error
  3707. */
  3708. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  3709. while (!error && !done) {
  3710. /*
  3711. * allocate and setup the transaction. Allow this
  3712. * transaction to dip into the reserve blocks to ensure
  3713. * the freeing of the space succeeds at ENOSPC.
  3714. */
  3715. tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
  3716. tp->t_flags |= XFS_TRANS_RESERVE;
  3717. error = xfs_trans_reserve(tp,
  3718. resblks,
  3719. XFS_WRITE_LOG_RES(mp),
  3720. 0,
  3721. XFS_TRANS_PERM_LOG_RES,
  3722. XFS_WRITE_LOG_COUNT);
  3723. /*
  3724. * check for running out of space
  3725. */
  3726. if (error) {
  3727. /*
  3728. * Free the transaction structure.
  3729. */
  3730. ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  3731. xfs_trans_cancel(tp, 0);
  3732. break;
  3733. }
  3734. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3735. error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
  3736. ip->i_udquot, ip->i_gdquot, resblks, 0,
  3737. XFS_QMOPT_RES_REGBLKS);
  3738. if (error)
  3739. goto error1;
  3740. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  3741. xfs_trans_ihold(tp, ip);
  3742. /*
  3743. * issue the bunmapi() call to free the blocks
  3744. */
  3745. XFS_BMAP_INIT(&free_list, &firstfsb);
  3746. error = xfs_bunmapi(tp, ip, startoffset_fsb,
  3747. endoffset_fsb - startoffset_fsb,
  3748. 0, 2, &firstfsb, &free_list, NULL, &done);
  3749. if (error) {
  3750. goto error0;
  3751. }
  3752. /*
  3753. * complete the transaction
  3754. */
  3755. error = xfs_bmap_finish(&tp, &free_list, &committed);
  3756. if (error) {
  3757. goto error0;
  3758. }
  3759. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  3760. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3761. }
  3762. out_unlock_iolock:
  3763. if (need_iolock)
  3764. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  3765. return error;
  3766. error0:
  3767. xfs_bmap_cancel(&free_list);
  3768. error1:
  3769. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  3770. xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
  3771. XFS_ILOCK_EXCL);
  3772. return error;
  3773. }
  3774. /*
  3775. * xfs_change_file_space()
  3776. * This routine allocates or frees disk space for the given file.
  3777. * The user specified parameters are checked for alignment and size
  3778. * limitations.
  3779. *
  3780. * RETURNS:
  3781. * 0 on success
  3782. * errno on error
  3783. *
  3784. */
  3785. int
  3786. xfs_change_file_space(
  3787. xfs_inode_t *ip,
  3788. int cmd,
  3789. xfs_flock64_t *bf,
  3790. xfs_off_t offset,
  3791. cred_t *credp,
  3792. int attr_flags)
  3793. {
  3794. xfs_mount_t *mp = ip->i_mount;
  3795. int clrprealloc;
  3796. int error;
  3797. xfs_fsize_t fsize;
  3798. int setprealloc;
  3799. xfs_off_t startoffset;
  3800. xfs_off_t llen;
  3801. xfs_trans_t *tp;
  3802. bhv_vattr_t va;
  3803. xfs_itrace_entry(ip);
  3804. if (!S_ISREG(ip->i_d.di_mode))
  3805. return XFS_ERROR(EINVAL);
  3806. switch (bf->l_whence) {
  3807. case 0: /*SEEK_SET*/
  3808. break;
  3809. case 1: /*SEEK_CUR*/
  3810. bf->l_start += offset;
  3811. break;
  3812. case 2: /*SEEK_END*/
  3813. bf->l_start += ip->i_size;
  3814. break;
  3815. default:
  3816. return XFS_ERROR(EINVAL);
  3817. }
  3818. llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
  3819. if ( (bf->l_start < 0)
  3820. || (bf->l_start > XFS_MAXIOFFSET(mp))
  3821. || (bf->l_start + llen < 0)
  3822. || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
  3823. return XFS_ERROR(EINVAL);
  3824. bf->l_whence = 0;
  3825. startoffset = bf->l_start;
  3826. fsize = ip->i_size;
  3827. /*
  3828. * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
  3829. * file space.
  3830. * These calls do NOT zero the data space allocated to the file,
  3831. * nor do they change the file size.
  3832. *
  3833. * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
  3834. * space.
  3835. * These calls cause the new file data to be zeroed and the file
  3836. * size to be changed.
  3837. */
  3838. setprealloc = clrprealloc = 0;
  3839. switch (cmd) {
  3840. case XFS_IOC_RESVSP:
  3841. case XFS_IOC_RESVSP64:
  3842. error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
  3843. 1, attr_flags);
  3844. if (error)
  3845. return error;
  3846. setprealloc = 1;
  3847. break;
  3848. case XFS_IOC_UNRESVSP:
  3849. case XFS_IOC_UNRESVSP64:
  3850. if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
  3851. attr_flags)))
  3852. return error;
  3853. break;
  3854. case XFS_IOC_ALLOCSP:
  3855. case XFS_IOC_ALLOCSP64:
  3856. case XFS_IOC_FREESP:
  3857. case XFS_IOC_FREESP64:
  3858. if (startoffset > fsize) {
  3859. error = xfs_alloc_file_space(ip, fsize,
  3860. startoffset - fsize, 0, attr_flags);
  3861. if (error)
  3862. break;
  3863. }
  3864. va.va_mask = XFS_AT_SIZE;
  3865. va.va_size = startoffset;
  3866. error = xfs_setattr(ip, &va, attr_flags, credp);
  3867. if (error)
  3868. return error;
  3869. clrprealloc = 1;
  3870. break;
  3871. default:
  3872. ASSERT(0);
  3873. return XFS_ERROR(EINVAL);
  3874. }
  3875. /*
  3876. * update the inode timestamp, mode, and prealloc flag bits
  3877. */
  3878. tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
  3879. if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
  3880. 0, 0, 0))) {
  3881. /* ASSERT(0); */
  3882. xfs_trans_cancel(tp, 0);
  3883. return error;
  3884. }
  3885. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3886. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  3887. xfs_trans_ihold(tp, ip);
  3888. if ((attr_flags & ATTR_DMI) == 0) {
  3889. ip->i_d.di_mode &= ~S_ISUID;
  3890. /*
  3891. * Note that we don't have to worry about mandatory
  3892. * file locking being disabled here because we only
  3893. * clear the S_ISGID bit if the Group execute bit is
  3894. * on, but if it was on then mandatory locking wouldn't
  3895. * have been enabled.
  3896. */
  3897. if (ip->i_d.di_mode & S_IXGRP)
  3898. ip->i_d.di_mode &= ~S_ISGID;
  3899. xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  3900. }
  3901. if (setprealloc)
  3902. ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
  3903. else if (clrprealloc)
  3904. ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
  3905. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  3906. xfs_trans_set_sync(tp);
  3907. error = xfs_trans_commit(tp, 0);
  3908. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3909. return error;
  3910. }