xfs_vnodeops.c 58 KB

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