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