xfs_vnodeops.c 79 KB

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