xfs_vnodeops.c 86 KB

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