xfs_vnodeops.c 106 KB

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