xfs_vnodeops.c 58 KB

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