xfs_vnodeops.c 90 KB

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