xfs_vnodeops.c 89 KB

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