xfs_inode.c 89 KB

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