xfs_vnodeops.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380
  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. xfs_bmap_init(&free_list, &first_block);
  823. /*
  824. * Reserve disk quota and the inode.
  825. */
  826. error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
  827. if (error)
  828. goto out_trans_cancel;
  829. error = xfs_dir_canenter(tp, dp, name, resblks);
  830. if (error)
  831. goto out_trans_cancel;
  832. /*
  833. * A newly created regular or special file just has one directory
  834. * entry pointing to them, but a directory also the "." entry
  835. * pointing to itself.
  836. */
  837. error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev,
  838. prid, resblks > 0, &ip, &committed);
  839. if (error) {
  840. if (error == ENOSPC)
  841. goto out_trans_cancel;
  842. goto out_trans_abort;
  843. }
  844. /*
  845. * Now we join the directory inode to the transaction. We do not do it
  846. * earlier because xfs_dir_ialloc might commit the previous transaction
  847. * (and release all the locks). An error from here on will result in
  848. * the transaction cancel unlocking dp so don't do it explicitly in the
  849. * error path.
  850. */
  851. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  852. unlock_dp_on_error = B_FALSE;
  853. error = xfs_dir_createname(tp, dp, name, ip->i_ino,
  854. &first_block, &free_list, resblks ?
  855. resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
  856. if (error) {
  857. ASSERT(error != ENOSPC);
  858. goto out_trans_abort;
  859. }
  860. xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  861. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  862. if (is_dir) {
  863. error = xfs_dir_init(tp, ip, dp);
  864. if (error)
  865. goto out_bmap_cancel;
  866. error = xfs_bumplink(tp, dp);
  867. if (error)
  868. goto out_bmap_cancel;
  869. }
  870. /*
  871. * If this is a synchronous mount, make sure that the
  872. * create transaction goes to disk before returning to
  873. * the user.
  874. */
  875. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
  876. xfs_trans_set_sync(tp);
  877. /*
  878. * Attach the dquot(s) to the inodes and modify them incore.
  879. * These ids of the inode couldn't have changed since the new
  880. * inode has been locked ever since it was created.
  881. */
  882. xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
  883. error = xfs_bmap_finish(&tp, &free_list, &committed);
  884. if (error)
  885. goto out_bmap_cancel;
  886. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  887. if (error)
  888. goto out_release_inode;
  889. xfs_qm_dqrele(udqp);
  890. xfs_qm_dqrele(gdqp);
  891. *ipp = ip;
  892. return 0;
  893. out_bmap_cancel:
  894. xfs_bmap_cancel(&free_list);
  895. out_trans_abort:
  896. cancel_flags |= XFS_TRANS_ABORT;
  897. out_trans_cancel:
  898. xfs_trans_cancel(tp, cancel_flags);
  899. out_release_inode:
  900. /*
  901. * Wait until after the current transaction is aborted to
  902. * release the inode. This prevents recursive transactions
  903. * and deadlocks from xfs_inactive.
  904. */
  905. if (ip)
  906. IRELE(ip);
  907. xfs_qm_dqrele(udqp);
  908. xfs_qm_dqrele(gdqp);
  909. if (unlock_dp_on_error)
  910. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  911. return error;
  912. }
  913. #ifdef DEBUG
  914. int xfs_locked_n;
  915. int xfs_small_retries;
  916. int xfs_middle_retries;
  917. int xfs_lots_retries;
  918. int xfs_lock_delays;
  919. #endif
  920. /*
  921. * Bump the subclass so xfs_lock_inodes() acquires each lock with
  922. * a different value
  923. */
  924. static inline int
  925. xfs_lock_inumorder(int lock_mode, int subclass)
  926. {
  927. if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
  928. lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
  929. if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
  930. lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
  931. return lock_mode;
  932. }
  933. /*
  934. * The following routine will lock n inodes in exclusive mode.
  935. * We assume the caller calls us with the inodes in i_ino order.
  936. *
  937. * We need to detect deadlock where an inode that we lock
  938. * is in the AIL and we start waiting for another inode that is locked
  939. * by a thread in a long running transaction (such as truncate). This can
  940. * result in deadlock since the long running trans might need to wait
  941. * for the inode we just locked in order to push the tail and free space
  942. * in the log.
  943. */
  944. void
  945. xfs_lock_inodes(
  946. xfs_inode_t **ips,
  947. int inodes,
  948. uint lock_mode)
  949. {
  950. int attempts = 0, i, j, try_lock;
  951. xfs_log_item_t *lp;
  952. ASSERT(ips && (inodes >= 2)); /* we need at least two */
  953. try_lock = 0;
  954. i = 0;
  955. again:
  956. for (; i < inodes; i++) {
  957. ASSERT(ips[i]);
  958. if (i && (ips[i] == ips[i-1])) /* Already locked */
  959. continue;
  960. /*
  961. * If try_lock is not set yet, make sure all locked inodes
  962. * are not in the AIL.
  963. * If any are, set try_lock to be used later.
  964. */
  965. if (!try_lock) {
  966. for (j = (i - 1); j >= 0 && !try_lock; j--) {
  967. lp = (xfs_log_item_t *)ips[j]->i_itemp;
  968. if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
  969. try_lock++;
  970. }
  971. }
  972. }
  973. /*
  974. * If any of the previous locks we have locked is in the AIL,
  975. * we must TRY to get the second and subsequent locks. If
  976. * we can't get any, we must release all we have
  977. * and try again.
  978. */
  979. if (try_lock) {
  980. /* try_lock must be 0 if i is 0. */
  981. /*
  982. * try_lock means we have an inode locked
  983. * that is in the AIL.
  984. */
  985. ASSERT(i != 0);
  986. if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
  987. attempts++;
  988. /*
  989. * Unlock all previous guys and try again.
  990. * xfs_iunlock will try to push the tail
  991. * if the inode is in the AIL.
  992. */
  993. for(j = i - 1; j >= 0; j--) {
  994. /*
  995. * Check to see if we've already
  996. * unlocked this one.
  997. * Not the first one going back,
  998. * and the inode ptr is the same.
  999. */
  1000. if ((j != (i - 1)) && ips[j] ==
  1001. ips[j+1])
  1002. continue;
  1003. xfs_iunlock(ips[j], lock_mode);
  1004. }
  1005. if ((attempts % 5) == 0) {
  1006. delay(1); /* Don't just spin the CPU */
  1007. #ifdef DEBUG
  1008. xfs_lock_delays++;
  1009. #endif
  1010. }
  1011. i = 0;
  1012. try_lock = 0;
  1013. goto again;
  1014. }
  1015. } else {
  1016. xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
  1017. }
  1018. }
  1019. #ifdef DEBUG
  1020. if (attempts) {
  1021. if (attempts < 5) xfs_small_retries++;
  1022. else if (attempts < 100) xfs_middle_retries++;
  1023. else xfs_lots_retries++;
  1024. } else {
  1025. xfs_locked_n++;
  1026. }
  1027. #endif
  1028. }
  1029. /*
  1030. * xfs_lock_two_inodes() can only be used to lock one type of lock
  1031. * at a time - the iolock or the ilock, but not both at once. If
  1032. * we lock both at once, lockdep will report false positives saying
  1033. * we have violated locking orders.
  1034. */
  1035. void
  1036. xfs_lock_two_inodes(
  1037. xfs_inode_t *ip0,
  1038. xfs_inode_t *ip1,
  1039. uint lock_mode)
  1040. {
  1041. xfs_inode_t *temp;
  1042. int attempts = 0;
  1043. xfs_log_item_t *lp;
  1044. if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
  1045. ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
  1046. ASSERT(ip0->i_ino != ip1->i_ino);
  1047. if (ip0->i_ino > ip1->i_ino) {
  1048. temp = ip0;
  1049. ip0 = ip1;
  1050. ip1 = temp;
  1051. }
  1052. again:
  1053. xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
  1054. /*
  1055. * If the first lock we have locked is in the AIL, we must TRY to get
  1056. * the second lock. If we can't get it, we must release the first one
  1057. * and try again.
  1058. */
  1059. lp = (xfs_log_item_t *)ip0->i_itemp;
  1060. if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
  1061. if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
  1062. xfs_iunlock(ip0, lock_mode);
  1063. if ((++attempts % 5) == 0)
  1064. delay(1); /* Don't just spin the CPU */
  1065. goto again;
  1066. }
  1067. } else {
  1068. xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
  1069. }
  1070. }
  1071. int
  1072. xfs_remove(
  1073. xfs_inode_t *dp,
  1074. struct xfs_name *name,
  1075. xfs_inode_t *ip)
  1076. {
  1077. xfs_mount_t *mp = dp->i_mount;
  1078. xfs_trans_t *tp = NULL;
  1079. int is_dir = S_ISDIR(ip->i_d.di_mode);
  1080. int error = 0;
  1081. xfs_bmap_free_t free_list;
  1082. xfs_fsblock_t first_block;
  1083. int cancel_flags;
  1084. int committed;
  1085. int link_zero;
  1086. uint resblks;
  1087. uint log_count;
  1088. trace_xfs_remove(dp, name);
  1089. if (XFS_FORCED_SHUTDOWN(mp))
  1090. return XFS_ERROR(EIO);
  1091. error = xfs_qm_dqattach(dp, 0);
  1092. if (error)
  1093. goto std_return;
  1094. error = xfs_qm_dqattach(ip, 0);
  1095. if (error)
  1096. goto std_return;
  1097. if (is_dir) {
  1098. tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
  1099. log_count = XFS_DEFAULT_LOG_COUNT;
  1100. } else {
  1101. tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
  1102. log_count = XFS_REMOVE_LOG_COUNT;
  1103. }
  1104. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  1105. /*
  1106. * We try to get the real space reservation first,
  1107. * allowing for directory btree deletion(s) implying
  1108. * possible bmap insert(s). If we can't get the space
  1109. * reservation then we use 0 instead, and avoid the bmap
  1110. * btree insert(s) in the directory code by, if the bmap
  1111. * insert tries to happen, instead trimming the LAST
  1112. * block from the directory.
  1113. */
  1114. resblks = XFS_REMOVE_SPACE_RES(mp);
  1115. error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
  1116. XFS_TRANS_PERM_LOG_RES, log_count);
  1117. if (error == ENOSPC) {
  1118. resblks = 0;
  1119. error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
  1120. XFS_TRANS_PERM_LOG_RES, log_count);
  1121. }
  1122. if (error) {
  1123. ASSERT(error != ENOSPC);
  1124. cancel_flags = 0;
  1125. goto out_trans_cancel;
  1126. }
  1127. xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
  1128. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  1129. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  1130. /*
  1131. * If we're removing a directory perform some additional validation.
  1132. */
  1133. if (is_dir) {
  1134. ASSERT(ip->i_d.di_nlink >= 2);
  1135. if (ip->i_d.di_nlink != 2) {
  1136. error = XFS_ERROR(ENOTEMPTY);
  1137. goto out_trans_cancel;
  1138. }
  1139. if (!xfs_dir_isempty(ip)) {
  1140. error = XFS_ERROR(ENOTEMPTY);
  1141. goto out_trans_cancel;
  1142. }
  1143. }
  1144. xfs_bmap_init(&free_list, &first_block);
  1145. error = xfs_dir_removename(tp, dp, name, ip->i_ino,
  1146. &first_block, &free_list, resblks);
  1147. if (error) {
  1148. ASSERT(error != ENOENT);
  1149. goto out_bmap_cancel;
  1150. }
  1151. xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1152. if (is_dir) {
  1153. /*
  1154. * Drop the link from ip's "..".
  1155. */
  1156. error = xfs_droplink(tp, dp);
  1157. if (error)
  1158. goto out_bmap_cancel;
  1159. /*
  1160. * Drop the "." link from ip to self.
  1161. */
  1162. error = xfs_droplink(tp, ip);
  1163. if (error)
  1164. goto out_bmap_cancel;
  1165. } else {
  1166. /*
  1167. * When removing a non-directory we need to log the parent
  1168. * inode here. For a directory this is done implicitly
  1169. * by the xfs_droplink call for the ".." entry.
  1170. */
  1171. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  1172. }
  1173. /*
  1174. * Drop the link from dp to ip.
  1175. */
  1176. error = xfs_droplink(tp, ip);
  1177. if (error)
  1178. goto out_bmap_cancel;
  1179. /*
  1180. * Determine if this is the last link while
  1181. * we are in the transaction.
  1182. */
  1183. link_zero = (ip->i_d.di_nlink == 0);
  1184. /*
  1185. * If this is a synchronous mount, make sure that the
  1186. * remove transaction goes to disk before returning to
  1187. * the user.
  1188. */
  1189. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
  1190. xfs_trans_set_sync(tp);
  1191. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1192. if (error)
  1193. goto out_bmap_cancel;
  1194. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1195. if (error)
  1196. goto std_return;
  1197. /*
  1198. * If we are using filestreams, kill the stream association.
  1199. * If the file is still open it may get a new one but that
  1200. * will get killed on last close in xfs_close() so we don't
  1201. * have to worry about that.
  1202. */
  1203. if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
  1204. xfs_filestream_deassociate(ip);
  1205. return 0;
  1206. out_bmap_cancel:
  1207. xfs_bmap_cancel(&free_list);
  1208. cancel_flags |= XFS_TRANS_ABORT;
  1209. out_trans_cancel:
  1210. xfs_trans_cancel(tp, cancel_flags);
  1211. std_return:
  1212. return error;
  1213. }
  1214. int
  1215. xfs_link(
  1216. xfs_inode_t *tdp,
  1217. xfs_inode_t *sip,
  1218. struct xfs_name *target_name)
  1219. {
  1220. xfs_mount_t *mp = tdp->i_mount;
  1221. xfs_trans_t *tp;
  1222. int error;
  1223. xfs_bmap_free_t free_list;
  1224. xfs_fsblock_t first_block;
  1225. int cancel_flags;
  1226. int committed;
  1227. int resblks;
  1228. trace_xfs_link(tdp, target_name);
  1229. ASSERT(!S_ISDIR(sip->i_d.di_mode));
  1230. if (XFS_FORCED_SHUTDOWN(mp))
  1231. return XFS_ERROR(EIO);
  1232. error = xfs_qm_dqattach(sip, 0);
  1233. if (error)
  1234. goto std_return;
  1235. error = xfs_qm_dqattach(tdp, 0);
  1236. if (error)
  1237. goto std_return;
  1238. tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
  1239. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  1240. resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
  1241. error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
  1242. XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
  1243. if (error == ENOSPC) {
  1244. resblks = 0;
  1245. error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
  1246. XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
  1247. }
  1248. if (error) {
  1249. cancel_flags = 0;
  1250. goto error_return;
  1251. }
  1252. xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
  1253. xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
  1254. xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
  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. umode_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(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_write(tp, ip, first_fsb, fs_blocks,
  1443. XFS_BMAPI_METADATA, &first_block, resblks,
  1444. mval, &nmaps, &free_list);
  1445. if (error)
  1446. goto error2;
  1447. if (resblks)
  1448. resblks -= fs_blocks;
  1449. ip->i_d.di_size = pathlen;
  1450. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1451. cur_chunk = target_path;
  1452. for (n = 0; n < nmaps; n++) {
  1453. d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
  1454. byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
  1455. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
  1456. BTOBB(byte_cnt), 0);
  1457. if (!bp) {
  1458. error = ENOMEM;
  1459. goto error2;
  1460. }
  1461. if (pathlen < byte_cnt) {
  1462. byte_cnt = pathlen;
  1463. }
  1464. pathlen -= byte_cnt;
  1465. memcpy(bp->b_addr, cur_chunk, byte_cnt);
  1466. cur_chunk += byte_cnt;
  1467. xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
  1468. }
  1469. }
  1470. /*
  1471. * Create the directory entry for the symlink.
  1472. */
  1473. error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
  1474. &first_block, &free_list, resblks);
  1475. if (error)
  1476. goto error2;
  1477. xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1478. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  1479. /*
  1480. * If this is a synchronous mount, make sure that the
  1481. * symlink transaction goes to disk before returning to
  1482. * the user.
  1483. */
  1484. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  1485. xfs_trans_set_sync(tp);
  1486. }
  1487. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1488. if (error) {
  1489. goto error2;
  1490. }
  1491. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1492. xfs_qm_dqrele(udqp);
  1493. xfs_qm_dqrele(gdqp);
  1494. *ipp = ip;
  1495. return 0;
  1496. error2:
  1497. IRELE(ip);
  1498. error1:
  1499. xfs_bmap_cancel(&free_list);
  1500. cancel_flags |= XFS_TRANS_ABORT;
  1501. error_return:
  1502. xfs_trans_cancel(tp, cancel_flags);
  1503. xfs_qm_dqrele(udqp);
  1504. xfs_qm_dqrele(gdqp);
  1505. if (unlock_dp_on_error)
  1506. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  1507. std_return:
  1508. return error;
  1509. }
  1510. int
  1511. xfs_set_dmattrs(
  1512. xfs_inode_t *ip,
  1513. u_int evmask,
  1514. u_int16_t state)
  1515. {
  1516. xfs_mount_t *mp = ip->i_mount;
  1517. xfs_trans_t *tp;
  1518. int error;
  1519. if (!capable(CAP_SYS_ADMIN))
  1520. return XFS_ERROR(EPERM);
  1521. if (XFS_FORCED_SHUTDOWN(mp))
  1522. return XFS_ERROR(EIO);
  1523. tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
  1524. error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
  1525. if (error) {
  1526. xfs_trans_cancel(tp, 0);
  1527. return error;
  1528. }
  1529. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1530. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  1531. ip->i_d.di_dmevmask = evmask;
  1532. ip->i_d.di_dmstate = state;
  1533. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1534. error = xfs_trans_commit(tp, 0);
  1535. return error;
  1536. }
  1537. /*
  1538. * xfs_alloc_file_space()
  1539. * This routine allocates disk space for the given file.
  1540. *
  1541. * If alloc_type == 0, this request is for an ALLOCSP type
  1542. * request which will change the file size. In this case, no
  1543. * DMAPI event will be generated by the call. A TRUNCATE event
  1544. * will be generated later by xfs_setattr.
  1545. *
  1546. * If alloc_type != 0, this request is for a RESVSP type
  1547. * request, and a DMAPI DM_EVENT_WRITE will be generated if the
  1548. * lower block boundary byte address is less than the file's
  1549. * length.
  1550. *
  1551. * RETURNS:
  1552. * 0 on success
  1553. * errno on error
  1554. *
  1555. */
  1556. STATIC int
  1557. xfs_alloc_file_space(
  1558. xfs_inode_t *ip,
  1559. xfs_off_t offset,
  1560. xfs_off_t len,
  1561. int alloc_type,
  1562. int attr_flags)
  1563. {
  1564. xfs_mount_t *mp = ip->i_mount;
  1565. xfs_off_t count;
  1566. xfs_filblks_t allocated_fsb;
  1567. xfs_filblks_t allocatesize_fsb;
  1568. xfs_extlen_t extsz, temp;
  1569. xfs_fileoff_t startoffset_fsb;
  1570. xfs_fsblock_t firstfsb;
  1571. int nimaps;
  1572. int quota_flag;
  1573. int rt;
  1574. xfs_trans_t *tp;
  1575. xfs_bmbt_irec_t imaps[1], *imapp;
  1576. xfs_bmap_free_t free_list;
  1577. uint qblocks, resblks, resrtextents;
  1578. int committed;
  1579. int error;
  1580. trace_xfs_alloc_file_space(ip);
  1581. if (XFS_FORCED_SHUTDOWN(mp))
  1582. return XFS_ERROR(EIO);
  1583. error = xfs_qm_dqattach(ip, 0);
  1584. if (error)
  1585. return error;
  1586. if (len <= 0)
  1587. return XFS_ERROR(EINVAL);
  1588. rt = XFS_IS_REALTIME_INODE(ip);
  1589. extsz = xfs_get_extsz_hint(ip);
  1590. count = len;
  1591. imapp = &imaps[0];
  1592. nimaps = 1;
  1593. startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
  1594. allocatesize_fsb = XFS_B_TO_FSB(mp, count);
  1595. /*
  1596. * Allocate file space until done or until there is an error
  1597. */
  1598. while (allocatesize_fsb && !error) {
  1599. xfs_fileoff_t s, e;
  1600. /*
  1601. * Determine space reservations for data/realtime.
  1602. */
  1603. if (unlikely(extsz)) {
  1604. s = startoffset_fsb;
  1605. do_div(s, extsz);
  1606. s *= extsz;
  1607. e = startoffset_fsb + allocatesize_fsb;
  1608. if ((temp = do_mod(startoffset_fsb, extsz)))
  1609. e += temp;
  1610. if ((temp = do_mod(e, extsz)))
  1611. e += extsz - temp;
  1612. } else {
  1613. s = 0;
  1614. e = allocatesize_fsb;
  1615. }
  1616. /*
  1617. * The transaction reservation is limited to a 32-bit block
  1618. * count, hence we need to limit the number of blocks we are
  1619. * trying to reserve to avoid an overflow. We can't allocate
  1620. * more than @nimaps extents, and an extent is limited on disk
  1621. * to MAXEXTLEN (21 bits), so use that to enforce the limit.
  1622. */
  1623. resblks = min_t(xfs_fileoff_t, (e - s), (MAXEXTLEN * nimaps));
  1624. if (unlikely(rt)) {
  1625. resrtextents = qblocks = resblks;
  1626. resrtextents /= mp->m_sb.sb_rextsize;
  1627. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  1628. quota_flag = XFS_QMOPT_RES_RTBLKS;
  1629. } else {
  1630. resrtextents = 0;
  1631. resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
  1632. quota_flag = XFS_QMOPT_RES_REGBLKS;
  1633. }
  1634. /*
  1635. * Allocate and setup the transaction.
  1636. */
  1637. tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
  1638. error = xfs_trans_reserve(tp, resblks,
  1639. XFS_WRITE_LOG_RES(mp), resrtextents,
  1640. XFS_TRANS_PERM_LOG_RES,
  1641. XFS_WRITE_LOG_COUNT);
  1642. /*
  1643. * Check for running out of space
  1644. */
  1645. if (error) {
  1646. /*
  1647. * Free the transaction structure.
  1648. */
  1649. ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  1650. xfs_trans_cancel(tp, 0);
  1651. break;
  1652. }
  1653. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1654. error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
  1655. 0, quota_flag);
  1656. if (error)
  1657. goto error1;
  1658. xfs_trans_ijoin(tp, ip, 0);
  1659. xfs_bmap_init(&free_list, &firstfsb);
  1660. error = xfs_bmapi_write(tp, ip, startoffset_fsb,
  1661. allocatesize_fsb, alloc_type, &firstfsb,
  1662. 0, imapp, &nimaps, &free_list);
  1663. if (error) {
  1664. goto error0;
  1665. }
  1666. /*
  1667. * Complete the transaction
  1668. */
  1669. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1670. if (error) {
  1671. goto error0;
  1672. }
  1673. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1674. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1675. if (error) {
  1676. break;
  1677. }
  1678. allocated_fsb = imapp->br_blockcount;
  1679. if (nimaps == 0) {
  1680. error = XFS_ERROR(ENOSPC);
  1681. break;
  1682. }
  1683. startoffset_fsb += allocated_fsb;
  1684. allocatesize_fsb -= allocated_fsb;
  1685. }
  1686. return error;
  1687. error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
  1688. xfs_bmap_cancel(&free_list);
  1689. xfs_trans_unreserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
  1690. error1: /* Just cancel transaction */
  1691. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  1692. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1693. return error;
  1694. }
  1695. /*
  1696. * Zero file bytes between startoff and endoff inclusive.
  1697. * The iolock is held exclusive and no blocks are buffered.
  1698. *
  1699. * This function is used by xfs_free_file_space() to zero
  1700. * partial blocks when the range to free is not block aligned.
  1701. * When unreserving space with boundaries that are not block
  1702. * aligned we round up the start and round down the end
  1703. * boundaries and then use this function to zero the parts of
  1704. * the blocks that got dropped during the rounding.
  1705. */
  1706. STATIC int
  1707. xfs_zero_remaining_bytes(
  1708. xfs_inode_t *ip,
  1709. xfs_off_t startoff,
  1710. xfs_off_t endoff)
  1711. {
  1712. xfs_bmbt_irec_t imap;
  1713. xfs_fileoff_t offset_fsb;
  1714. xfs_off_t lastoffset;
  1715. xfs_off_t offset;
  1716. xfs_buf_t *bp;
  1717. xfs_mount_t *mp = ip->i_mount;
  1718. int nimap;
  1719. int error = 0;
  1720. /*
  1721. * Avoid doing I/O beyond eof - it's not necessary
  1722. * since nothing can read beyond eof. The space will
  1723. * be zeroed when the file is extended anyway.
  1724. */
  1725. if (startoff >= XFS_ISIZE(ip))
  1726. return 0;
  1727. if (endoff > XFS_ISIZE(ip))
  1728. endoff = XFS_ISIZE(ip);
  1729. bp = xfs_buf_get_uncached(XFS_IS_REALTIME_INODE(ip) ?
  1730. mp->m_rtdev_targp : mp->m_ddev_targp,
  1731. mp->m_sb.sb_blocksize, XBF_DONT_BLOCK);
  1732. if (!bp)
  1733. return XFS_ERROR(ENOMEM);
  1734. xfs_buf_unlock(bp);
  1735. for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
  1736. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  1737. nimap = 1;
  1738. error = xfs_bmapi_read(ip, offset_fsb, 1, &imap, &nimap, 0);
  1739. if (error || nimap < 1)
  1740. break;
  1741. ASSERT(imap.br_blockcount >= 1);
  1742. ASSERT(imap.br_startoff == offset_fsb);
  1743. lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
  1744. if (lastoffset > endoff)
  1745. lastoffset = endoff;
  1746. if (imap.br_startblock == HOLESTARTBLOCK)
  1747. continue;
  1748. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  1749. if (imap.br_state == XFS_EXT_UNWRITTEN)
  1750. continue;
  1751. XFS_BUF_UNDONE(bp);
  1752. XFS_BUF_UNWRITE(bp);
  1753. XFS_BUF_READ(bp);
  1754. XFS_BUF_SET_ADDR(bp, xfs_fsb_to_db(ip, imap.br_startblock));
  1755. xfsbdstrat(mp, bp);
  1756. error = xfs_buf_iowait(bp);
  1757. if (error) {
  1758. xfs_buf_ioerror_alert(bp,
  1759. "xfs_zero_remaining_bytes(read)");
  1760. break;
  1761. }
  1762. memset(bp->b_addr +
  1763. (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
  1764. 0, lastoffset - offset + 1);
  1765. XFS_BUF_UNDONE(bp);
  1766. XFS_BUF_UNREAD(bp);
  1767. XFS_BUF_WRITE(bp);
  1768. xfsbdstrat(mp, bp);
  1769. error = xfs_buf_iowait(bp);
  1770. if (error) {
  1771. xfs_buf_ioerror_alert(bp,
  1772. "xfs_zero_remaining_bytes(write)");
  1773. break;
  1774. }
  1775. }
  1776. xfs_buf_free(bp);
  1777. return error;
  1778. }
  1779. /*
  1780. * xfs_free_file_space()
  1781. * This routine frees disk space for the given file.
  1782. *
  1783. * This routine is only called by xfs_change_file_space
  1784. * for an UNRESVSP type call.
  1785. *
  1786. * RETURNS:
  1787. * 0 on success
  1788. * errno on error
  1789. *
  1790. */
  1791. STATIC int
  1792. xfs_free_file_space(
  1793. xfs_inode_t *ip,
  1794. xfs_off_t offset,
  1795. xfs_off_t len,
  1796. int attr_flags)
  1797. {
  1798. int committed;
  1799. int done;
  1800. xfs_fileoff_t endoffset_fsb;
  1801. int error;
  1802. xfs_fsblock_t firstfsb;
  1803. xfs_bmap_free_t free_list;
  1804. xfs_bmbt_irec_t imap;
  1805. xfs_off_t ioffset;
  1806. xfs_extlen_t mod=0;
  1807. xfs_mount_t *mp;
  1808. int nimap;
  1809. uint resblks;
  1810. uint rounding;
  1811. int rt;
  1812. xfs_fileoff_t startoffset_fsb;
  1813. xfs_trans_t *tp;
  1814. int need_iolock = 1;
  1815. mp = ip->i_mount;
  1816. trace_xfs_free_file_space(ip);
  1817. error = xfs_qm_dqattach(ip, 0);
  1818. if (error)
  1819. return error;
  1820. error = 0;
  1821. if (len <= 0) /* if nothing being freed */
  1822. return error;
  1823. rt = XFS_IS_REALTIME_INODE(ip);
  1824. startoffset_fsb = XFS_B_TO_FSB(mp, offset);
  1825. endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
  1826. if (attr_flags & XFS_ATTR_NOLOCK)
  1827. need_iolock = 0;
  1828. if (need_iolock) {
  1829. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  1830. /* wait for the completion of any pending DIOs */
  1831. inode_dio_wait(VFS_I(ip));
  1832. }
  1833. rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
  1834. ioffset = offset & ~(rounding - 1);
  1835. if (VN_CACHED(VFS_I(ip)) != 0) {
  1836. error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
  1837. if (error)
  1838. goto out_unlock_iolock;
  1839. }
  1840. /*
  1841. * Need to zero the stuff we're not freeing, on disk.
  1842. * If it's a realtime file & can't use unwritten extents then we
  1843. * actually need to zero the extent edges. Otherwise xfs_bunmapi
  1844. * will take care of it for us.
  1845. */
  1846. if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
  1847. nimap = 1;
  1848. error = xfs_bmapi_read(ip, startoffset_fsb, 1,
  1849. &imap, &nimap, 0);
  1850. if (error)
  1851. goto out_unlock_iolock;
  1852. ASSERT(nimap == 0 || nimap == 1);
  1853. if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
  1854. xfs_daddr_t block;
  1855. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  1856. block = imap.br_startblock;
  1857. mod = do_div(block, mp->m_sb.sb_rextsize);
  1858. if (mod)
  1859. startoffset_fsb += mp->m_sb.sb_rextsize - mod;
  1860. }
  1861. nimap = 1;
  1862. error = xfs_bmapi_read(ip, endoffset_fsb - 1, 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. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  1869. mod++;
  1870. if (mod && (mod != mp->m_sb.sb_rextsize))
  1871. endoffset_fsb -= mod;
  1872. }
  1873. }
  1874. if ((done = (endoffset_fsb <= startoffset_fsb)))
  1875. /*
  1876. * One contiguous piece to clear
  1877. */
  1878. error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
  1879. else {
  1880. /*
  1881. * Some full blocks, possibly two pieces to clear
  1882. */
  1883. if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
  1884. error = xfs_zero_remaining_bytes(ip, offset,
  1885. XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
  1886. if (!error &&
  1887. XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
  1888. error = xfs_zero_remaining_bytes(ip,
  1889. XFS_FSB_TO_B(mp, endoffset_fsb),
  1890. offset + len - 1);
  1891. }
  1892. /*
  1893. * free file space until done or until there is an error
  1894. */
  1895. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  1896. while (!error && !done) {
  1897. /*
  1898. * allocate and setup the transaction. Allow this
  1899. * transaction to dip into the reserve blocks to ensure
  1900. * the freeing of the space succeeds at ENOSPC.
  1901. */
  1902. tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
  1903. tp->t_flags |= XFS_TRANS_RESERVE;
  1904. error = xfs_trans_reserve(tp,
  1905. resblks,
  1906. XFS_WRITE_LOG_RES(mp),
  1907. 0,
  1908. XFS_TRANS_PERM_LOG_RES,
  1909. XFS_WRITE_LOG_COUNT);
  1910. /*
  1911. * check for running out of space
  1912. */
  1913. if (error) {
  1914. /*
  1915. * Free the transaction structure.
  1916. */
  1917. ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  1918. xfs_trans_cancel(tp, 0);
  1919. break;
  1920. }
  1921. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1922. error = xfs_trans_reserve_quota(tp, mp,
  1923. ip->i_udquot, ip->i_gdquot,
  1924. resblks, 0, XFS_QMOPT_RES_REGBLKS);
  1925. if (error)
  1926. goto error1;
  1927. xfs_trans_ijoin(tp, ip, 0);
  1928. /*
  1929. * issue the bunmapi() call to free the blocks
  1930. */
  1931. xfs_bmap_init(&free_list, &firstfsb);
  1932. error = xfs_bunmapi(tp, ip, startoffset_fsb,
  1933. endoffset_fsb - startoffset_fsb,
  1934. 0, 2, &firstfsb, &free_list, &done);
  1935. if (error) {
  1936. goto error0;
  1937. }
  1938. /*
  1939. * complete the transaction
  1940. */
  1941. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1942. if (error) {
  1943. goto error0;
  1944. }
  1945. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1946. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1947. }
  1948. out_unlock_iolock:
  1949. if (need_iolock)
  1950. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1951. return error;
  1952. error0:
  1953. xfs_bmap_cancel(&free_list);
  1954. error1:
  1955. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  1956. xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
  1957. XFS_ILOCK_EXCL);
  1958. return error;
  1959. }
  1960. /*
  1961. * xfs_change_file_space()
  1962. * This routine allocates or frees disk space for the given file.
  1963. * The user specified parameters are checked for alignment and size
  1964. * limitations.
  1965. *
  1966. * RETURNS:
  1967. * 0 on success
  1968. * errno on error
  1969. *
  1970. */
  1971. int
  1972. xfs_change_file_space(
  1973. xfs_inode_t *ip,
  1974. int cmd,
  1975. xfs_flock64_t *bf,
  1976. xfs_off_t offset,
  1977. int attr_flags)
  1978. {
  1979. xfs_mount_t *mp = ip->i_mount;
  1980. int clrprealloc;
  1981. int error;
  1982. xfs_fsize_t fsize;
  1983. int setprealloc;
  1984. xfs_off_t startoffset;
  1985. xfs_off_t llen;
  1986. xfs_trans_t *tp;
  1987. struct iattr iattr;
  1988. int prealloc_type;
  1989. if (!S_ISREG(ip->i_d.di_mode))
  1990. return XFS_ERROR(EINVAL);
  1991. switch (bf->l_whence) {
  1992. case 0: /*SEEK_SET*/
  1993. break;
  1994. case 1: /*SEEK_CUR*/
  1995. bf->l_start += offset;
  1996. break;
  1997. case 2: /*SEEK_END*/
  1998. bf->l_start += XFS_ISIZE(ip);
  1999. break;
  2000. default:
  2001. return XFS_ERROR(EINVAL);
  2002. }
  2003. llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
  2004. if ( (bf->l_start < 0)
  2005. || (bf->l_start > XFS_MAXIOFFSET(mp))
  2006. || (bf->l_start + llen < 0)
  2007. || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
  2008. return XFS_ERROR(EINVAL);
  2009. bf->l_whence = 0;
  2010. startoffset = bf->l_start;
  2011. fsize = XFS_ISIZE(ip);
  2012. /*
  2013. * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
  2014. * file space.
  2015. * These calls do NOT zero the data space allocated to the file,
  2016. * nor do they change the file size.
  2017. *
  2018. * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
  2019. * space.
  2020. * These calls cause the new file data to be zeroed and the file
  2021. * size to be changed.
  2022. */
  2023. setprealloc = clrprealloc = 0;
  2024. prealloc_type = XFS_BMAPI_PREALLOC;
  2025. switch (cmd) {
  2026. case XFS_IOC_ZERO_RANGE:
  2027. prealloc_type |= XFS_BMAPI_CONVERT;
  2028. xfs_tosspages(ip, startoffset, startoffset + bf->l_len, 0);
  2029. /* FALLTHRU */
  2030. case XFS_IOC_RESVSP:
  2031. case XFS_IOC_RESVSP64:
  2032. error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
  2033. prealloc_type, attr_flags);
  2034. if (error)
  2035. return error;
  2036. setprealloc = 1;
  2037. break;
  2038. case XFS_IOC_UNRESVSP:
  2039. case XFS_IOC_UNRESVSP64:
  2040. if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
  2041. attr_flags)))
  2042. return error;
  2043. break;
  2044. case XFS_IOC_ALLOCSP:
  2045. case XFS_IOC_ALLOCSP64:
  2046. case XFS_IOC_FREESP:
  2047. case XFS_IOC_FREESP64:
  2048. if (startoffset > fsize) {
  2049. error = xfs_alloc_file_space(ip, fsize,
  2050. startoffset - fsize, 0, attr_flags);
  2051. if (error)
  2052. break;
  2053. }
  2054. iattr.ia_valid = ATTR_SIZE;
  2055. iattr.ia_size = startoffset;
  2056. error = xfs_setattr_size(ip, &iattr, attr_flags);
  2057. if (error)
  2058. return error;
  2059. clrprealloc = 1;
  2060. break;
  2061. default:
  2062. ASSERT(0);
  2063. return XFS_ERROR(EINVAL);
  2064. }
  2065. /*
  2066. * update the inode timestamp, mode, and prealloc flag bits
  2067. */
  2068. tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
  2069. if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
  2070. 0, 0, 0))) {
  2071. /* ASSERT(0); */
  2072. xfs_trans_cancel(tp, 0);
  2073. return error;
  2074. }
  2075. xfs_ilock(ip, XFS_ILOCK_EXCL);
  2076. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  2077. if ((attr_flags & XFS_ATTR_DMI) == 0) {
  2078. ip->i_d.di_mode &= ~S_ISUID;
  2079. /*
  2080. * Note that we don't have to worry about mandatory
  2081. * file locking being disabled here because we only
  2082. * clear the S_ISGID bit if the Group execute bit is
  2083. * on, but if it was on then mandatory locking wouldn't
  2084. * have been enabled.
  2085. */
  2086. if (ip->i_d.di_mode & S_IXGRP)
  2087. ip->i_d.di_mode &= ~S_ISGID;
  2088. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2089. }
  2090. if (setprealloc)
  2091. ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
  2092. else if (clrprealloc)
  2093. ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
  2094. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  2095. if (attr_flags & XFS_ATTR_SYNC)
  2096. xfs_trans_set_sync(tp);
  2097. return xfs_trans_commit(tp, 0);
  2098. }