xfs_vnodeops.c 90 KB

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