xfs_vnodeops.c 108 KB

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