xfs_vnodeops.c 78 KB

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