xfs_vnodeops.c 70 KB

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