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