xfs_vnodeops.c 101 KB

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