xfs_vnodeops.c 109 KB

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