xfs_inode.c 88 KB

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