xfs_inode.c 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335
  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 <linux/log2.h>
  19. #include "xfs.h"
  20. #include "xfs_fs.h"
  21. #include "xfs_shared.h"
  22. #include "xfs_format.h"
  23. #include "xfs_log_format.h"
  24. #include "xfs_trans_resv.h"
  25. #include "xfs_inum.h"
  26. #include "xfs_sb.h"
  27. #include "xfs_ag.h"
  28. #include "xfs_mount.h"
  29. #include "xfs_inode.h"
  30. #include "xfs_da_format.h"
  31. #include "xfs_da_btree.h"
  32. #include "xfs_dir2.h"
  33. #include "xfs_attr_sf.h"
  34. #include "xfs_attr.h"
  35. #include "xfs_trans_space.h"
  36. #include "xfs_trans.h"
  37. #include "xfs_buf_item.h"
  38. #include "xfs_inode_item.h"
  39. #include "xfs_ialloc.h"
  40. #include "xfs_bmap.h"
  41. #include "xfs_bmap_util.h"
  42. #include "xfs_error.h"
  43. #include "xfs_quota.h"
  44. #include "xfs_filestream.h"
  45. #include "xfs_cksum.h"
  46. #include "xfs_trace.h"
  47. #include "xfs_icache.h"
  48. #include "xfs_symlink.h"
  49. #include "xfs_trans_priv.h"
  50. #include "xfs_log.h"
  51. #include "xfs_bmap_btree.h"
  52. kmem_zone_t *xfs_inode_zone;
  53. /*
  54. * Used in xfs_itruncate_extents(). This is the maximum number of extents
  55. * freed from a file in a single transaction.
  56. */
  57. #define XFS_ITRUNC_MAX_EXTENTS 2
  58. STATIC int xfs_iflush_int(xfs_inode_t *, xfs_buf_t *);
  59. STATIC int xfs_iunlink_remove(xfs_trans_t *, xfs_inode_t *);
  60. /*
  61. * helper function to extract extent size hint from inode
  62. */
  63. xfs_extlen_t
  64. xfs_get_extsz_hint(
  65. struct xfs_inode *ip)
  66. {
  67. if ((ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE) && ip->i_d.di_extsize)
  68. return ip->i_d.di_extsize;
  69. if (XFS_IS_REALTIME_INODE(ip))
  70. return ip->i_mount->m_sb.sb_rextsize;
  71. return 0;
  72. }
  73. /*
  74. * These two are wrapper routines around the xfs_ilock() routine used to
  75. * centralize some grungy code. They are used in places that wish to lock the
  76. * inode solely for reading the extents. The reason these places can't just
  77. * call xfs_ilock(ip, XFS_ILOCK_SHARED) is that the inode lock also guards to
  78. * bringing in of the extents from disk for a file in b-tree format. If the
  79. * inode is in b-tree format, then we need to lock the inode exclusively until
  80. * the extents are read in. Locking it exclusively all the time would limit
  81. * our parallelism unnecessarily, though. What we do instead is check to see
  82. * if the extents have been read in yet, and only lock the inode exclusively
  83. * if they have not.
  84. *
  85. * The functions return a value which should be given to the corresponding
  86. * xfs_iunlock() call.
  87. */
  88. uint
  89. xfs_ilock_data_map_shared(
  90. struct xfs_inode *ip)
  91. {
  92. uint lock_mode = XFS_ILOCK_SHARED;
  93. if (ip->i_d.di_format == XFS_DINODE_FMT_BTREE &&
  94. (ip->i_df.if_flags & XFS_IFEXTENTS) == 0)
  95. lock_mode = XFS_ILOCK_EXCL;
  96. xfs_ilock(ip, lock_mode);
  97. return lock_mode;
  98. }
  99. uint
  100. xfs_ilock_attr_map_shared(
  101. struct xfs_inode *ip)
  102. {
  103. uint lock_mode = XFS_ILOCK_SHARED;
  104. if (ip->i_d.di_aformat == XFS_DINODE_FMT_BTREE &&
  105. (ip->i_afp->if_flags & XFS_IFEXTENTS) == 0)
  106. lock_mode = XFS_ILOCK_EXCL;
  107. xfs_ilock(ip, lock_mode);
  108. return lock_mode;
  109. }
  110. /*
  111. * The xfs inode contains 2 locks: a multi-reader lock called the
  112. * i_iolock and a multi-reader lock called the i_lock. This routine
  113. * allows either or both of the locks to be obtained.
  114. *
  115. * The 2 locks should always be ordered so that the IO lock is
  116. * obtained first in order to prevent deadlock.
  117. *
  118. * ip -- the inode being locked
  119. * lock_flags -- this parameter indicates the inode's locks
  120. * to be locked. It can be:
  121. * XFS_IOLOCK_SHARED,
  122. * XFS_IOLOCK_EXCL,
  123. * XFS_ILOCK_SHARED,
  124. * XFS_ILOCK_EXCL,
  125. * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
  126. * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
  127. * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
  128. * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
  129. */
  130. void
  131. xfs_ilock(
  132. xfs_inode_t *ip,
  133. uint lock_flags)
  134. {
  135. trace_xfs_ilock(ip, lock_flags, _RET_IP_);
  136. /*
  137. * You can't set both SHARED and EXCL for the same lock,
  138. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  139. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  140. */
  141. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  142. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  143. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  144. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  145. ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
  146. if (lock_flags & XFS_IOLOCK_EXCL)
  147. mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
  148. else if (lock_flags & XFS_IOLOCK_SHARED)
  149. mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
  150. if (lock_flags & XFS_ILOCK_EXCL)
  151. mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
  152. else if (lock_flags & XFS_ILOCK_SHARED)
  153. mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
  154. }
  155. /*
  156. * This is just like xfs_ilock(), except that the caller
  157. * is guaranteed not to sleep. It returns 1 if it gets
  158. * the requested locks and 0 otherwise. If the IO lock is
  159. * obtained but the inode lock cannot be, then the IO lock
  160. * is dropped before returning.
  161. *
  162. * ip -- the inode being locked
  163. * lock_flags -- this parameter indicates the inode's locks to be
  164. * to be locked. See the comment for xfs_ilock() for a list
  165. * of valid values.
  166. */
  167. int
  168. xfs_ilock_nowait(
  169. xfs_inode_t *ip,
  170. uint lock_flags)
  171. {
  172. trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_);
  173. /*
  174. * You can't set both SHARED and EXCL for the same lock,
  175. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  176. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  177. */
  178. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  179. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  180. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  181. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  182. ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
  183. if (lock_flags & XFS_IOLOCK_EXCL) {
  184. if (!mrtryupdate(&ip->i_iolock))
  185. goto out;
  186. } else if (lock_flags & XFS_IOLOCK_SHARED) {
  187. if (!mrtryaccess(&ip->i_iolock))
  188. goto out;
  189. }
  190. if (lock_flags & XFS_ILOCK_EXCL) {
  191. if (!mrtryupdate(&ip->i_lock))
  192. goto out_undo_iolock;
  193. } else if (lock_flags & XFS_ILOCK_SHARED) {
  194. if (!mrtryaccess(&ip->i_lock))
  195. goto out_undo_iolock;
  196. }
  197. return 1;
  198. out_undo_iolock:
  199. if (lock_flags & XFS_IOLOCK_EXCL)
  200. mrunlock_excl(&ip->i_iolock);
  201. else if (lock_flags & XFS_IOLOCK_SHARED)
  202. mrunlock_shared(&ip->i_iolock);
  203. out:
  204. return 0;
  205. }
  206. /*
  207. * xfs_iunlock() is used to drop the inode locks acquired with
  208. * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
  209. * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
  210. * that we know which locks to drop.
  211. *
  212. * ip -- the inode being unlocked
  213. * lock_flags -- this parameter indicates the inode's locks to be
  214. * to be unlocked. See the comment for xfs_ilock() for a list
  215. * of valid values for this parameter.
  216. *
  217. */
  218. void
  219. xfs_iunlock(
  220. xfs_inode_t *ip,
  221. uint lock_flags)
  222. {
  223. /*
  224. * You can't set both SHARED and EXCL for the same lock,
  225. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  226. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  227. */
  228. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  229. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  230. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  231. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  232. ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
  233. ASSERT(lock_flags != 0);
  234. if (lock_flags & XFS_IOLOCK_EXCL)
  235. mrunlock_excl(&ip->i_iolock);
  236. else if (lock_flags & XFS_IOLOCK_SHARED)
  237. mrunlock_shared(&ip->i_iolock);
  238. if (lock_flags & XFS_ILOCK_EXCL)
  239. mrunlock_excl(&ip->i_lock);
  240. else if (lock_flags & XFS_ILOCK_SHARED)
  241. mrunlock_shared(&ip->i_lock);
  242. trace_xfs_iunlock(ip, lock_flags, _RET_IP_);
  243. }
  244. /*
  245. * give up write locks. the i/o lock cannot be held nested
  246. * if it is being demoted.
  247. */
  248. void
  249. xfs_ilock_demote(
  250. xfs_inode_t *ip,
  251. uint lock_flags)
  252. {
  253. ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
  254. ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
  255. if (lock_flags & XFS_ILOCK_EXCL)
  256. mrdemote(&ip->i_lock);
  257. if (lock_flags & XFS_IOLOCK_EXCL)
  258. mrdemote(&ip->i_iolock);
  259. trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_);
  260. }
  261. #if defined(DEBUG) || defined(XFS_WARN)
  262. int
  263. xfs_isilocked(
  264. xfs_inode_t *ip,
  265. uint lock_flags)
  266. {
  267. if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) {
  268. if (!(lock_flags & XFS_ILOCK_SHARED))
  269. return !!ip->i_lock.mr_writer;
  270. return rwsem_is_locked(&ip->i_lock.mr_lock);
  271. }
  272. if (lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) {
  273. if (!(lock_flags & XFS_IOLOCK_SHARED))
  274. return !!ip->i_iolock.mr_writer;
  275. return rwsem_is_locked(&ip->i_iolock.mr_lock);
  276. }
  277. ASSERT(0);
  278. return 0;
  279. }
  280. #endif
  281. #ifdef DEBUG
  282. int xfs_locked_n;
  283. int xfs_small_retries;
  284. int xfs_middle_retries;
  285. int xfs_lots_retries;
  286. int xfs_lock_delays;
  287. #endif
  288. /*
  289. * Bump the subclass so xfs_lock_inodes() acquires each lock with
  290. * a different value
  291. */
  292. static inline int
  293. xfs_lock_inumorder(int lock_mode, int subclass)
  294. {
  295. if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
  296. lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
  297. if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
  298. lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
  299. return lock_mode;
  300. }
  301. /*
  302. * The following routine will lock n inodes in exclusive mode.
  303. * We assume the caller calls us with the inodes in i_ino order.
  304. *
  305. * We need to detect deadlock where an inode that we lock
  306. * is in the AIL and we start waiting for another inode that is locked
  307. * by a thread in a long running transaction (such as truncate). This can
  308. * result in deadlock since the long running trans might need to wait
  309. * for the inode we just locked in order to push the tail and free space
  310. * in the log.
  311. */
  312. void
  313. xfs_lock_inodes(
  314. xfs_inode_t **ips,
  315. int inodes,
  316. uint lock_mode)
  317. {
  318. int attempts = 0, i, j, try_lock;
  319. xfs_log_item_t *lp;
  320. ASSERT(ips && (inodes >= 2)); /* we need at least two */
  321. try_lock = 0;
  322. i = 0;
  323. again:
  324. for (; i < inodes; i++) {
  325. ASSERT(ips[i]);
  326. if (i && (ips[i] == ips[i-1])) /* Already locked */
  327. continue;
  328. /*
  329. * If try_lock is not set yet, make sure all locked inodes
  330. * are not in the AIL.
  331. * If any are, set try_lock to be used later.
  332. */
  333. if (!try_lock) {
  334. for (j = (i - 1); j >= 0 && !try_lock; j--) {
  335. lp = (xfs_log_item_t *)ips[j]->i_itemp;
  336. if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
  337. try_lock++;
  338. }
  339. }
  340. }
  341. /*
  342. * If any of the previous locks we have locked is in the AIL,
  343. * we must TRY to get the second and subsequent locks. If
  344. * we can't get any, we must release all we have
  345. * and try again.
  346. */
  347. if (try_lock) {
  348. /* try_lock must be 0 if i is 0. */
  349. /*
  350. * try_lock means we have an inode locked
  351. * that is in the AIL.
  352. */
  353. ASSERT(i != 0);
  354. if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
  355. attempts++;
  356. /*
  357. * Unlock all previous guys and try again.
  358. * xfs_iunlock will try to push the tail
  359. * if the inode is in the AIL.
  360. */
  361. for(j = i - 1; j >= 0; j--) {
  362. /*
  363. * Check to see if we've already
  364. * unlocked this one.
  365. * Not the first one going back,
  366. * and the inode ptr is the same.
  367. */
  368. if ((j != (i - 1)) && ips[j] ==
  369. ips[j+1])
  370. continue;
  371. xfs_iunlock(ips[j], lock_mode);
  372. }
  373. if ((attempts % 5) == 0) {
  374. delay(1); /* Don't just spin the CPU */
  375. #ifdef DEBUG
  376. xfs_lock_delays++;
  377. #endif
  378. }
  379. i = 0;
  380. try_lock = 0;
  381. goto again;
  382. }
  383. } else {
  384. xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
  385. }
  386. }
  387. #ifdef DEBUG
  388. if (attempts) {
  389. if (attempts < 5) xfs_small_retries++;
  390. else if (attempts < 100) xfs_middle_retries++;
  391. else xfs_lots_retries++;
  392. } else {
  393. xfs_locked_n++;
  394. }
  395. #endif
  396. }
  397. /*
  398. * xfs_lock_two_inodes() can only be used to lock one type of lock
  399. * at a time - the iolock or the ilock, but not both at once. If
  400. * we lock both at once, lockdep will report false positives saying
  401. * we have violated locking orders.
  402. */
  403. void
  404. xfs_lock_two_inodes(
  405. xfs_inode_t *ip0,
  406. xfs_inode_t *ip1,
  407. uint lock_mode)
  408. {
  409. xfs_inode_t *temp;
  410. int attempts = 0;
  411. xfs_log_item_t *lp;
  412. if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
  413. ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
  414. ASSERT(ip0->i_ino != ip1->i_ino);
  415. if (ip0->i_ino > ip1->i_ino) {
  416. temp = ip0;
  417. ip0 = ip1;
  418. ip1 = temp;
  419. }
  420. again:
  421. xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
  422. /*
  423. * If the first lock we have locked is in the AIL, we must TRY to get
  424. * the second lock. If we can't get it, we must release the first one
  425. * and try again.
  426. */
  427. lp = (xfs_log_item_t *)ip0->i_itemp;
  428. if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
  429. if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
  430. xfs_iunlock(ip0, lock_mode);
  431. if ((++attempts % 5) == 0)
  432. delay(1); /* Don't just spin the CPU */
  433. goto again;
  434. }
  435. } else {
  436. xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
  437. }
  438. }
  439. void
  440. __xfs_iflock(
  441. struct xfs_inode *ip)
  442. {
  443. wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IFLOCK_BIT);
  444. DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IFLOCK_BIT);
  445. do {
  446. prepare_to_wait_exclusive(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
  447. if (xfs_isiflocked(ip))
  448. io_schedule();
  449. } while (!xfs_iflock_nowait(ip));
  450. finish_wait(wq, &wait.wait);
  451. }
  452. STATIC uint
  453. _xfs_dic2xflags(
  454. __uint16_t di_flags)
  455. {
  456. uint flags = 0;
  457. if (di_flags & XFS_DIFLAG_ANY) {
  458. if (di_flags & XFS_DIFLAG_REALTIME)
  459. flags |= XFS_XFLAG_REALTIME;
  460. if (di_flags & XFS_DIFLAG_PREALLOC)
  461. flags |= XFS_XFLAG_PREALLOC;
  462. if (di_flags & XFS_DIFLAG_IMMUTABLE)
  463. flags |= XFS_XFLAG_IMMUTABLE;
  464. if (di_flags & XFS_DIFLAG_APPEND)
  465. flags |= XFS_XFLAG_APPEND;
  466. if (di_flags & XFS_DIFLAG_SYNC)
  467. flags |= XFS_XFLAG_SYNC;
  468. if (di_flags & XFS_DIFLAG_NOATIME)
  469. flags |= XFS_XFLAG_NOATIME;
  470. if (di_flags & XFS_DIFLAG_NODUMP)
  471. flags |= XFS_XFLAG_NODUMP;
  472. if (di_flags & XFS_DIFLAG_RTINHERIT)
  473. flags |= XFS_XFLAG_RTINHERIT;
  474. if (di_flags & XFS_DIFLAG_PROJINHERIT)
  475. flags |= XFS_XFLAG_PROJINHERIT;
  476. if (di_flags & XFS_DIFLAG_NOSYMLINKS)
  477. flags |= XFS_XFLAG_NOSYMLINKS;
  478. if (di_flags & XFS_DIFLAG_EXTSIZE)
  479. flags |= XFS_XFLAG_EXTSIZE;
  480. if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
  481. flags |= XFS_XFLAG_EXTSZINHERIT;
  482. if (di_flags & XFS_DIFLAG_NODEFRAG)
  483. flags |= XFS_XFLAG_NODEFRAG;
  484. if (di_flags & XFS_DIFLAG_FILESTREAM)
  485. flags |= XFS_XFLAG_FILESTREAM;
  486. }
  487. return flags;
  488. }
  489. uint
  490. xfs_ip2xflags(
  491. xfs_inode_t *ip)
  492. {
  493. xfs_icdinode_t *dic = &ip->i_d;
  494. return _xfs_dic2xflags(dic->di_flags) |
  495. (XFS_IFORK_Q(ip) ? XFS_XFLAG_HASATTR : 0);
  496. }
  497. uint
  498. xfs_dic2xflags(
  499. xfs_dinode_t *dip)
  500. {
  501. return _xfs_dic2xflags(be16_to_cpu(dip->di_flags)) |
  502. (XFS_DFORK_Q(dip) ? XFS_XFLAG_HASATTR : 0);
  503. }
  504. /*
  505. * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
  506. * is allowed, otherwise it has to be an exact match. If a CI match is found,
  507. * ci_name->name will point to a the actual name (caller must free) or
  508. * will be set to NULL if an exact match is found.
  509. */
  510. int
  511. xfs_lookup(
  512. xfs_inode_t *dp,
  513. struct xfs_name *name,
  514. xfs_inode_t **ipp,
  515. struct xfs_name *ci_name)
  516. {
  517. xfs_ino_t inum;
  518. int error;
  519. uint lock_mode;
  520. trace_xfs_lookup(dp, name);
  521. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  522. return XFS_ERROR(EIO);
  523. lock_mode = xfs_ilock_data_map_shared(dp);
  524. error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
  525. xfs_iunlock(dp, lock_mode);
  526. if (error)
  527. goto out;
  528. error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp);
  529. if (error)
  530. goto out_free_name;
  531. return 0;
  532. out_free_name:
  533. if (ci_name)
  534. kmem_free(ci_name->name);
  535. out:
  536. *ipp = NULL;
  537. return error;
  538. }
  539. /*
  540. * Allocate an inode on disk and return a copy of its in-core version.
  541. * The in-core inode is locked exclusively. Set mode, nlink, and rdev
  542. * appropriately within the inode. The uid and gid for the inode are
  543. * set according to the contents of the given cred structure.
  544. *
  545. * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
  546. * has a free inode available, call xfs_iget() to obtain the in-core
  547. * version of the allocated inode. Finally, fill in the inode and
  548. * log its initial contents. In this case, ialloc_context would be
  549. * set to NULL.
  550. *
  551. * If xfs_dialloc() does not have an available inode, it will replenish
  552. * its supply by doing an allocation. Since we can only do one
  553. * allocation within a transaction without deadlocks, we must commit
  554. * the current transaction before returning the inode itself.
  555. * In this case, therefore, we will set ialloc_context and return.
  556. * The caller should then commit the current transaction, start a new
  557. * transaction, and call xfs_ialloc() again to actually get the inode.
  558. *
  559. * To ensure that some other process does not grab the inode that
  560. * was allocated during the first call to xfs_ialloc(), this routine
  561. * also returns the [locked] bp pointing to the head of the freelist
  562. * as ialloc_context. The caller should hold this buffer across
  563. * the commit and pass it back into this routine on the second call.
  564. *
  565. * If we are allocating quota inodes, we do not have a parent inode
  566. * to attach to or associate with (i.e. pip == NULL) because they
  567. * are not linked into the directory structure - they are attached
  568. * directly to the superblock - and so have no parent.
  569. */
  570. int
  571. xfs_ialloc(
  572. xfs_trans_t *tp,
  573. xfs_inode_t *pip,
  574. umode_t mode,
  575. xfs_nlink_t nlink,
  576. xfs_dev_t rdev,
  577. prid_t prid,
  578. int okalloc,
  579. xfs_buf_t **ialloc_context,
  580. xfs_inode_t **ipp)
  581. {
  582. struct xfs_mount *mp = tp->t_mountp;
  583. xfs_ino_t ino;
  584. xfs_inode_t *ip;
  585. uint flags;
  586. int error;
  587. timespec_t tv;
  588. /*
  589. * Call the space management code to pick
  590. * the on-disk inode to be allocated.
  591. */
  592. error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc,
  593. ialloc_context, &ino);
  594. if (error)
  595. return error;
  596. if (*ialloc_context || ino == NULLFSINO) {
  597. *ipp = NULL;
  598. return 0;
  599. }
  600. ASSERT(*ialloc_context == NULL);
  601. /*
  602. * Get the in-core inode with the lock held exclusively.
  603. * This is because we're setting fields here we need
  604. * to prevent others from looking at until we're done.
  605. */
  606. error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE,
  607. XFS_ILOCK_EXCL, &ip);
  608. if (error)
  609. return error;
  610. ASSERT(ip != NULL);
  611. /*
  612. * We always convert v1 inodes to v2 now - we only support filesystems
  613. * with >= v2 inode capability, so there is no reason for ever leaving
  614. * an inode in v1 format.
  615. */
  616. if (ip->i_d.di_version == 1)
  617. ip->i_d.di_version = 2;
  618. ip->i_d.di_mode = mode;
  619. ip->i_d.di_onlink = 0;
  620. ip->i_d.di_nlink = nlink;
  621. ASSERT(ip->i_d.di_nlink == nlink);
  622. ip->i_d.di_uid = xfs_kuid_to_uid(current_fsuid());
  623. ip->i_d.di_gid = xfs_kgid_to_gid(current_fsgid());
  624. xfs_set_projid(ip, prid);
  625. memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
  626. if (pip && XFS_INHERIT_GID(pip)) {
  627. ip->i_d.di_gid = pip->i_d.di_gid;
  628. if ((pip->i_d.di_mode & S_ISGID) && S_ISDIR(mode)) {
  629. ip->i_d.di_mode |= S_ISGID;
  630. }
  631. }
  632. /*
  633. * If the group ID of the new file does not match the effective group
  634. * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
  635. * (and only if the irix_sgid_inherit compatibility variable is set).
  636. */
  637. if ((irix_sgid_inherit) &&
  638. (ip->i_d.di_mode & S_ISGID) &&
  639. (!in_group_p(xfs_gid_to_kgid(ip->i_d.di_gid)))) {
  640. ip->i_d.di_mode &= ~S_ISGID;
  641. }
  642. ip->i_d.di_size = 0;
  643. ip->i_d.di_nextents = 0;
  644. ASSERT(ip->i_d.di_nblocks == 0);
  645. nanotime(&tv);
  646. ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
  647. ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
  648. ip->i_d.di_atime = ip->i_d.di_mtime;
  649. ip->i_d.di_ctime = ip->i_d.di_mtime;
  650. /*
  651. * di_gen will have been taken care of in xfs_iread.
  652. */
  653. ip->i_d.di_extsize = 0;
  654. ip->i_d.di_dmevmask = 0;
  655. ip->i_d.di_dmstate = 0;
  656. ip->i_d.di_flags = 0;
  657. if (ip->i_d.di_version == 3) {
  658. ASSERT(ip->i_d.di_ino == ino);
  659. ASSERT(uuid_equal(&ip->i_d.di_uuid, &mp->m_sb.sb_uuid));
  660. ip->i_d.di_crc = 0;
  661. ip->i_d.di_changecount = 1;
  662. ip->i_d.di_lsn = 0;
  663. ip->i_d.di_flags2 = 0;
  664. memset(&(ip->i_d.di_pad2[0]), 0, sizeof(ip->i_d.di_pad2));
  665. ip->i_d.di_crtime = ip->i_d.di_mtime;
  666. }
  667. flags = XFS_ILOG_CORE;
  668. switch (mode & S_IFMT) {
  669. case S_IFIFO:
  670. case S_IFCHR:
  671. case S_IFBLK:
  672. case S_IFSOCK:
  673. ip->i_d.di_format = XFS_DINODE_FMT_DEV;
  674. ip->i_df.if_u2.if_rdev = rdev;
  675. ip->i_df.if_flags = 0;
  676. flags |= XFS_ILOG_DEV;
  677. break;
  678. case S_IFREG:
  679. case S_IFDIR:
  680. if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
  681. uint di_flags = 0;
  682. if (S_ISDIR(mode)) {
  683. if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
  684. di_flags |= XFS_DIFLAG_RTINHERIT;
  685. if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
  686. di_flags |= XFS_DIFLAG_EXTSZINHERIT;
  687. ip->i_d.di_extsize = pip->i_d.di_extsize;
  688. }
  689. } else if (S_ISREG(mode)) {
  690. if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
  691. di_flags |= XFS_DIFLAG_REALTIME;
  692. if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
  693. di_flags |= XFS_DIFLAG_EXTSIZE;
  694. ip->i_d.di_extsize = pip->i_d.di_extsize;
  695. }
  696. }
  697. if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
  698. xfs_inherit_noatime)
  699. di_flags |= XFS_DIFLAG_NOATIME;
  700. if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
  701. xfs_inherit_nodump)
  702. di_flags |= XFS_DIFLAG_NODUMP;
  703. if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
  704. xfs_inherit_sync)
  705. di_flags |= XFS_DIFLAG_SYNC;
  706. if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
  707. xfs_inherit_nosymlinks)
  708. di_flags |= XFS_DIFLAG_NOSYMLINKS;
  709. if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  710. di_flags |= XFS_DIFLAG_PROJINHERIT;
  711. if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
  712. xfs_inherit_nodefrag)
  713. di_flags |= XFS_DIFLAG_NODEFRAG;
  714. if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
  715. di_flags |= XFS_DIFLAG_FILESTREAM;
  716. ip->i_d.di_flags |= di_flags;
  717. }
  718. /* FALLTHROUGH */
  719. case S_IFLNK:
  720. ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
  721. ip->i_df.if_flags = XFS_IFEXTENTS;
  722. ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
  723. ip->i_df.if_u1.if_extents = NULL;
  724. break;
  725. default:
  726. ASSERT(0);
  727. }
  728. /*
  729. * Attribute fork settings for new inode.
  730. */
  731. ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
  732. ip->i_d.di_anextents = 0;
  733. /*
  734. * Log the new values stuffed into the inode.
  735. */
  736. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  737. xfs_trans_log_inode(tp, ip, flags);
  738. /* now that we have an i_mode we can setup inode ops and unlock */
  739. xfs_setup_inode(ip);
  740. *ipp = ip;
  741. return 0;
  742. }
  743. /*
  744. * Allocates a new inode from disk and return a pointer to the
  745. * incore copy. This routine will internally commit the current
  746. * transaction and allocate a new one if the Space Manager needed
  747. * to do an allocation to replenish the inode free-list.
  748. *
  749. * This routine is designed to be called from xfs_create and
  750. * xfs_create_dir.
  751. *
  752. */
  753. int
  754. xfs_dir_ialloc(
  755. xfs_trans_t **tpp, /* input: current transaction;
  756. output: may be a new transaction. */
  757. xfs_inode_t *dp, /* directory within whose allocate
  758. the inode. */
  759. umode_t mode,
  760. xfs_nlink_t nlink,
  761. xfs_dev_t rdev,
  762. prid_t prid, /* project id */
  763. int okalloc, /* ok to allocate new space */
  764. xfs_inode_t **ipp, /* pointer to inode; it will be
  765. locked. */
  766. int *committed)
  767. {
  768. xfs_trans_t *tp;
  769. xfs_trans_t *ntp;
  770. xfs_inode_t *ip;
  771. xfs_buf_t *ialloc_context = NULL;
  772. int code;
  773. void *dqinfo;
  774. uint tflags;
  775. tp = *tpp;
  776. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  777. /*
  778. * xfs_ialloc will return a pointer to an incore inode if
  779. * the Space Manager has an available inode on the free
  780. * list. Otherwise, it will do an allocation and replenish
  781. * the freelist. Since we can only do one allocation per
  782. * transaction without deadlocks, we will need to commit the
  783. * current transaction and start a new one. We will then
  784. * need to call xfs_ialloc again to get the inode.
  785. *
  786. * If xfs_ialloc did an allocation to replenish the freelist,
  787. * it returns the bp containing the head of the freelist as
  788. * ialloc_context. We will hold a lock on it across the
  789. * transaction commit so that no other process can steal
  790. * the inode(s) that we've just allocated.
  791. */
  792. code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid, okalloc,
  793. &ialloc_context, &ip);
  794. /*
  795. * Return an error if we were unable to allocate a new inode.
  796. * This should only happen if we run out of space on disk or
  797. * encounter a disk error.
  798. */
  799. if (code) {
  800. *ipp = NULL;
  801. return code;
  802. }
  803. if (!ialloc_context && !ip) {
  804. *ipp = NULL;
  805. return XFS_ERROR(ENOSPC);
  806. }
  807. /*
  808. * If the AGI buffer is non-NULL, then we were unable to get an
  809. * inode in one operation. We need to commit the current
  810. * transaction and call xfs_ialloc() again. It is guaranteed
  811. * to succeed the second time.
  812. */
  813. if (ialloc_context) {
  814. struct xfs_trans_res tres;
  815. /*
  816. * Normally, xfs_trans_commit releases all the locks.
  817. * We call bhold to hang on to the ialloc_context across
  818. * the commit. Holding this buffer prevents any other
  819. * processes from doing any allocations in this
  820. * allocation group.
  821. */
  822. xfs_trans_bhold(tp, ialloc_context);
  823. /*
  824. * Save the log reservation so we can use
  825. * them in the next transaction.
  826. */
  827. tres.tr_logres = xfs_trans_get_log_res(tp);
  828. tres.tr_logcount = xfs_trans_get_log_count(tp);
  829. /*
  830. * We want the quota changes to be associated with the next
  831. * transaction, NOT this one. So, detach the dqinfo from this
  832. * and attach it to the next transaction.
  833. */
  834. dqinfo = NULL;
  835. tflags = 0;
  836. if (tp->t_dqinfo) {
  837. dqinfo = (void *)tp->t_dqinfo;
  838. tp->t_dqinfo = NULL;
  839. tflags = tp->t_flags & XFS_TRANS_DQ_DIRTY;
  840. tp->t_flags &= ~(XFS_TRANS_DQ_DIRTY);
  841. }
  842. ntp = xfs_trans_dup(tp);
  843. code = xfs_trans_commit(tp, 0);
  844. tp = ntp;
  845. if (committed != NULL) {
  846. *committed = 1;
  847. }
  848. /*
  849. * If we get an error during the commit processing,
  850. * release the buffer that is still held and return
  851. * to the caller.
  852. */
  853. if (code) {
  854. xfs_buf_relse(ialloc_context);
  855. if (dqinfo) {
  856. tp->t_dqinfo = dqinfo;
  857. xfs_trans_free_dqinfo(tp);
  858. }
  859. *tpp = ntp;
  860. *ipp = NULL;
  861. return code;
  862. }
  863. /*
  864. * transaction commit worked ok so we can drop the extra ticket
  865. * reference that we gained in xfs_trans_dup()
  866. */
  867. xfs_log_ticket_put(tp->t_ticket);
  868. tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
  869. code = xfs_trans_reserve(tp, &tres, 0, 0);
  870. /*
  871. * Re-attach the quota info that we detached from prev trx.
  872. */
  873. if (dqinfo) {
  874. tp->t_dqinfo = dqinfo;
  875. tp->t_flags |= tflags;
  876. }
  877. if (code) {
  878. xfs_buf_relse(ialloc_context);
  879. *tpp = ntp;
  880. *ipp = NULL;
  881. return code;
  882. }
  883. xfs_trans_bjoin(tp, ialloc_context);
  884. /*
  885. * Call ialloc again. Since we've locked out all
  886. * other allocations in this allocation group,
  887. * this call should always succeed.
  888. */
  889. code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid,
  890. okalloc, &ialloc_context, &ip);
  891. /*
  892. * If we get an error at this point, return to the caller
  893. * so that the current transaction can be aborted.
  894. */
  895. if (code) {
  896. *tpp = tp;
  897. *ipp = NULL;
  898. return code;
  899. }
  900. ASSERT(!ialloc_context && ip);
  901. } else {
  902. if (committed != NULL)
  903. *committed = 0;
  904. }
  905. *ipp = ip;
  906. *tpp = tp;
  907. return 0;
  908. }
  909. /*
  910. * Decrement the link count on an inode & log the change.
  911. * If this causes the link count to go to zero, initiate the
  912. * logging activity required to truncate a file.
  913. */
  914. int /* error */
  915. xfs_droplink(
  916. xfs_trans_t *tp,
  917. xfs_inode_t *ip)
  918. {
  919. int error;
  920. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
  921. ASSERT (ip->i_d.di_nlink > 0);
  922. ip->i_d.di_nlink--;
  923. drop_nlink(VFS_I(ip));
  924. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  925. error = 0;
  926. if (ip->i_d.di_nlink == 0) {
  927. /*
  928. * We're dropping the last link to this file.
  929. * Move the on-disk inode to the AGI unlinked list.
  930. * From xfs_inactive() we will pull the inode from
  931. * the list and free it.
  932. */
  933. error = xfs_iunlink(tp, ip);
  934. }
  935. return error;
  936. }
  937. /*
  938. * Increment the link count on an inode & log the change.
  939. */
  940. int
  941. xfs_bumplink(
  942. xfs_trans_t *tp,
  943. xfs_inode_t *ip)
  944. {
  945. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
  946. ASSERT(ip->i_d.di_version > 1);
  947. ASSERT(ip->i_d.di_nlink > 0 || (VFS_I(ip)->i_state & I_LINKABLE));
  948. ip->i_d.di_nlink++;
  949. inc_nlink(VFS_I(ip));
  950. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  951. return 0;
  952. }
  953. int
  954. xfs_create(
  955. xfs_inode_t *dp,
  956. struct xfs_name *name,
  957. umode_t mode,
  958. xfs_dev_t rdev,
  959. xfs_inode_t **ipp)
  960. {
  961. int is_dir = S_ISDIR(mode);
  962. struct xfs_mount *mp = dp->i_mount;
  963. struct xfs_inode *ip = NULL;
  964. struct xfs_trans *tp = NULL;
  965. int error;
  966. xfs_bmap_free_t free_list;
  967. xfs_fsblock_t first_block;
  968. bool unlock_dp_on_error = false;
  969. uint cancel_flags;
  970. int committed;
  971. prid_t prid;
  972. struct xfs_dquot *udqp = NULL;
  973. struct xfs_dquot *gdqp = NULL;
  974. struct xfs_dquot *pdqp = NULL;
  975. struct xfs_trans_res tres;
  976. uint resblks;
  977. trace_xfs_create(dp, name);
  978. if (XFS_FORCED_SHUTDOWN(mp))
  979. return XFS_ERROR(EIO);
  980. prid = xfs_get_initial_prid(dp);
  981. /*
  982. * Make sure that we have allocated dquot(s) on disk.
  983. */
  984. error = xfs_qm_vop_dqalloc(dp, xfs_kuid_to_uid(current_fsuid()),
  985. xfs_kgid_to_gid(current_fsgid()), prid,
  986. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
  987. &udqp, &gdqp, &pdqp);
  988. if (error)
  989. return error;
  990. if (is_dir) {
  991. rdev = 0;
  992. resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
  993. tres.tr_logres = M_RES(mp)->tr_mkdir.tr_logres;
  994. tres.tr_logcount = XFS_MKDIR_LOG_COUNT;
  995. tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
  996. } else {
  997. resblks = XFS_CREATE_SPACE_RES(mp, name->len);
  998. tres.tr_logres = M_RES(mp)->tr_create.tr_logres;
  999. tres.tr_logcount = XFS_CREATE_LOG_COUNT;
  1000. tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
  1001. }
  1002. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  1003. /*
  1004. * Initially assume that the file does not exist and
  1005. * reserve the resources for that case. If that is not
  1006. * the case we'll drop the one we have and get a more
  1007. * appropriate transaction later.
  1008. */
  1009. tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
  1010. error = xfs_trans_reserve(tp, &tres, resblks, 0);
  1011. if (error == ENOSPC) {
  1012. /* flush outstanding delalloc blocks and retry */
  1013. xfs_flush_inodes(mp);
  1014. error = xfs_trans_reserve(tp, &tres, resblks, 0);
  1015. }
  1016. if (error == ENOSPC) {
  1017. /* No space at all so try a "no-allocation" reservation */
  1018. resblks = 0;
  1019. error = xfs_trans_reserve(tp, &tres, 0, 0);
  1020. }
  1021. if (error) {
  1022. cancel_flags = 0;
  1023. goto out_trans_cancel;
  1024. }
  1025. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  1026. unlock_dp_on_error = true;
  1027. xfs_bmap_init(&free_list, &first_block);
  1028. /*
  1029. * Reserve disk quota and the inode.
  1030. */
  1031. error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
  1032. pdqp, resblks, 1, 0);
  1033. if (error)
  1034. goto out_trans_cancel;
  1035. error = xfs_dir_canenter(tp, dp, name, resblks);
  1036. if (error)
  1037. goto out_trans_cancel;
  1038. /*
  1039. * A newly created regular or special file just has one directory
  1040. * entry pointing to them, but a directory also the "." entry
  1041. * pointing to itself.
  1042. */
  1043. error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev,
  1044. prid, resblks > 0, &ip, &committed);
  1045. if (error) {
  1046. if (error == ENOSPC)
  1047. goto out_trans_cancel;
  1048. goto out_trans_abort;
  1049. }
  1050. /*
  1051. * Now we join the directory inode to the transaction. We do not do it
  1052. * earlier because xfs_dir_ialloc might commit the previous transaction
  1053. * (and release all the locks). An error from here on will result in
  1054. * the transaction cancel unlocking dp so don't do it explicitly in the
  1055. * error path.
  1056. */
  1057. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  1058. unlock_dp_on_error = false;
  1059. error = xfs_dir_createname(tp, dp, name, ip->i_ino,
  1060. &first_block, &free_list, resblks ?
  1061. resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
  1062. if (error) {
  1063. ASSERT(error != ENOSPC);
  1064. goto out_trans_abort;
  1065. }
  1066. xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1067. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  1068. if (is_dir) {
  1069. error = xfs_dir_init(tp, ip, dp);
  1070. if (error)
  1071. goto out_bmap_cancel;
  1072. error = xfs_bumplink(tp, dp);
  1073. if (error)
  1074. goto out_bmap_cancel;
  1075. }
  1076. /*
  1077. * If this is a synchronous mount, make sure that the
  1078. * create transaction goes to disk before returning to
  1079. * the user.
  1080. */
  1081. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
  1082. xfs_trans_set_sync(tp);
  1083. /*
  1084. * Attach the dquot(s) to the inodes and modify them incore.
  1085. * These ids of the inode couldn't have changed since the new
  1086. * inode has been locked ever since it was created.
  1087. */
  1088. xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
  1089. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1090. if (error)
  1091. goto out_bmap_cancel;
  1092. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1093. if (error)
  1094. goto out_release_inode;
  1095. xfs_qm_dqrele(udqp);
  1096. xfs_qm_dqrele(gdqp);
  1097. xfs_qm_dqrele(pdqp);
  1098. *ipp = ip;
  1099. return 0;
  1100. out_bmap_cancel:
  1101. xfs_bmap_cancel(&free_list);
  1102. out_trans_abort:
  1103. cancel_flags |= XFS_TRANS_ABORT;
  1104. out_trans_cancel:
  1105. xfs_trans_cancel(tp, cancel_flags);
  1106. out_release_inode:
  1107. /*
  1108. * Wait until after the current transaction is aborted to
  1109. * release the inode. This prevents recursive transactions
  1110. * and deadlocks from xfs_inactive.
  1111. */
  1112. if (ip)
  1113. IRELE(ip);
  1114. xfs_qm_dqrele(udqp);
  1115. xfs_qm_dqrele(gdqp);
  1116. xfs_qm_dqrele(pdqp);
  1117. if (unlock_dp_on_error)
  1118. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  1119. return error;
  1120. }
  1121. int
  1122. xfs_create_tmpfile(
  1123. struct xfs_inode *dp,
  1124. struct dentry *dentry,
  1125. umode_t mode,
  1126. struct xfs_inode **ipp)
  1127. {
  1128. struct xfs_mount *mp = dp->i_mount;
  1129. struct xfs_inode *ip = NULL;
  1130. struct xfs_trans *tp = NULL;
  1131. int error;
  1132. uint cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  1133. prid_t prid;
  1134. struct xfs_dquot *udqp = NULL;
  1135. struct xfs_dquot *gdqp = NULL;
  1136. struct xfs_dquot *pdqp = NULL;
  1137. struct xfs_trans_res *tres;
  1138. uint resblks;
  1139. if (XFS_FORCED_SHUTDOWN(mp))
  1140. return XFS_ERROR(EIO);
  1141. prid = xfs_get_initial_prid(dp);
  1142. /*
  1143. * Make sure that we have allocated dquot(s) on disk.
  1144. */
  1145. error = xfs_qm_vop_dqalloc(dp, xfs_kuid_to_uid(current_fsuid()),
  1146. xfs_kgid_to_gid(current_fsgid()), prid,
  1147. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
  1148. &udqp, &gdqp, &pdqp);
  1149. if (error)
  1150. return error;
  1151. resblks = XFS_IALLOC_SPACE_RES(mp);
  1152. tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE_TMPFILE);
  1153. tres = &M_RES(mp)->tr_create_tmpfile;
  1154. error = xfs_trans_reserve(tp, tres, resblks, 0);
  1155. if (error == ENOSPC) {
  1156. /* No space at all so try a "no-allocation" reservation */
  1157. resblks = 0;
  1158. error = xfs_trans_reserve(tp, tres, 0, 0);
  1159. }
  1160. if (error) {
  1161. cancel_flags = 0;
  1162. goto out_trans_cancel;
  1163. }
  1164. error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
  1165. pdqp, resblks, 1, 0);
  1166. if (error)
  1167. goto out_trans_cancel;
  1168. error = xfs_dir_ialloc(&tp, dp, mode, 1, 0,
  1169. prid, resblks > 0, &ip, NULL);
  1170. if (error) {
  1171. if (error == ENOSPC)
  1172. goto out_trans_cancel;
  1173. goto out_trans_abort;
  1174. }
  1175. if (mp->m_flags & XFS_MOUNT_WSYNC)
  1176. xfs_trans_set_sync(tp);
  1177. /*
  1178. * Attach the dquot(s) to the inodes and modify them incore.
  1179. * These ids of the inode couldn't have changed since the new
  1180. * inode has been locked ever since it was created.
  1181. */
  1182. xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
  1183. ip->i_d.di_nlink--;
  1184. error = xfs_iunlink(tp, ip);
  1185. if (error)
  1186. goto out_trans_abort;
  1187. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1188. if (error)
  1189. goto out_release_inode;
  1190. xfs_qm_dqrele(udqp);
  1191. xfs_qm_dqrele(gdqp);
  1192. xfs_qm_dqrele(pdqp);
  1193. *ipp = ip;
  1194. return 0;
  1195. out_trans_abort:
  1196. cancel_flags |= XFS_TRANS_ABORT;
  1197. out_trans_cancel:
  1198. xfs_trans_cancel(tp, cancel_flags);
  1199. out_release_inode:
  1200. /*
  1201. * Wait until after the current transaction is aborted to
  1202. * release the inode. This prevents recursive transactions
  1203. * and deadlocks from xfs_inactive.
  1204. */
  1205. if (ip)
  1206. IRELE(ip);
  1207. xfs_qm_dqrele(udqp);
  1208. xfs_qm_dqrele(gdqp);
  1209. xfs_qm_dqrele(pdqp);
  1210. return error;
  1211. }
  1212. int
  1213. xfs_link(
  1214. xfs_inode_t *tdp,
  1215. xfs_inode_t *sip,
  1216. struct xfs_name *target_name)
  1217. {
  1218. xfs_mount_t *mp = tdp->i_mount;
  1219. xfs_trans_t *tp;
  1220. int error;
  1221. xfs_bmap_free_t free_list;
  1222. xfs_fsblock_t first_block;
  1223. int cancel_flags;
  1224. int committed;
  1225. int resblks;
  1226. trace_xfs_link(tdp, target_name);
  1227. ASSERT(!S_ISDIR(sip->i_d.di_mode));
  1228. if (XFS_FORCED_SHUTDOWN(mp))
  1229. return XFS_ERROR(EIO);
  1230. error = xfs_qm_dqattach(sip, 0);
  1231. if (error)
  1232. goto std_return;
  1233. error = xfs_qm_dqattach(tdp, 0);
  1234. if (error)
  1235. goto std_return;
  1236. tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
  1237. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  1238. resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
  1239. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_link, resblks, 0);
  1240. if (error == ENOSPC) {
  1241. resblks = 0;
  1242. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_link, 0, 0);
  1243. }
  1244. if (error) {
  1245. cancel_flags = 0;
  1246. goto error_return;
  1247. }
  1248. xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
  1249. xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
  1250. xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
  1251. /*
  1252. * If we are using project inheritance, we only allow hard link
  1253. * creation in our tree when the project IDs are the same; else
  1254. * the tree quota mechanism could be circumvented.
  1255. */
  1256. if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
  1257. (xfs_get_projid(tdp) != xfs_get_projid(sip)))) {
  1258. error = XFS_ERROR(EXDEV);
  1259. goto error_return;
  1260. }
  1261. error = xfs_dir_canenter(tp, tdp, target_name, resblks);
  1262. if (error)
  1263. goto error_return;
  1264. xfs_bmap_init(&free_list, &first_block);
  1265. if (sip->i_d.di_nlink == 0) {
  1266. error = xfs_iunlink_remove(tp, sip);
  1267. if (error)
  1268. goto abort_return;
  1269. }
  1270. error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
  1271. &first_block, &free_list, resblks);
  1272. if (error)
  1273. goto abort_return;
  1274. xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1275. xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
  1276. error = xfs_bumplink(tp, sip);
  1277. if (error)
  1278. goto abort_return;
  1279. /*
  1280. * If this is a synchronous mount, make sure that the
  1281. * link transaction goes to disk before returning to
  1282. * the user.
  1283. */
  1284. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  1285. xfs_trans_set_sync(tp);
  1286. }
  1287. error = xfs_bmap_finish (&tp, &free_list, &committed);
  1288. if (error) {
  1289. xfs_bmap_cancel(&free_list);
  1290. goto abort_return;
  1291. }
  1292. return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1293. abort_return:
  1294. cancel_flags |= XFS_TRANS_ABORT;
  1295. error_return:
  1296. xfs_trans_cancel(tp, cancel_flags);
  1297. std_return:
  1298. return error;
  1299. }
  1300. /*
  1301. * Free up the underlying blocks past new_size. The new size must be smaller
  1302. * than the current size. This routine can be used both for the attribute and
  1303. * data fork, and does not modify the inode size, which is left to the caller.
  1304. *
  1305. * The transaction passed to this routine must have made a permanent log
  1306. * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the
  1307. * given transaction and start new ones, so make sure everything involved in
  1308. * the transaction is tidy before calling here. Some transaction will be
  1309. * returned to the caller to be committed. The incoming transaction must
  1310. * already include the inode, and both inode locks must be held exclusively.
  1311. * The inode must also be "held" within the transaction. On return the inode
  1312. * will be "held" within the returned transaction. This routine does NOT
  1313. * require any disk space to be reserved for it within the transaction.
  1314. *
  1315. * If we get an error, we must return with the inode locked and linked into the
  1316. * current transaction. This keeps things simple for the higher level code,
  1317. * because it always knows that the inode is locked and held in the transaction
  1318. * that returns to it whether errors occur or not. We don't mark the inode
  1319. * dirty on error so that transactions can be easily aborted if possible.
  1320. */
  1321. int
  1322. xfs_itruncate_extents(
  1323. struct xfs_trans **tpp,
  1324. struct xfs_inode *ip,
  1325. int whichfork,
  1326. xfs_fsize_t new_size)
  1327. {
  1328. struct xfs_mount *mp = ip->i_mount;
  1329. struct xfs_trans *tp = *tpp;
  1330. struct xfs_trans *ntp;
  1331. xfs_bmap_free_t free_list;
  1332. xfs_fsblock_t first_block;
  1333. xfs_fileoff_t first_unmap_block;
  1334. xfs_fileoff_t last_block;
  1335. xfs_filblks_t unmap_len;
  1336. int committed;
  1337. int error = 0;
  1338. int done = 0;
  1339. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  1340. ASSERT(!atomic_read(&VFS_I(ip)->i_count) ||
  1341. xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  1342. ASSERT(new_size <= XFS_ISIZE(ip));
  1343. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  1344. ASSERT(ip->i_itemp != NULL);
  1345. ASSERT(ip->i_itemp->ili_lock_flags == 0);
  1346. ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
  1347. trace_xfs_itruncate_extents_start(ip, new_size);
  1348. /*
  1349. * Since it is possible for space to become allocated beyond
  1350. * the end of the file (in a crash where the space is allocated
  1351. * but the inode size is not yet updated), simply remove any
  1352. * blocks which show up between the new EOF and the maximum
  1353. * possible file size. If the first block to be removed is
  1354. * beyond the maximum file size (ie it is the same as last_block),
  1355. * then there is nothing to do.
  1356. */
  1357. first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
  1358. last_block = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
  1359. if (first_unmap_block == last_block)
  1360. return 0;
  1361. ASSERT(first_unmap_block < last_block);
  1362. unmap_len = last_block - first_unmap_block + 1;
  1363. while (!done) {
  1364. xfs_bmap_init(&free_list, &first_block);
  1365. error = xfs_bunmapi(tp, ip,
  1366. first_unmap_block, unmap_len,
  1367. xfs_bmapi_aflag(whichfork),
  1368. XFS_ITRUNC_MAX_EXTENTS,
  1369. &first_block, &free_list,
  1370. &done);
  1371. if (error)
  1372. goto out_bmap_cancel;
  1373. /*
  1374. * Duplicate the transaction that has the permanent
  1375. * reservation and commit the old transaction.
  1376. */
  1377. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1378. if (committed)
  1379. xfs_trans_ijoin(tp, ip, 0);
  1380. if (error)
  1381. goto out_bmap_cancel;
  1382. if (committed) {
  1383. /*
  1384. * Mark the inode dirty so it will be logged and
  1385. * moved forward in the log as part of every commit.
  1386. */
  1387. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1388. }
  1389. ntp = xfs_trans_dup(tp);
  1390. error = xfs_trans_commit(tp, 0);
  1391. tp = ntp;
  1392. xfs_trans_ijoin(tp, ip, 0);
  1393. if (error)
  1394. goto out;
  1395. /*
  1396. * Transaction commit worked ok so we can drop the extra ticket
  1397. * reference that we gained in xfs_trans_dup()
  1398. */
  1399. xfs_log_ticket_put(tp->t_ticket);
  1400. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
  1401. if (error)
  1402. goto out;
  1403. }
  1404. /*
  1405. * Always re-log the inode so that our permanent transaction can keep
  1406. * on rolling it forward in the log.
  1407. */
  1408. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1409. trace_xfs_itruncate_extents_end(ip, new_size);
  1410. out:
  1411. *tpp = tp;
  1412. return error;
  1413. out_bmap_cancel:
  1414. /*
  1415. * If the bunmapi call encounters an error, return to the caller where
  1416. * the transaction can be properly aborted. We just need to make sure
  1417. * we're not holding any resources that we were not when we came in.
  1418. */
  1419. xfs_bmap_cancel(&free_list);
  1420. goto out;
  1421. }
  1422. int
  1423. xfs_release(
  1424. xfs_inode_t *ip)
  1425. {
  1426. xfs_mount_t *mp = ip->i_mount;
  1427. int error;
  1428. if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
  1429. return 0;
  1430. /* If this is a read-only mount, don't do this (would generate I/O) */
  1431. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1432. return 0;
  1433. if (!XFS_FORCED_SHUTDOWN(mp)) {
  1434. int truncated;
  1435. /*
  1436. * If we previously truncated this file and removed old data
  1437. * in the process, we want to initiate "early" writeout on
  1438. * the last close. This is an attempt to combat the notorious
  1439. * NULL files problem which is particularly noticeable from a
  1440. * truncate down, buffered (re-)write (delalloc), followed by
  1441. * a crash. What we are effectively doing here is
  1442. * significantly reducing the time window where we'd otherwise
  1443. * be exposed to that problem.
  1444. */
  1445. truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
  1446. if (truncated) {
  1447. xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE);
  1448. if (VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0) {
  1449. error = -filemap_flush(VFS_I(ip)->i_mapping);
  1450. if (error)
  1451. return error;
  1452. }
  1453. }
  1454. }
  1455. if (ip->i_d.di_nlink == 0)
  1456. return 0;
  1457. if (xfs_can_free_eofblocks(ip, false)) {
  1458. /*
  1459. * If we can't get the iolock just skip truncating the blocks
  1460. * past EOF because we could deadlock with the mmap_sem
  1461. * otherwise. We'll get another chance to drop them once the
  1462. * last reference to the inode is dropped, so we'll never leak
  1463. * blocks permanently.
  1464. *
  1465. * Further, check if the inode is being opened, written and
  1466. * closed frequently and we have delayed allocation blocks
  1467. * outstanding (e.g. streaming writes from the NFS server),
  1468. * truncating the blocks past EOF will cause fragmentation to
  1469. * occur.
  1470. *
  1471. * In this case don't do the truncation, either, but we have to
  1472. * be careful how we detect this case. Blocks beyond EOF show
  1473. * up as i_delayed_blks even when the inode is clean, so we
  1474. * need to truncate them away first before checking for a dirty
  1475. * release. Hence on the first dirty close we will still remove
  1476. * the speculative allocation, but after that we will leave it
  1477. * in place.
  1478. */
  1479. if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE))
  1480. return 0;
  1481. error = xfs_free_eofblocks(mp, ip, true);
  1482. if (error && error != EAGAIN)
  1483. return error;
  1484. /* delalloc blocks after truncation means it really is dirty */
  1485. if (ip->i_delayed_blks)
  1486. xfs_iflags_set(ip, XFS_IDIRTY_RELEASE);
  1487. }
  1488. return 0;
  1489. }
  1490. /*
  1491. * xfs_inactive_truncate
  1492. *
  1493. * Called to perform a truncate when an inode becomes unlinked.
  1494. */
  1495. STATIC int
  1496. xfs_inactive_truncate(
  1497. struct xfs_inode *ip)
  1498. {
  1499. struct xfs_mount *mp = ip->i_mount;
  1500. struct xfs_trans *tp;
  1501. int error;
  1502. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  1503. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
  1504. if (error) {
  1505. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1506. xfs_trans_cancel(tp, 0);
  1507. return error;
  1508. }
  1509. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1510. xfs_trans_ijoin(tp, ip, 0);
  1511. /*
  1512. * Log the inode size first to prevent stale data exposure in the event
  1513. * of a system crash before the truncate completes. See the related
  1514. * comment in xfs_setattr_size() for details.
  1515. */
  1516. ip->i_d.di_size = 0;
  1517. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1518. error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
  1519. if (error)
  1520. goto error_trans_cancel;
  1521. ASSERT(ip->i_d.di_nextents == 0);
  1522. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1523. if (error)
  1524. goto error_unlock;
  1525. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1526. return 0;
  1527. error_trans_cancel:
  1528. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  1529. error_unlock:
  1530. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1531. return error;
  1532. }
  1533. /*
  1534. * xfs_inactive_ifree()
  1535. *
  1536. * Perform the inode free when an inode is unlinked.
  1537. */
  1538. STATIC int
  1539. xfs_inactive_ifree(
  1540. struct xfs_inode *ip)
  1541. {
  1542. xfs_bmap_free_t free_list;
  1543. xfs_fsblock_t first_block;
  1544. int committed;
  1545. struct xfs_mount *mp = ip->i_mount;
  1546. struct xfs_trans *tp;
  1547. int error;
  1548. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  1549. /*
  1550. * The ifree transaction might need to allocate blocks for record
  1551. * insertion to the finobt. We don't want to fail here at ENOSPC, so
  1552. * allow ifree to dip into the reserved block pool if necessary.
  1553. *
  1554. * Freeing large sets of inodes generally means freeing inode chunks,
  1555. * directory and file data blocks, so this should be relatively safe.
  1556. * Only under severe circumstances should it be possible to free enough
  1557. * inodes to exhaust the reserve block pool via finobt expansion while
  1558. * at the same time not creating free space in the filesystem.
  1559. *
  1560. * Send a warning if the reservation does happen to fail, as the inode
  1561. * now remains allocated and sits on the unlinked list until the fs is
  1562. * repaired.
  1563. */
  1564. tp->t_flags |= XFS_TRANS_RESERVE;
  1565. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_ifree,
  1566. XFS_IFREE_SPACE_RES(mp), 0);
  1567. if (error) {
  1568. if (error == ENOSPC) {
  1569. xfs_warn_ratelimited(mp,
  1570. "Failed to remove inode(s) from unlinked list. "
  1571. "Please free space, unmount and run xfs_repair.");
  1572. } else {
  1573. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1574. }
  1575. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
  1576. return error;
  1577. }
  1578. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1579. xfs_trans_ijoin(tp, ip, 0);
  1580. xfs_bmap_init(&free_list, &first_block);
  1581. error = xfs_ifree(tp, ip, &free_list);
  1582. if (error) {
  1583. /*
  1584. * If we fail to free the inode, shut down. The cancel
  1585. * might do that, we need to make sure. Otherwise the
  1586. * inode might be lost for a long time or forever.
  1587. */
  1588. if (!XFS_FORCED_SHUTDOWN(mp)) {
  1589. xfs_notice(mp, "%s: xfs_ifree returned error %d",
  1590. __func__, error);
  1591. xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
  1592. }
  1593. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  1594. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1595. return error;
  1596. }
  1597. /*
  1598. * Credit the quota account(s). The inode is gone.
  1599. */
  1600. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
  1601. /*
  1602. * Just ignore errors at this point. There is nothing we can
  1603. * do except to try to keep going. Make sure it's not a silent
  1604. * error.
  1605. */
  1606. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1607. if (error)
  1608. xfs_notice(mp, "%s: xfs_bmap_finish returned error %d",
  1609. __func__, error);
  1610. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1611. if (error)
  1612. xfs_notice(mp, "%s: xfs_trans_commit returned error %d",
  1613. __func__, error);
  1614. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1615. return 0;
  1616. }
  1617. /*
  1618. * xfs_inactive
  1619. *
  1620. * This is called when the vnode reference count for the vnode
  1621. * goes to zero. If the file has been unlinked, then it must
  1622. * now be truncated. Also, we clear all of the read-ahead state
  1623. * kept for the inode here since the file is now closed.
  1624. */
  1625. void
  1626. xfs_inactive(
  1627. xfs_inode_t *ip)
  1628. {
  1629. struct xfs_mount *mp;
  1630. int error;
  1631. int truncate = 0;
  1632. /*
  1633. * If the inode is already free, then there can be nothing
  1634. * to clean up here.
  1635. */
  1636. if (ip->i_d.di_mode == 0) {
  1637. ASSERT(ip->i_df.if_real_bytes == 0);
  1638. ASSERT(ip->i_df.if_broot_bytes == 0);
  1639. return;
  1640. }
  1641. mp = ip->i_mount;
  1642. /* If this is a read-only mount, don't do this (would generate I/O) */
  1643. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1644. return;
  1645. if (ip->i_d.di_nlink != 0) {
  1646. /*
  1647. * force is true because we are evicting an inode from the
  1648. * cache. Post-eof blocks must be freed, lest we end up with
  1649. * broken free space accounting.
  1650. */
  1651. if (xfs_can_free_eofblocks(ip, true))
  1652. xfs_free_eofblocks(mp, ip, false);
  1653. return;
  1654. }
  1655. if (S_ISREG(ip->i_d.di_mode) &&
  1656. (ip->i_d.di_size != 0 || XFS_ISIZE(ip) != 0 ||
  1657. ip->i_d.di_nextents > 0 || ip->i_delayed_blks > 0))
  1658. truncate = 1;
  1659. error = xfs_qm_dqattach(ip, 0);
  1660. if (error)
  1661. return;
  1662. if (S_ISLNK(ip->i_d.di_mode))
  1663. error = xfs_inactive_symlink(ip);
  1664. else if (truncate)
  1665. error = xfs_inactive_truncate(ip);
  1666. if (error)
  1667. return;
  1668. /*
  1669. * If there are attributes associated with the file then blow them away
  1670. * now. The code calls a routine that recursively deconstructs the
  1671. * attribute fork. We need to just commit the current transaction
  1672. * because we can't use it for xfs_attr_inactive().
  1673. */
  1674. if (ip->i_d.di_anextents > 0) {
  1675. ASSERT(ip->i_d.di_forkoff != 0);
  1676. error = xfs_attr_inactive(ip);
  1677. if (error)
  1678. return;
  1679. }
  1680. if (ip->i_afp)
  1681. xfs_idestroy_fork(ip, XFS_ATTR_FORK);
  1682. ASSERT(ip->i_d.di_anextents == 0);
  1683. /*
  1684. * Free the inode.
  1685. */
  1686. error = xfs_inactive_ifree(ip);
  1687. if (error)
  1688. return;
  1689. /*
  1690. * Release the dquots held by inode, if any.
  1691. */
  1692. xfs_qm_dqdetach(ip);
  1693. }
  1694. /*
  1695. * This is called when the inode's link count goes to 0.
  1696. * We place the on-disk inode on a list in the AGI. It
  1697. * will be pulled from this list when the inode is freed.
  1698. */
  1699. int
  1700. xfs_iunlink(
  1701. xfs_trans_t *tp,
  1702. xfs_inode_t *ip)
  1703. {
  1704. xfs_mount_t *mp;
  1705. xfs_agi_t *agi;
  1706. xfs_dinode_t *dip;
  1707. xfs_buf_t *agibp;
  1708. xfs_buf_t *ibp;
  1709. xfs_agino_t agino;
  1710. short bucket_index;
  1711. int offset;
  1712. int error;
  1713. ASSERT(ip->i_d.di_nlink == 0);
  1714. ASSERT(ip->i_d.di_mode != 0);
  1715. mp = tp->t_mountp;
  1716. /*
  1717. * Get the agi buffer first. It ensures lock ordering
  1718. * on the list.
  1719. */
  1720. error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp);
  1721. if (error)
  1722. return error;
  1723. agi = XFS_BUF_TO_AGI(agibp);
  1724. /*
  1725. * Get the index into the agi hash table for the
  1726. * list this inode will go on.
  1727. */
  1728. agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
  1729. ASSERT(agino != 0);
  1730. bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
  1731. ASSERT(agi->agi_unlinked[bucket_index]);
  1732. ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
  1733. if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) {
  1734. /*
  1735. * There is already another inode in the bucket we need
  1736. * to add ourselves to. Add us at the front of the list.
  1737. * Here we put the head pointer into our next pointer,
  1738. * and then we fall through to point the head at us.
  1739. */
  1740. error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
  1741. 0, 0);
  1742. if (error)
  1743. return error;
  1744. ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO));
  1745. dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
  1746. offset = ip->i_imap.im_boffset +
  1747. offsetof(xfs_dinode_t, di_next_unlinked);
  1748. /* need to recalc the inode CRC if appropriate */
  1749. xfs_dinode_calc_crc(mp, dip);
  1750. xfs_trans_inode_buf(tp, ibp);
  1751. xfs_trans_log_buf(tp, ibp, offset,
  1752. (offset + sizeof(xfs_agino_t) - 1));
  1753. xfs_inobp_check(mp, ibp);
  1754. }
  1755. /*
  1756. * Point the bucket head pointer at the inode being inserted.
  1757. */
  1758. ASSERT(agino != 0);
  1759. agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
  1760. offset = offsetof(xfs_agi_t, agi_unlinked) +
  1761. (sizeof(xfs_agino_t) * bucket_index);
  1762. xfs_trans_log_buf(tp, agibp, offset,
  1763. (offset + sizeof(xfs_agino_t) - 1));
  1764. return 0;
  1765. }
  1766. /*
  1767. * Pull the on-disk inode from the AGI unlinked list.
  1768. */
  1769. STATIC int
  1770. xfs_iunlink_remove(
  1771. xfs_trans_t *tp,
  1772. xfs_inode_t *ip)
  1773. {
  1774. xfs_ino_t next_ino;
  1775. xfs_mount_t *mp;
  1776. xfs_agi_t *agi;
  1777. xfs_dinode_t *dip;
  1778. xfs_buf_t *agibp;
  1779. xfs_buf_t *ibp;
  1780. xfs_agnumber_t agno;
  1781. xfs_agino_t agino;
  1782. xfs_agino_t next_agino;
  1783. xfs_buf_t *last_ibp;
  1784. xfs_dinode_t *last_dip = NULL;
  1785. short bucket_index;
  1786. int offset, last_offset = 0;
  1787. int error;
  1788. mp = tp->t_mountp;
  1789. agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
  1790. /*
  1791. * Get the agi buffer first. It ensures lock ordering
  1792. * on the list.
  1793. */
  1794. error = xfs_read_agi(mp, tp, agno, &agibp);
  1795. if (error)
  1796. return error;
  1797. agi = XFS_BUF_TO_AGI(agibp);
  1798. /*
  1799. * Get the index into the agi hash table for the
  1800. * list this inode will go on.
  1801. */
  1802. agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
  1803. ASSERT(agino != 0);
  1804. bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
  1805. ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO));
  1806. ASSERT(agi->agi_unlinked[bucket_index]);
  1807. if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
  1808. /*
  1809. * We're at the head of the list. Get the inode's on-disk
  1810. * buffer to see if there is anyone after us on the list.
  1811. * Only modify our next pointer if it is not already NULLAGINO.
  1812. * This saves us the overhead of dealing with the buffer when
  1813. * there is no need to change it.
  1814. */
  1815. error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
  1816. 0, 0);
  1817. if (error) {
  1818. xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
  1819. __func__, error);
  1820. return error;
  1821. }
  1822. next_agino = be32_to_cpu(dip->di_next_unlinked);
  1823. ASSERT(next_agino != 0);
  1824. if (next_agino != NULLAGINO) {
  1825. dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
  1826. offset = ip->i_imap.im_boffset +
  1827. offsetof(xfs_dinode_t, di_next_unlinked);
  1828. /* need to recalc the inode CRC if appropriate */
  1829. xfs_dinode_calc_crc(mp, dip);
  1830. xfs_trans_inode_buf(tp, ibp);
  1831. xfs_trans_log_buf(tp, ibp, offset,
  1832. (offset + sizeof(xfs_agino_t) - 1));
  1833. xfs_inobp_check(mp, ibp);
  1834. } else {
  1835. xfs_trans_brelse(tp, ibp);
  1836. }
  1837. /*
  1838. * Point the bucket head pointer at the next inode.
  1839. */
  1840. ASSERT(next_agino != 0);
  1841. ASSERT(next_agino != agino);
  1842. agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
  1843. offset = offsetof(xfs_agi_t, agi_unlinked) +
  1844. (sizeof(xfs_agino_t) * bucket_index);
  1845. xfs_trans_log_buf(tp, agibp, offset,
  1846. (offset + sizeof(xfs_agino_t) - 1));
  1847. } else {
  1848. /*
  1849. * We need to search the list for the inode being freed.
  1850. */
  1851. next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
  1852. last_ibp = NULL;
  1853. while (next_agino != agino) {
  1854. struct xfs_imap imap;
  1855. if (last_ibp)
  1856. xfs_trans_brelse(tp, last_ibp);
  1857. imap.im_blkno = 0;
  1858. next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
  1859. error = xfs_imap(mp, tp, next_ino, &imap, 0);
  1860. if (error) {
  1861. xfs_warn(mp,
  1862. "%s: xfs_imap returned error %d.",
  1863. __func__, error);
  1864. return error;
  1865. }
  1866. error = xfs_imap_to_bp(mp, tp, &imap, &last_dip,
  1867. &last_ibp, 0, 0);
  1868. if (error) {
  1869. xfs_warn(mp,
  1870. "%s: xfs_imap_to_bp returned error %d.",
  1871. __func__, error);
  1872. return error;
  1873. }
  1874. last_offset = imap.im_boffset;
  1875. next_agino = be32_to_cpu(last_dip->di_next_unlinked);
  1876. ASSERT(next_agino != NULLAGINO);
  1877. ASSERT(next_agino != 0);
  1878. }
  1879. /*
  1880. * Now last_ibp points to the buffer previous to us on the
  1881. * unlinked list. Pull us from the list.
  1882. */
  1883. error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
  1884. 0, 0);
  1885. if (error) {
  1886. xfs_warn(mp, "%s: xfs_imap_to_bp(2) returned error %d.",
  1887. __func__, error);
  1888. return error;
  1889. }
  1890. next_agino = be32_to_cpu(dip->di_next_unlinked);
  1891. ASSERT(next_agino != 0);
  1892. ASSERT(next_agino != agino);
  1893. if (next_agino != NULLAGINO) {
  1894. dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
  1895. offset = ip->i_imap.im_boffset +
  1896. offsetof(xfs_dinode_t, di_next_unlinked);
  1897. /* need to recalc the inode CRC if appropriate */
  1898. xfs_dinode_calc_crc(mp, dip);
  1899. xfs_trans_inode_buf(tp, ibp);
  1900. xfs_trans_log_buf(tp, ibp, offset,
  1901. (offset + sizeof(xfs_agino_t) - 1));
  1902. xfs_inobp_check(mp, ibp);
  1903. } else {
  1904. xfs_trans_brelse(tp, ibp);
  1905. }
  1906. /*
  1907. * Point the previous inode on the list to the next inode.
  1908. */
  1909. last_dip->di_next_unlinked = cpu_to_be32(next_agino);
  1910. ASSERT(next_agino != 0);
  1911. offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
  1912. /* need to recalc the inode CRC if appropriate */
  1913. xfs_dinode_calc_crc(mp, last_dip);
  1914. xfs_trans_inode_buf(tp, last_ibp);
  1915. xfs_trans_log_buf(tp, last_ibp, offset,
  1916. (offset + sizeof(xfs_agino_t) - 1));
  1917. xfs_inobp_check(mp, last_ibp);
  1918. }
  1919. return 0;
  1920. }
  1921. /*
  1922. * A big issue when freeing the inode cluster is that we _cannot_ skip any
  1923. * inodes that are in memory - they all must be marked stale and attached to
  1924. * the cluster buffer.
  1925. */
  1926. STATIC int
  1927. xfs_ifree_cluster(
  1928. xfs_inode_t *free_ip,
  1929. xfs_trans_t *tp,
  1930. xfs_ino_t inum)
  1931. {
  1932. xfs_mount_t *mp = free_ip->i_mount;
  1933. int blks_per_cluster;
  1934. int inodes_per_cluster;
  1935. int nbufs;
  1936. int i, j;
  1937. xfs_daddr_t blkno;
  1938. xfs_buf_t *bp;
  1939. xfs_inode_t *ip;
  1940. xfs_inode_log_item_t *iip;
  1941. xfs_log_item_t *lip;
  1942. struct xfs_perag *pag;
  1943. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum));
  1944. blks_per_cluster = xfs_icluster_size_fsb(mp);
  1945. inodes_per_cluster = blks_per_cluster << mp->m_sb.sb_inopblog;
  1946. nbufs = mp->m_ialloc_blks / blks_per_cluster;
  1947. for (j = 0; j < nbufs; j++, inum += inodes_per_cluster) {
  1948. blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
  1949. XFS_INO_TO_AGBNO(mp, inum));
  1950. /*
  1951. * We obtain and lock the backing buffer first in the process
  1952. * here, as we have to ensure that any dirty inode that we
  1953. * can't get the flush lock on is attached to the buffer.
  1954. * If we scan the in-memory inodes first, then buffer IO can
  1955. * complete before we get a lock on it, and hence we may fail
  1956. * to mark all the active inodes on the buffer stale.
  1957. */
  1958. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
  1959. mp->m_bsize * blks_per_cluster,
  1960. XBF_UNMAPPED);
  1961. if (!bp)
  1962. return ENOMEM;
  1963. /*
  1964. * This buffer may not have been correctly initialised as we
  1965. * didn't read it from disk. That's not important because we are
  1966. * only using to mark the buffer as stale in the log, and to
  1967. * attach stale cached inodes on it. That means it will never be
  1968. * dispatched for IO. If it is, we want to know about it, and we
  1969. * want it to fail. We can acheive this by adding a write
  1970. * verifier to the buffer.
  1971. */
  1972. bp->b_ops = &xfs_inode_buf_ops;
  1973. /*
  1974. * Walk the inodes already attached to the buffer and mark them
  1975. * stale. These will all have the flush locks held, so an
  1976. * in-memory inode walk can't lock them. By marking them all
  1977. * stale first, we will not attempt to lock them in the loop
  1978. * below as the XFS_ISTALE flag will be set.
  1979. */
  1980. lip = bp->b_fspriv;
  1981. while (lip) {
  1982. if (lip->li_type == XFS_LI_INODE) {
  1983. iip = (xfs_inode_log_item_t *)lip;
  1984. ASSERT(iip->ili_logged == 1);
  1985. lip->li_cb = xfs_istale_done;
  1986. xfs_trans_ail_copy_lsn(mp->m_ail,
  1987. &iip->ili_flush_lsn,
  1988. &iip->ili_item.li_lsn);
  1989. xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
  1990. }
  1991. lip = lip->li_bio_list;
  1992. }
  1993. /*
  1994. * For each inode in memory attempt to add it to the inode
  1995. * buffer and set it up for being staled on buffer IO
  1996. * completion. This is safe as we've locked out tail pushing
  1997. * and flushing by locking the buffer.
  1998. *
  1999. * We have already marked every inode that was part of a
  2000. * transaction stale above, which means there is no point in
  2001. * even trying to lock them.
  2002. */
  2003. for (i = 0; i < inodes_per_cluster; i++) {
  2004. retry:
  2005. rcu_read_lock();
  2006. ip = radix_tree_lookup(&pag->pag_ici_root,
  2007. XFS_INO_TO_AGINO(mp, (inum + i)));
  2008. /* Inode not in memory, nothing to do */
  2009. if (!ip) {
  2010. rcu_read_unlock();
  2011. continue;
  2012. }
  2013. /*
  2014. * because this is an RCU protected lookup, we could
  2015. * find a recently freed or even reallocated inode
  2016. * during the lookup. We need to check under the
  2017. * i_flags_lock for a valid inode here. Skip it if it
  2018. * is not valid, the wrong inode or stale.
  2019. */
  2020. spin_lock(&ip->i_flags_lock);
  2021. if (ip->i_ino != inum + i ||
  2022. __xfs_iflags_test(ip, XFS_ISTALE)) {
  2023. spin_unlock(&ip->i_flags_lock);
  2024. rcu_read_unlock();
  2025. continue;
  2026. }
  2027. spin_unlock(&ip->i_flags_lock);
  2028. /*
  2029. * Don't try to lock/unlock the current inode, but we
  2030. * _cannot_ skip the other inodes that we did not find
  2031. * in the list attached to the buffer and are not
  2032. * already marked stale. If we can't lock it, back off
  2033. * and retry.
  2034. */
  2035. if (ip != free_ip &&
  2036. !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
  2037. rcu_read_unlock();
  2038. delay(1);
  2039. goto retry;
  2040. }
  2041. rcu_read_unlock();
  2042. xfs_iflock(ip);
  2043. xfs_iflags_set(ip, XFS_ISTALE);
  2044. /*
  2045. * we don't need to attach clean inodes or those only
  2046. * with unlogged changes (which we throw away, anyway).
  2047. */
  2048. iip = ip->i_itemp;
  2049. if (!iip || xfs_inode_clean(ip)) {
  2050. ASSERT(ip != free_ip);
  2051. xfs_ifunlock(ip);
  2052. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2053. continue;
  2054. }
  2055. iip->ili_last_fields = iip->ili_fields;
  2056. iip->ili_fields = 0;
  2057. iip->ili_logged = 1;
  2058. xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
  2059. &iip->ili_item.li_lsn);
  2060. xfs_buf_attach_iodone(bp, xfs_istale_done,
  2061. &iip->ili_item);
  2062. if (ip != free_ip)
  2063. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2064. }
  2065. xfs_trans_stale_inode_buf(tp, bp);
  2066. xfs_trans_binval(tp, bp);
  2067. }
  2068. xfs_perag_put(pag);
  2069. return 0;
  2070. }
  2071. /*
  2072. * This is called to return an inode to the inode free list.
  2073. * The inode should already be truncated to 0 length and have
  2074. * no pages associated with it. This routine also assumes that
  2075. * the inode is already a part of the transaction.
  2076. *
  2077. * The on-disk copy of the inode will have been added to the list
  2078. * of unlinked inodes in the AGI. We need to remove the inode from
  2079. * that list atomically with respect to freeing it here.
  2080. */
  2081. int
  2082. xfs_ifree(
  2083. xfs_trans_t *tp,
  2084. xfs_inode_t *ip,
  2085. xfs_bmap_free_t *flist)
  2086. {
  2087. int error;
  2088. int delete;
  2089. xfs_ino_t first_ino;
  2090. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  2091. ASSERT(ip->i_d.di_nlink == 0);
  2092. ASSERT(ip->i_d.di_nextents == 0);
  2093. ASSERT(ip->i_d.di_anextents == 0);
  2094. ASSERT(ip->i_d.di_size == 0 || !S_ISREG(ip->i_d.di_mode));
  2095. ASSERT(ip->i_d.di_nblocks == 0);
  2096. /*
  2097. * Pull the on-disk inode from the AGI unlinked list.
  2098. */
  2099. error = xfs_iunlink_remove(tp, ip);
  2100. if (error)
  2101. return error;
  2102. error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino);
  2103. if (error)
  2104. return error;
  2105. ip->i_d.di_mode = 0; /* mark incore inode as free */
  2106. ip->i_d.di_flags = 0;
  2107. ip->i_d.di_dmevmask = 0;
  2108. ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */
  2109. ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
  2110. ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
  2111. /*
  2112. * Bump the generation count so no one will be confused
  2113. * by reincarnations of this inode.
  2114. */
  2115. ip->i_d.di_gen++;
  2116. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  2117. if (delete)
  2118. error = xfs_ifree_cluster(ip, tp, first_ino);
  2119. return error;
  2120. }
  2121. /*
  2122. * This is called to unpin an inode. The caller must have the inode locked
  2123. * in at least shared mode so that the buffer cannot be subsequently pinned
  2124. * once someone is waiting for it to be unpinned.
  2125. */
  2126. static void
  2127. xfs_iunpin(
  2128. struct xfs_inode *ip)
  2129. {
  2130. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  2131. trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
  2132. /* Give the log a push to start the unpinning I/O */
  2133. xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0);
  2134. }
  2135. static void
  2136. __xfs_iunpin_wait(
  2137. struct xfs_inode *ip)
  2138. {
  2139. wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
  2140. DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
  2141. xfs_iunpin(ip);
  2142. do {
  2143. prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
  2144. if (xfs_ipincount(ip))
  2145. io_schedule();
  2146. } while (xfs_ipincount(ip));
  2147. finish_wait(wq, &wait.wait);
  2148. }
  2149. void
  2150. xfs_iunpin_wait(
  2151. struct xfs_inode *ip)
  2152. {
  2153. if (xfs_ipincount(ip))
  2154. __xfs_iunpin_wait(ip);
  2155. }
  2156. /*
  2157. * Removing an inode from the namespace involves removing the directory entry
  2158. * and dropping the link count on the inode. Removing the directory entry can
  2159. * result in locking an AGF (directory blocks were freed) and removing a link
  2160. * count can result in placing the inode on an unlinked list which results in
  2161. * locking an AGI.
  2162. *
  2163. * The big problem here is that we have an ordering constraint on AGF and AGI
  2164. * locking - inode allocation locks the AGI, then can allocate a new extent for
  2165. * new inodes, locking the AGF after the AGI. Similarly, freeing the inode
  2166. * removes the inode from the unlinked list, requiring that we lock the AGI
  2167. * first, and then freeing the inode can result in an inode chunk being freed
  2168. * and hence freeing disk space requiring that we lock an AGF.
  2169. *
  2170. * Hence the ordering that is imposed by other parts of the code is AGI before
  2171. * AGF. This means we cannot remove the directory entry before we drop the inode
  2172. * reference count and put it on the unlinked list as this results in a lock
  2173. * order of AGF then AGI, and this can deadlock against inode allocation and
  2174. * freeing. Therefore we must drop the link counts before we remove the
  2175. * directory entry.
  2176. *
  2177. * This is still safe from a transactional point of view - it is not until we
  2178. * get to xfs_bmap_finish() that we have the possibility of multiple
  2179. * transactions in this operation. Hence as long as we remove the directory
  2180. * entry and drop the link count in the first transaction of the remove
  2181. * operation, there are no transactional constraints on the ordering here.
  2182. */
  2183. int
  2184. xfs_remove(
  2185. xfs_inode_t *dp,
  2186. struct xfs_name *name,
  2187. xfs_inode_t *ip)
  2188. {
  2189. xfs_mount_t *mp = dp->i_mount;
  2190. xfs_trans_t *tp = NULL;
  2191. int is_dir = S_ISDIR(ip->i_d.di_mode);
  2192. int error = 0;
  2193. xfs_bmap_free_t free_list;
  2194. xfs_fsblock_t first_block;
  2195. int cancel_flags;
  2196. int committed;
  2197. int link_zero;
  2198. uint resblks;
  2199. uint log_count;
  2200. trace_xfs_remove(dp, name);
  2201. if (XFS_FORCED_SHUTDOWN(mp))
  2202. return XFS_ERROR(EIO);
  2203. error = xfs_qm_dqattach(dp, 0);
  2204. if (error)
  2205. goto std_return;
  2206. error = xfs_qm_dqattach(ip, 0);
  2207. if (error)
  2208. goto std_return;
  2209. if (is_dir) {
  2210. tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
  2211. log_count = XFS_DEFAULT_LOG_COUNT;
  2212. } else {
  2213. tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
  2214. log_count = XFS_REMOVE_LOG_COUNT;
  2215. }
  2216. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2217. /*
  2218. * We try to get the real space reservation first,
  2219. * allowing for directory btree deletion(s) implying
  2220. * possible bmap insert(s). If we can't get the space
  2221. * reservation then we use 0 instead, and avoid the bmap
  2222. * btree insert(s) in the directory code by, if the bmap
  2223. * insert tries to happen, instead trimming the LAST
  2224. * block from the directory.
  2225. */
  2226. resblks = XFS_REMOVE_SPACE_RES(mp);
  2227. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_remove, resblks, 0);
  2228. if (error == ENOSPC) {
  2229. resblks = 0;
  2230. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_remove, 0, 0);
  2231. }
  2232. if (error) {
  2233. ASSERT(error != ENOSPC);
  2234. cancel_flags = 0;
  2235. goto out_trans_cancel;
  2236. }
  2237. xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
  2238. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  2239. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  2240. /*
  2241. * If we're removing a directory perform some additional validation.
  2242. */
  2243. cancel_flags |= XFS_TRANS_ABORT;
  2244. if (is_dir) {
  2245. ASSERT(ip->i_d.di_nlink >= 2);
  2246. if (ip->i_d.di_nlink != 2) {
  2247. error = XFS_ERROR(ENOTEMPTY);
  2248. goto out_trans_cancel;
  2249. }
  2250. if (!xfs_dir_isempty(ip)) {
  2251. error = XFS_ERROR(ENOTEMPTY);
  2252. goto out_trans_cancel;
  2253. }
  2254. /* Drop the link from ip's "..". */
  2255. error = xfs_droplink(tp, dp);
  2256. if (error)
  2257. goto out_trans_cancel;
  2258. /* Drop the "." link from ip to self. */
  2259. error = xfs_droplink(tp, ip);
  2260. if (error)
  2261. goto out_trans_cancel;
  2262. } else {
  2263. /*
  2264. * When removing a non-directory we need to log the parent
  2265. * inode here. For a directory this is done implicitly
  2266. * by the xfs_droplink call for the ".." entry.
  2267. */
  2268. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  2269. }
  2270. xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2271. /* Drop the link from dp to ip. */
  2272. error = xfs_droplink(tp, ip);
  2273. if (error)
  2274. goto out_trans_cancel;
  2275. /* Determine if this is the last link while the inode is locked */
  2276. link_zero = (ip->i_d.di_nlink == 0);
  2277. xfs_bmap_init(&free_list, &first_block);
  2278. error = xfs_dir_removename(tp, dp, name, ip->i_ino,
  2279. &first_block, &free_list, resblks);
  2280. if (error) {
  2281. ASSERT(error != ENOENT);
  2282. goto out_bmap_cancel;
  2283. }
  2284. /*
  2285. * If this is a synchronous mount, make sure that the
  2286. * remove transaction goes to disk before returning to
  2287. * the user.
  2288. */
  2289. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
  2290. xfs_trans_set_sync(tp);
  2291. error = xfs_bmap_finish(&tp, &free_list, &committed);
  2292. if (error)
  2293. goto out_bmap_cancel;
  2294. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2295. if (error)
  2296. goto std_return;
  2297. if (is_dir && xfs_inode_is_filestream(ip))
  2298. xfs_filestream_deassociate(ip);
  2299. return 0;
  2300. out_bmap_cancel:
  2301. xfs_bmap_cancel(&free_list);
  2302. out_trans_cancel:
  2303. xfs_trans_cancel(tp, cancel_flags);
  2304. std_return:
  2305. return error;
  2306. }
  2307. /*
  2308. * Enter all inodes for a rename transaction into a sorted array.
  2309. */
  2310. STATIC void
  2311. xfs_sort_for_rename(
  2312. xfs_inode_t *dp1, /* in: old (source) directory inode */
  2313. xfs_inode_t *dp2, /* in: new (target) directory inode */
  2314. xfs_inode_t *ip1, /* in: inode of old entry */
  2315. xfs_inode_t *ip2, /* in: inode of new entry, if it
  2316. already exists, NULL otherwise. */
  2317. xfs_inode_t **i_tab,/* out: array of inode returned, sorted */
  2318. int *num_inodes) /* out: number of inodes in array */
  2319. {
  2320. xfs_inode_t *temp;
  2321. int i, j;
  2322. /*
  2323. * i_tab contains a list of pointers to inodes. We initialize
  2324. * the table here & we'll sort it. We will then use it to
  2325. * order the acquisition of the inode locks.
  2326. *
  2327. * Note that the table may contain duplicates. e.g., dp1 == dp2.
  2328. */
  2329. i_tab[0] = dp1;
  2330. i_tab[1] = dp2;
  2331. i_tab[2] = ip1;
  2332. if (ip2) {
  2333. *num_inodes = 4;
  2334. i_tab[3] = ip2;
  2335. } else {
  2336. *num_inodes = 3;
  2337. i_tab[3] = NULL;
  2338. }
  2339. /*
  2340. * Sort the elements via bubble sort. (Remember, there are at
  2341. * most 4 elements to sort, so this is adequate.)
  2342. */
  2343. for (i = 0; i < *num_inodes; i++) {
  2344. for (j = 1; j < *num_inodes; j++) {
  2345. if (i_tab[j]->i_ino < i_tab[j-1]->i_ino) {
  2346. temp = i_tab[j];
  2347. i_tab[j] = i_tab[j-1];
  2348. i_tab[j-1] = temp;
  2349. }
  2350. }
  2351. }
  2352. }
  2353. /*
  2354. * xfs_rename
  2355. */
  2356. int
  2357. xfs_rename(
  2358. xfs_inode_t *src_dp,
  2359. struct xfs_name *src_name,
  2360. xfs_inode_t *src_ip,
  2361. xfs_inode_t *target_dp,
  2362. struct xfs_name *target_name,
  2363. xfs_inode_t *target_ip)
  2364. {
  2365. xfs_trans_t *tp = NULL;
  2366. xfs_mount_t *mp = src_dp->i_mount;
  2367. int new_parent; /* moving to a new dir */
  2368. int src_is_directory; /* src_name is a directory */
  2369. int error;
  2370. xfs_bmap_free_t free_list;
  2371. xfs_fsblock_t first_block;
  2372. int cancel_flags;
  2373. int committed;
  2374. xfs_inode_t *inodes[4];
  2375. int spaceres;
  2376. int num_inodes;
  2377. trace_xfs_rename(src_dp, target_dp, src_name, target_name);
  2378. new_parent = (src_dp != target_dp);
  2379. src_is_directory = S_ISDIR(src_ip->i_d.di_mode);
  2380. xfs_sort_for_rename(src_dp, target_dp, src_ip, target_ip,
  2381. inodes, &num_inodes);
  2382. xfs_bmap_init(&free_list, &first_block);
  2383. tp = xfs_trans_alloc(mp, XFS_TRANS_RENAME);
  2384. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2385. spaceres = XFS_RENAME_SPACE_RES(mp, target_name->len);
  2386. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_rename, spaceres, 0);
  2387. if (error == ENOSPC) {
  2388. spaceres = 0;
  2389. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_rename, 0, 0);
  2390. }
  2391. if (error) {
  2392. xfs_trans_cancel(tp, 0);
  2393. goto std_return;
  2394. }
  2395. /*
  2396. * Attach the dquots to the inodes
  2397. */
  2398. error = xfs_qm_vop_rename_dqattach(inodes);
  2399. if (error) {
  2400. xfs_trans_cancel(tp, cancel_flags);
  2401. goto std_return;
  2402. }
  2403. /*
  2404. * Lock all the participating inodes. Depending upon whether
  2405. * the target_name exists in the target directory, and
  2406. * whether the target directory is the same as the source
  2407. * directory, we can lock from 2 to 4 inodes.
  2408. */
  2409. xfs_lock_inodes(inodes, num_inodes, XFS_ILOCK_EXCL);
  2410. /*
  2411. * Join all the inodes to the transaction. From this point on,
  2412. * we can rely on either trans_commit or trans_cancel to unlock
  2413. * them.
  2414. */
  2415. xfs_trans_ijoin(tp, src_dp, XFS_ILOCK_EXCL);
  2416. if (new_parent)
  2417. xfs_trans_ijoin(tp, target_dp, XFS_ILOCK_EXCL);
  2418. xfs_trans_ijoin(tp, src_ip, XFS_ILOCK_EXCL);
  2419. if (target_ip)
  2420. xfs_trans_ijoin(tp, target_ip, XFS_ILOCK_EXCL);
  2421. /*
  2422. * If we are using project inheritance, we only allow renames
  2423. * into our tree when the project IDs are the same; else the
  2424. * tree quota mechanism would be circumvented.
  2425. */
  2426. if (unlikely((target_dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
  2427. (xfs_get_projid(target_dp) != xfs_get_projid(src_ip)))) {
  2428. error = XFS_ERROR(EXDEV);
  2429. goto error_return;
  2430. }
  2431. /*
  2432. * Set up the target.
  2433. */
  2434. if (target_ip == NULL) {
  2435. /*
  2436. * If there's no space reservation, check the entry will
  2437. * fit before actually inserting it.
  2438. */
  2439. error = xfs_dir_canenter(tp, target_dp, target_name, spaceres);
  2440. if (error)
  2441. goto error_return;
  2442. /*
  2443. * If target does not exist and the rename crosses
  2444. * directories, adjust the target directory link count
  2445. * to account for the ".." reference from the new entry.
  2446. */
  2447. error = xfs_dir_createname(tp, target_dp, target_name,
  2448. src_ip->i_ino, &first_block,
  2449. &free_list, spaceres);
  2450. if (error == ENOSPC)
  2451. goto error_return;
  2452. if (error)
  2453. goto abort_return;
  2454. xfs_trans_ichgtime(tp, target_dp,
  2455. XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2456. if (new_parent && src_is_directory) {
  2457. error = xfs_bumplink(tp, target_dp);
  2458. if (error)
  2459. goto abort_return;
  2460. }
  2461. } else { /* target_ip != NULL */
  2462. /*
  2463. * If target exists and it's a directory, check that both
  2464. * target and source are directories and that target can be
  2465. * destroyed, or that neither is a directory.
  2466. */
  2467. if (S_ISDIR(target_ip->i_d.di_mode)) {
  2468. /*
  2469. * Make sure target dir is empty.
  2470. */
  2471. if (!(xfs_dir_isempty(target_ip)) ||
  2472. (target_ip->i_d.di_nlink > 2)) {
  2473. error = XFS_ERROR(EEXIST);
  2474. goto error_return;
  2475. }
  2476. }
  2477. /*
  2478. * Link the source inode under the target name.
  2479. * If the source inode is a directory and we are moving
  2480. * it across directories, its ".." entry will be
  2481. * inconsistent until we replace that down below.
  2482. *
  2483. * In case there is already an entry with the same
  2484. * name at the destination directory, remove it first.
  2485. */
  2486. error = xfs_dir_replace(tp, target_dp, target_name,
  2487. src_ip->i_ino,
  2488. &first_block, &free_list, spaceres);
  2489. if (error)
  2490. goto abort_return;
  2491. xfs_trans_ichgtime(tp, target_dp,
  2492. XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2493. /*
  2494. * Decrement the link count on the target since the target
  2495. * dir no longer points to it.
  2496. */
  2497. error = xfs_droplink(tp, target_ip);
  2498. if (error)
  2499. goto abort_return;
  2500. if (src_is_directory) {
  2501. /*
  2502. * Drop the link from the old "." entry.
  2503. */
  2504. error = xfs_droplink(tp, target_ip);
  2505. if (error)
  2506. goto abort_return;
  2507. }
  2508. } /* target_ip != NULL */
  2509. /*
  2510. * Remove the source.
  2511. */
  2512. if (new_parent && src_is_directory) {
  2513. /*
  2514. * Rewrite the ".." entry to point to the new
  2515. * directory.
  2516. */
  2517. error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot,
  2518. target_dp->i_ino,
  2519. &first_block, &free_list, spaceres);
  2520. ASSERT(error != EEXIST);
  2521. if (error)
  2522. goto abort_return;
  2523. }
  2524. /*
  2525. * We always want to hit the ctime on the source inode.
  2526. *
  2527. * This isn't strictly required by the standards since the source
  2528. * inode isn't really being changed, but old unix file systems did
  2529. * it and some incremental backup programs won't work without it.
  2530. */
  2531. xfs_trans_ichgtime(tp, src_ip, XFS_ICHGTIME_CHG);
  2532. xfs_trans_log_inode(tp, src_ip, XFS_ILOG_CORE);
  2533. /*
  2534. * Adjust the link count on src_dp. This is necessary when
  2535. * renaming a directory, either within one parent when
  2536. * the target existed, or across two parent directories.
  2537. */
  2538. if (src_is_directory && (new_parent || target_ip != NULL)) {
  2539. /*
  2540. * Decrement link count on src_directory since the
  2541. * entry that's moved no longer points to it.
  2542. */
  2543. error = xfs_droplink(tp, src_dp);
  2544. if (error)
  2545. goto abort_return;
  2546. }
  2547. error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino,
  2548. &first_block, &free_list, spaceres);
  2549. if (error)
  2550. goto abort_return;
  2551. xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2552. xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE);
  2553. if (new_parent)
  2554. xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE);
  2555. /*
  2556. * If this is a synchronous mount, make sure that the
  2557. * rename transaction goes to disk before returning to
  2558. * the user.
  2559. */
  2560. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  2561. xfs_trans_set_sync(tp);
  2562. }
  2563. error = xfs_bmap_finish(&tp, &free_list, &committed);
  2564. if (error) {
  2565. xfs_bmap_cancel(&free_list);
  2566. xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
  2567. XFS_TRANS_ABORT));
  2568. goto std_return;
  2569. }
  2570. /*
  2571. * trans_commit will unlock src_ip, target_ip & decrement
  2572. * the vnode references.
  2573. */
  2574. return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2575. abort_return:
  2576. cancel_flags |= XFS_TRANS_ABORT;
  2577. error_return:
  2578. xfs_bmap_cancel(&free_list);
  2579. xfs_trans_cancel(tp, cancel_flags);
  2580. std_return:
  2581. return error;
  2582. }
  2583. STATIC int
  2584. xfs_iflush_cluster(
  2585. xfs_inode_t *ip,
  2586. xfs_buf_t *bp)
  2587. {
  2588. xfs_mount_t *mp = ip->i_mount;
  2589. struct xfs_perag *pag;
  2590. unsigned long first_index, mask;
  2591. unsigned long inodes_per_cluster;
  2592. int ilist_size;
  2593. xfs_inode_t **ilist;
  2594. xfs_inode_t *iq;
  2595. int nr_found;
  2596. int clcount = 0;
  2597. int bufwasdelwri;
  2598. int i;
  2599. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
  2600. inodes_per_cluster = mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog;
  2601. ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *);
  2602. ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS);
  2603. if (!ilist)
  2604. goto out_put;
  2605. mask = ~(((mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog)) - 1);
  2606. first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask;
  2607. rcu_read_lock();
  2608. /* really need a gang lookup range call here */
  2609. nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist,
  2610. first_index, inodes_per_cluster);
  2611. if (nr_found == 0)
  2612. goto out_free;
  2613. for (i = 0; i < nr_found; i++) {
  2614. iq = ilist[i];
  2615. if (iq == ip)
  2616. continue;
  2617. /*
  2618. * because this is an RCU protected lookup, we could find a
  2619. * recently freed or even reallocated inode during the lookup.
  2620. * We need to check under the i_flags_lock for a valid inode
  2621. * here. Skip it if it is not valid or the wrong inode.
  2622. */
  2623. spin_lock(&ip->i_flags_lock);
  2624. if (!ip->i_ino ||
  2625. (XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) {
  2626. spin_unlock(&ip->i_flags_lock);
  2627. continue;
  2628. }
  2629. spin_unlock(&ip->i_flags_lock);
  2630. /*
  2631. * Do an un-protected check to see if the inode is dirty and
  2632. * is a candidate for flushing. These checks will be repeated
  2633. * later after the appropriate locks are acquired.
  2634. */
  2635. if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0)
  2636. continue;
  2637. /*
  2638. * Try to get locks. If any are unavailable or it is pinned,
  2639. * then this inode cannot be flushed and is skipped.
  2640. */
  2641. if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED))
  2642. continue;
  2643. if (!xfs_iflock_nowait(iq)) {
  2644. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2645. continue;
  2646. }
  2647. if (xfs_ipincount(iq)) {
  2648. xfs_ifunlock(iq);
  2649. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2650. continue;
  2651. }
  2652. /*
  2653. * arriving here means that this inode can be flushed. First
  2654. * re-check that it's dirty before flushing.
  2655. */
  2656. if (!xfs_inode_clean(iq)) {
  2657. int error;
  2658. error = xfs_iflush_int(iq, bp);
  2659. if (error) {
  2660. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2661. goto cluster_corrupt_out;
  2662. }
  2663. clcount++;
  2664. } else {
  2665. xfs_ifunlock(iq);
  2666. }
  2667. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2668. }
  2669. if (clcount) {
  2670. XFS_STATS_INC(xs_icluster_flushcnt);
  2671. XFS_STATS_ADD(xs_icluster_flushinode, clcount);
  2672. }
  2673. out_free:
  2674. rcu_read_unlock();
  2675. kmem_free(ilist);
  2676. out_put:
  2677. xfs_perag_put(pag);
  2678. return 0;
  2679. cluster_corrupt_out:
  2680. /*
  2681. * Corruption detected in the clustering loop. Invalidate the
  2682. * inode buffer and shut down the filesystem.
  2683. */
  2684. rcu_read_unlock();
  2685. /*
  2686. * Clean up the buffer. If it was delwri, just release it --
  2687. * brelse can handle it with no problems. If not, shut down the
  2688. * filesystem before releasing the buffer.
  2689. */
  2690. bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q);
  2691. if (bufwasdelwri)
  2692. xfs_buf_relse(bp);
  2693. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  2694. if (!bufwasdelwri) {
  2695. /*
  2696. * Just like incore_relse: if we have b_iodone functions,
  2697. * mark the buffer as an error and call them. Otherwise
  2698. * mark it as stale and brelse.
  2699. */
  2700. if (bp->b_iodone) {
  2701. XFS_BUF_UNDONE(bp);
  2702. xfs_buf_stale(bp);
  2703. xfs_buf_ioerror(bp, EIO);
  2704. xfs_buf_ioend(bp, 0);
  2705. } else {
  2706. xfs_buf_stale(bp);
  2707. xfs_buf_relse(bp);
  2708. }
  2709. }
  2710. /*
  2711. * Unlocks the flush lock
  2712. */
  2713. xfs_iflush_abort(iq, false);
  2714. kmem_free(ilist);
  2715. xfs_perag_put(pag);
  2716. return XFS_ERROR(EFSCORRUPTED);
  2717. }
  2718. /*
  2719. * Flush dirty inode metadata into the backing buffer.
  2720. *
  2721. * The caller must have the inode lock and the inode flush lock held. The
  2722. * inode lock will still be held upon return to the caller, and the inode
  2723. * flush lock will be released after the inode has reached the disk.
  2724. *
  2725. * The caller must write out the buffer returned in *bpp and release it.
  2726. */
  2727. int
  2728. xfs_iflush(
  2729. struct xfs_inode *ip,
  2730. struct xfs_buf **bpp)
  2731. {
  2732. struct xfs_mount *mp = ip->i_mount;
  2733. struct xfs_buf *bp;
  2734. struct xfs_dinode *dip;
  2735. int error;
  2736. XFS_STATS_INC(xs_iflush_count);
  2737. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  2738. ASSERT(xfs_isiflocked(ip));
  2739. ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
  2740. ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
  2741. *bpp = NULL;
  2742. xfs_iunpin_wait(ip);
  2743. /*
  2744. * For stale inodes we cannot rely on the backing buffer remaining
  2745. * stale in cache for the remaining life of the stale inode and so
  2746. * xfs_imap_to_bp() below may give us a buffer that no longer contains
  2747. * inodes below. We have to check this after ensuring the inode is
  2748. * unpinned so that it is safe to reclaim the stale inode after the
  2749. * flush call.
  2750. */
  2751. if (xfs_iflags_test(ip, XFS_ISTALE)) {
  2752. xfs_ifunlock(ip);
  2753. return 0;
  2754. }
  2755. /*
  2756. * This may have been unpinned because the filesystem is shutting
  2757. * down forcibly. If that's the case we must not write this inode
  2758. * to disk, because the log record didn't make it to disk.
  2759. *
  2760. * We also have to remove the log item from the AIL in this case,
  2761. * as we wait for an empty AIL as part of the unmount process.
  2762. */
  2763. if (XFS_FORCED_SHUTDOWN(mp)) {
  2764. error = XFS_ERROR(EIO);
  2765. goto abort_out;
  2766. }
  2767. /*
  2768. * Get the buffer containing the on-disk inode.
  2769. */
  2770. error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, XBF_TRYLOCK,
  2771. 0);
  2772. if (error || !bp) {
  2773. xfs_ifunlock(ip);
  2774. return error;
  2775. }
  2776. /*
  2777. * First flush out the inode that xfs_iflush was called with.
  2778. */
  2779. error = xfs_iflush_int(ip, bp);
  2780. if (error)
  2781. goto corrupt_out;
  2782. /*
  2783. * If the buffer is pinned then push on the log now so we won't
  2784. * get stuck waiting in the write for too long.
  2785. */
  2786. if (xfs_buf_ispinned(bp))
  2787. xfs_log_force(mp, 0);
  2788. /*
  2789. * inode clustering:
  2790. * see if other inodes can be gathered into this write
  2791. */
  2792. error = xfs_iflush_cluster(ip, bp);
  2793. if (error)
  2794. goto cluster_corrupt_out;
  2795. *bpp = bp;
  2796. return 0;
  2797. corrupt_out:
  2798. xfs_buf_relse(bp);
  2799. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  2800. cluster_corrupt_out:
  2801. error = XFS_ERROR(EFSCORRUPTED);
  2802. abort_out:
  2803. /*
  2804. * Unlocks the flush lock
  2805. */
  2806. xfs_iflush_abort(ip, false);
  2807. return error;
  2808. }
  2809. STATIC int
  2810. xfs_iflush_int(
  2811. struct xfs_inode *ip,
  2812. struct xfs_buf *bp)
  2813. {
  2814. struct xfs_inode_log_item *iip = ip->i_itemp;
  2815. struct xfs_dinode *dip;
  2816. struct xfs_mount *mp = ip->i_mount;
  2817. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  2818. ASSERT(xfs_isiflocked(ip));
  2819. ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
  2820. ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
  2821. ASSERT(iip != NULL && iip->ili_fields != 0);
  2822. ASSERT(ip->i_d.di_version > 1);
  2823. /* set *dip = inode's place in the buffer */
  2824. dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
  2825. if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
  2826. mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
  2827. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2828. "%s: Bad inode %Lu magic number 0x%x, ptr 0x%p",
  2829. __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
  2830. goto corrupt_out;
  2831. }
  2832. if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC,
  2833. mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) {
  2834. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2835. "%s: Bad inode %Lu, ptr 0x%p, magic number 0x%x",
  2836. __func__, ip->i_ino, ip, ip->i_d.di_magic);
  2837. goto corrupt_out;
  2838. }
  2839. if (S_ISREG(ip->i_d.di_mode)) {
  2840. if (XFS_TEST_ERROR(
  2841. (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
  2842. (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
  2843. mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
  2844. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2845. "%s: Bad regular inode %Lu, ptr 0x%p",
  2846. __func__, ip->i_ino, ip);
  2847. goto corrupt_out;
  2848. }
  2849. } else if (S_ISDIR(ip->i_d.di_mode)) {
  2850. if (XFS_TEST_ERROR(
  2851. (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
  2852. (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
  2853. (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
  2854. mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
  2855. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2856. "%s: Bad directory inode %Lu, ptr 0x%p",
  2857. __func__, ip->i_ino, ip);
  2858. goto corrupt_out;
  2859. }
  2860. }
  2861. if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
  2862. ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
  2863. XFS_RANDOM_IFLUSH_5)) {
  2864. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2865. "%s: detected corrupt incore inode %Lu, "
  2866. "total extents = %d, nblocks = %Ld, ptr 0x%p",
  2867. __func__, ip->i_ino,
  2868. ip->i_d.di_nextents + ip->i_d.di_anextents,
  2869. ip->i_d.di_nblocks, ip);
  2870. goto corrupt_out;
  2871. }
  2872. if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
  2873. mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
  2874. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2875. "%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
  2876. __func__, ip->i_ino, ip->i_d.di_forkoff, ip);
  2877. goto corrupt_out;
  2878. }
  2879. /*
  2880. * Inode item log recovery for v2 inodes are dependent on the
  2881. * di_flushiter count for correct sequencing. We bump the flush
  2882. * iteration count so we can detect flushes which postdate a log record
  2883. * during recovery. This is redundant as we now log every change and
  2884. * hence this can't happen but we need to still do it to ensure
  2885. * backwards compatibility with old kernels that predate logging all
  2886. * inode changes.
  2887. */
  2888. if (ip->i_d.di_version < 3)
  2889. ip->i_d.di_flushiter++;
  2890. /*
  2891. * Copy the dirty parts of the inode into the on-disk
  2892. * inode. We always copy out the core of the inode,
  2893. * because if the inode is dirty at all the core must
  2894. * be.
  2895. */
  2896. xfs_dinode_to_disk(dip, &ip->i_d);
  2897. /* Wrap, we never let the log put out DI_MAX_FLUSH */
  2898. if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
  2899. ip->i_d.di_flushiter = 0;
  2900. xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK);
  2901. if (XFS_IFORK_Q(ip))
  2902. xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK);
  2903. xfs_inobp_check(mp, bp);
  2904. /*
  2905. * We've recorded everything logged in the inode, so we'd like to clear
  2906. * the ili_fields bits so we don't log and flush things unnecessarily.
  2907. * However, we can't stop logging all this information until the data
  2908. * we've copied into the disk buffer is written to disk. If we did we
  2909. * might overwrite the copy of the inode in the log with all the data
  2910. * after re-logging only part of it, and in the face of a crash we
  2911. * wouldn't have all the data we need to recover.
  2912. *
  2913. * What we do is move the bits to the ili_last_fields field. When
  2914. * logging the inode, these bits are moved back to the ili_fields field.
  2915. * In the xfs_iflush_done() routine we clear ili_last_fields, since we
  2916. * know that the information those bits represent is permanently on
  2917. * disk. As long as the flush completes before the inode is logged
  2918. * again, then both ili_fields and ili_last_fields will be cleared.
  2919. *
  2920. * We can play with the ili_fields bits here, because the inode lock
  2921. * must be held exclusively in order to set bits there and the flush
  2922. * lock protects the ili_last_fields bits. Set ili_logged so the flush
  2923. * done routine can tell whether or not to look in the AIL. Also, store
  2924. * the current LSN of the inode so that we can tell whether the item has
  2925. * moved in the AIL from xfs_iflush_done(). In order to read the lsn we
  2926. * need the AIL lock, because it is a 64 bit value that cannot be read
  2927. * atomically.
  2928. */
  2929. iip->ili_last_fields = iip->ili_fields;
  2930. iip->ili_fields = 0;
  2931. iip->ili_logged = 1;
  2932. xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
  2933. &iip->ili_item.li_lsn);
  2934. /*
  2935. * Attach the function xfs_iflush_done to the inode's
  2936. * buffer. This will remove the inode from the AIL
  2937. * and unlock the inode's flush lock when the inode is
  2938. * completely written to disk.
  2939. */
  2940. xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item);
  2941. /* update the lsn in the on disk inode if required */
  2942. if (ip->i_d.di_version == 3)
  2943. dip->di_lsn = cpu_to_be64(iip->ili_item.li_lsn);
  2944. /* generate the checksum. */
  2945. xfs_dinode_calc_crc(mp, dip);
  2946. ASSERT(bp->b_fspriv != NULL);
  2947. ASSERT(bp->b_iodone != NULL);
  2948. return 0;
  2949. corrupt_out:
  2950. return XFS_ERROR(EFSCORRUPTED);
  2951. }