xfs_vnodeops.c 87 KB

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