xfs_vnodeops.c 70 KB

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