xfs_vnodeops.c 58 KB

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