xfs_vnodeops.c 78 KB

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