xfs_vnodeops.c 78 KB

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