xfs_inode.c 81 KB

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