xfs_vnodeops.c 106 KB

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