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_trans.h"
  24. #include "xfs_sb.h"
  25. #include "xfs_ag.h"
  26. #include "xfs_dir2.h"
  27. #include "xfs_mount.h"
  28. #include "xfs_da_btree.h"
  29. #include "xfs_bmap_btree.h"
  30. #include "xfs_ialloc_btree.h"
  31. #include "xfs_dinode.h"
  32. #include "xfs_inode.h"
  33. #include "xfs_inode_item.h"
  34. #include "xfs_itable.h"
  35. #include "xfs_ialloc.h"
  36. #include "xfs_alloc.h"
  37. #include "xfs_bmap.h"
  38. #include "xfs_acl.h"
  39. #include "xfs_attr.h"
  40. #include "xfs_rw.h"
  41. #include "xfs_error.h"
  42. #include "xfs_quota.h"
  43. #include "xfs_utils.h"
  44. #include "xfs_rtalloc.h"
  45. #include "xfs_trans_space.h"
  46. #include "xfs_log_priv.h"
  47. #include "xfs_filestream.h"
  48. #include "xfs_vnodeops.h"
  49. #include "xfs_trace.h"
  50. /*
  51. * The maximum pathlen is 1024 bytes. Since the minimum file system
  52. * blocksize is 512 bytes, we can get a max of 2 extents back from
  53. * bmapi.
  54. */
  55. #define SYMLINK_MAPS 2
  56. STATIC int
  57. xfs_readlink_bmap(
  58. xfs_inode_t *ip,
  59. char *link)
  60. {
  61. xfs_mount_t *mp = ip->i_mount;
  62. int pathlen = ip->i_d.di_size;
  63. int nmaps = SYMLINK_MAPS;
  64. xfs_bmbt_irec_t mval[SYMLINK_MAPS];
  65. xfs_daddr_t d;
  66. int byte_cnt;
  67. int n;
  68. xfs_buf_t *bp;
  69. int error = 0;
  70. error = xfs_bmapi_read(ip, 0, XFS_B_TO_FSB(mp, pathlen), mval, &nmaps,
  71. 0);
  72. if (error)
  73. goto out;
  74. for (n = 0; n < nmaps; n++) {
  75. d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
  76. byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
  77. bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt),
  78. XBF_MAPPED);
  79. if (!bp)
  80. return XFS_ERROR(ENOMEM);
  81. error = bp->b_error;
  82. if (error) {
  83. xfs_buf_ioerror_alert(bp, __func__);
  84. xfs_buf_relse(bp);
  85. goto out;
  86. }
  87. if (pathlen < byte_cnt)
  88. byte_cnt = pathlen;
  89. pathlen -= byte_cnt;
  90. memcpy(link, bp->b_addr, byte_cnt);
  91. xfs_buf_relse(bp);
  92. }
  93. link[ip->i_d.di_size] = '\0';
  94. error = 0;
  95. out:
  96. return error;
  97. }
  98. int
  99. xfs_readlink(
  100. xfs_inode_t *ip,
  101. char *link)
  102. {
  103. xfs_mount_t *mp = ip->i_mount;
  104. xfs_fsize_t pathlen;
  105. int error = 0;
  106. trace_xfs_readlink(ip);
  107. if (XFS_FORCED_SHUTDOWN(mp))
  108. return XFS_ERROR(EIO);
  109. xfs_ilock(ip, XFS_ILOCK_SHARED);
  110. pathlen = ip->i_d.di_size;
  111. if (!pathlen)
  112. goto out;
  113. if (pathlen < 0 || pathlen > MAXPATHLEN) {
  114. xfs_alert(mp, "%s: inode (%llu) bad symlink length (%lld)",
  115. __func__, (unsigned long long) ip->i_ino,
  116. (long long) pathlen);
  117. ASSERT(0);
  118. error = XFS_ERROR(EFSCORRUPTED);
  119. goto out;
  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. xfs_bmap_init(&free_list, &first_block);
  822. /*
  823. * Reserve disk quota and the inode.
  824. */
  825. error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
  826. if (error)
  827. goto out_trans_cancel;
  828. error = xfs_dir_canenter(tp, dp, name, resblks);
  829. if (error)
  830. goto out_trans_cancel;
  831. /*
  832. * A newly created regular or special file just has one directory
  833. * entry pointing to them, but a directory also the "." entry
  834. * pointing to itself.
  835. */
  836. error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev,
  837. prid, resblks > 0, &ip, &committed);
  838. if (error) {
  839. if (error == ENOSPC)
  840. goto out_trans_cancel;
  841. goto out_trans_abort;
  842. }
  843. /*
  844. * Now we join the directory inode to the transaction. We do not do it
  845. * earlier because xfs_dir_ialloc might commit the previous transaction
  846. * (and release all the locks). An error from here on will result in
  847. * the transaction cancel unlocking dp so don't do it explicitly in the
  848. * error path.
  849. */
  850. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  851. unlock_dp_on_error = B_FALSE;
  852. error = xfs_dir_createname(tp, dp, name, ip->i_ino,
  853. &first_block, &free_list, resblks ?
  854. resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
  855. if (error) {
  856. ASSERT(error != ENOSPC);
  857. goto out_trans_abort;
  858. }
  859. xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  860. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  861. if (is_dir) {
  862. error = xfs_dir_init(tp, ip, dp);
  863. if (error)
  864. goto out_bmap_cancel;
  865. error = xfs_bumplink(tp, dp);
  866. if (error)
  867. goto out_bmap_cancel;
  868. }
  869. /*
  870. * If this is a synchronous mount, make sure that the
  871. * create transaction goes to disk before returning to
  872. * the user.
  873. */
  874. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
  875. xfs_trans_set_sync(tp);
  876. /*
  877. * Attach the dquot(s) to the inodes and modify them incore.
  878. * These ids of the inode couldn't have changed since the new
  879. * inode has been locked ever since it was created.
  880. */
  881. xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
  882. error = xfs_bmap_finish(&tp, &free_list, &committed);
  883. if (error)
  884. goto out_bmap_cancel;
  885. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  886. if (error)
  887. goto out_release_inode;
  888. xfs_qm_dqrele(udqp);
  889. xfs_qm_dqrele(gdqp);
  890. *ipp = ip;
  891. return 0;
  892. out_bmap_cancel:
  893. xfs_bmap_cancel(&free_list);
  894. out_trans_abort:
  895. cancel_flags |= XFS_TRANS_ABORT;
  896. out_trans_cancel:
  897. xfs_trans_cancel(tp, cancel_flags);
  898. out_release_inode:
  899. /*
  900. * Wait until after the current transaction is aborted to
  901. * release the inode. This prevents recursive transactions
  902. * and deadlocks from xfs_inactive.
  903. */
  904. if (ip)
  905. IRELE(ip);
  906. xfs_qm_dqrele(udqp);
  907. xfs_qm_dqrele(gdqp);
  908. if (unlock_dp_on_error)
  909. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  910. return error;
  911. }
  912. #ifdef DEBUG
  913. int xfs_locked_n;
  914. int xfs_small_retries;
  915. int xfs_middle_retries;
  916. int xfs_lots_retries;
  917. int xfs_lock_delays;
  918. #endif
  919. /*
  920. * Bump the subclass so xfs_lock_inodes() acquires each lock with
  921. * a different value
  922. */
  923. static inline int
  924. xfs_lock_inumorder(int lock_mode, int subclass)
  925. {
  926. if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
  927. lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
  928. if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
  929. lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
  930. return lock_mode;
  931. }
  932. /*
  933. * The following routine will lock n inodes in exclusive mode.
  934. * We assume the caller calls us with the inodes in i_ino order.
  935. *
  936. * We need to detect deadlock where an inode that we lock
  937. * is in the AIL and we start waiting for another inode that is locked
  938. * by a thread in a long running transaction (such as truncate). This can
  939. * result in deadlock since the long running trans might need to wait
  940. * for the inode we just locked in order to push the tail and free space
  941. * in the log.
  942. */
  943. void
  944. xfs_lock_inodes(
  945. xfs_inode_t **ips,
  946. int inodes,
  947. uint lock_mode)
  948. {
  949. int attempts = 0, i, j, try_lock;
  950. xfs_log_item_t *lp;
  951. ASSERT(ips && (inodes >= 2)); /* we need at least two */
  952. try_lock = 0;
  953. i = 0;
  954. again:
  955. for (; i < inodes; i++) {
  956. ASSERT(ips[i]);
  957. if (i && (ips[i] == ips[i-1])) /* Already locked */
  958. continue;
  959. /*
  960. * If try_lock is not set yet, make sure all locked inodes
  961. * are not in the AIL.
  962. * If any are, set try_lock to be used later.
  963. */
  964. if (!try_lock) {
  965. for (j = (i - 1); j >= 0 && !try_lock; j--) {
  966. lp = (xfs_log_item_t *)ips[j]->i_itemp;
  967. if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
  968. try_lock++;
  969. }
  970. }
  971. }
  972. /*
  973. * If any of the previous locks we have locked is in the AIL,
  974. * we must TRY to get the second and subsequent locks. If
  975. * we can't get any, we must release all we have
  976. * and try again.
  977. */
  978. if (try_lock) {
  979. /* try_lock must be 0 if i is 0. */
  980. /*
  981. * try_lock means we have an inode locked
  982. * that is in the AIL.
  983. */
  984. ASSERT(i != 0);
  985. if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
  986. attempts++;
  987. /*
  988. * Unlock all previous guys and try again.
  989. * xfs_iunlock will try to push the tail
  990. * if the inode is in the AIL.
  991. */
  992. for(j = i - 1; j >= 0; j--) {
  993. /*
  994. * Check to see if we've already
  995. * unlocked this one.
  996. * Not the first one going back,
  997. * and the inode ptr is the same.
  998. */
  999. if ((j != (i - 1)) && ips[j] ==
  1000. ips[j+1])
  1001. continue;
  1002. xfs_iunlock(ips[j], lock_mode);
  1003. }
  1004. if ((attempts % 5) == 0) {
  1005. delay(1); /* Don't just spin the CPU */
  1006. #ifdef DEBUG
  1007. xfs_lock_delays++;
  1008. #endif
  1009. }
  1010. i = 0;
  1011. try_lock = 0;
  1012. goto again;
  1013. }
  1014. } else {
  1015. xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
  1016. }
  1017. }
  1018. #ifdef DEBUG
  1019. if (attempts) {
  1020. if (attempts < 5) xfs_small_retries++;
  1021. else if (attempts < 100) xfs_middle_retries++;
  1022. else xfs_lots_retries++;
  1023. } else {
  1024. xfs_locked_n++;
  1025. }
  1026. #endif
  1027. }
  1028. /*
  1029. * xfs_lock_two_inodes() can only be used to lock one type of lock
  1030. * at a time - the iolock or the ilock, but not both at once. If
  1031. * we lock both at once, lockdep will report false positives saying
  1032. * we have violated locking orders.
  1033. */
  1034. void
  1035. xfs_lock_two_inodes(
  1036. xfs_inode_t *ip0,
  1037. xfs_inode_t *ip1,
  1038. uint lock_mode)
  1039. {
  1040. xfs_inode_t *temp;
  1041. int attempts = 0;
  1042. xfs_log_item_t *lp;
  1043. if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
  1044. ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
  1045. ASSERT(ip0->i_ino != ip1->i_ino);
  1046. if (ip0->i_ino > ip1->i_ino) {
  1047. temp = ip0;
  1048. ip0 = ip1;
  1049. ip1 = temp;
  1050. }
  1051. again:
  1052. xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
  1053. /*
  1054. * If the first lock we have locked is in the AIL, we must TRY to get
  1055. * the second lock. If we can't get it, we must release the first one
  1056. * and try again.
  1057. */
  1058. lp = (xfs_log_item_t *)ip0->i_itemp;
  1059. if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
  1060. if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
  1061. xfs_iunlock(ip0, lock_mode);
  1062. if ((++attempts % 5) == 0)
  1063. delay(1); /* Don't just spin the CPU */
  1064. goto again;
  1065. }
  1066. } else {
  1067. xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
  1068. }
  1069. }
  1070. int
  1071. xfs_remove(
  1072. xfs_inode_t *dp,
  1073. struct xfs_name *name,
  1074. xfs_inode_t *ip)
  1075. {
  1076. xfs_mount_t *mp = dp->i_mount;
  1077. xfs_trans_t *tp = NULL;
  1078. int is_dir = S_ISDIR(ip->i_d.di_mode);
  1079. int error = 0;
  1080. xfs_bmap_free_t free_list;
  1081. xfs_fsblock_t first_block;
  1082. int cancel_flags;
  1083. int committed;
  1084. int link_zero;
  1085. uint resblks;
  1086. uint log_count;
  1087. trace_xfs_remove(dp, name);
  1088. if (XFS_FORCED_SHUTDOWN(mp))
  1089. return XFS_ERROR(EIO);
  1090. error = xfs_qm_dqattach(dp, 0);
  1091. if (error)
  1092. goto std_return;
  1093. error = xfs_qm_dqattach(ip, 0);
  1094. if (error)
  1095. goto std_return;
  1096. if (is_dir) {
  1097. tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
  1098. log_count = XFS_DEFAULT_LOG_COUNT;
  1099. } else {
  1100. tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
  1101. log_count = XFS_REMOVE_LOG_COUNT;
  1102. }
  1103. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  1104. /*
  1105. * We try to get the real space reservation first,
  1106. * allowing for directory btree deletion(s) implying
  1107. * possible bmap insert(s). If we can't get the space
  1108. * reservation then we use 0 instead, and avoid the bmap
  1109. * btree insert(s) in the directory code by, if the bmap
  1110. * insert tries to happen, instead trimming the LAST
  1111. * block from the directory.
  1112. */
  1113. resblks = XFS_REMOVE_SPACE_RES(mp);
  1114. error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
  1115. XFS_TRANS_PERM_LOG_RES, log_count);
  1116. if (error == ENOSPC) {
  1117. resblks = 0;
  1118. error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
  1119. XFS_TRANS_PERM_LOG_RES, log_count);
  1120. }
  1121. if (error) {
  1122. ASSERT(error != ENOSPC);
  1123. cancel_flags = 0;
  1124. goto out_trans_cancel;
  1125. }
  1126. xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
  1127. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  1128. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  1129. /*
  1130. * If we're removing a directory perform some additional validation.
  1131. */
  1132. if (is_dir) {
  1133. ASSERT(ip->i_d.di_nlink >= 2);
  1134. if (ip->i_d.di_nlink != 2) {
  1135. error = XFS_ERROR(ENOTEMPTY);
  1136. goto out_trans_cancel;
  1137. }
  1138. if (!xfs_dir_isempty(ip)) {
  1139. error = XFS_ERROR(ENOTEMPTY);
  1140. goto out_trans_cancel;
  1141. }
  1142. }
  1143. xfs_bmap_init(&free_list, &first_block);
  1144. error = xfs_dir_removename(tp, dp, name, ip->i_ino,
  1145. &first_block, &free_list, resblks);
  1146. if (error) {
  1147. ASSERT(error != ENOENT);
  1148. goto out_bmap_cancel;
  1149. }
  1150. xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1151. if (is_dir) {
  1152. /*
  1153. * Drop the link from ip's "..".
  1154. */
  1155. error = xfs_droplink(tp, dp);
  1156. if (error)
  1157. goto out_bmap_cancel;
  1158. /*
  1159. * Drop the "." link from ip to self.
  1160. */
  1161. error = xfs_droplink(tp, ip);
  1162. if (error)
  1163. goto out_bmap_cancel;
  1164. } else {
  1165. /*
  1166. * When removing a non-directory we need to log the parent
  1167. * inode here. For a directory this is done implicitly
  1168. * by the xfs_droplink call for the ".." entry.
  1169. */
  1170. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  1171. }
  1172. /*
  1173. * Drop the link from dp to ip.
  1174. */
  1175. error = xfs_droplink(tp, ip);
  1176. if (error)
  1177. goto out_bmap_cancel;
  1178. /*
  1179. * Determine if this is the last link while
  1180. * we are in the transaction.
  1181. */
  1182. link_zero = (ip->i_d.di_nlink == 0);
  1183. /*
  1184. * If this is a synchronous mount, make sure that the
  1185. * remove transaction goes to disk before returning to
  1186. * the user.
  1187. */
  1188. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
  1189. xfs_trans_set_sync(tp);
  1190. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1191. if (error)
  1192. goto out_bmap_cancel;
  1193. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1194. if (error)
  1195. goto std_return;
  1196. /*
  1197. * If we are using filestreams, kill the stream association.
  1198. * If the file is still open it may get a new one but that
  1199. * will get killed on last close in xfs_close() so we don't
  1200. * have to worry about that.
  1201. */
  1202. if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
  1203. xfs_filestream_deassociate(ip);
  1204. return 0;
  1205. out_bmap_cancel:
  1206. xfs_bmap_cancel(&free_list);
  1207. cancel_flags |= XFS_TRANS_ABORT;
  1208. out_trans_cancel:
  1209. xfs_trans_cancel(tp, cancel_flags);
  1210. std_return:
  1211. return error;
  1212. }
  1213. int
  1214. xfs_link(
  1215. xfs_inode_t *tdp,
  1216. xfs_inode_t *sip,
  1217. struct xfs_name *target_name)
  1218. {
  1219. xfs_mount_t *mp = tdp->i_mount;
  1220. xfs_trans_t *tp;
  1221. int error;
  1222. xfs_bmap_free_t free_list;
  1223. xfs_fsblock_t first_block;
  1224. int cancel_flags;
  1225. int committed;
  1226. int resblks;
  1227. trace_xfs_link(tdp, target_name);
  1228. ASSERT(!S_ISDIR(sip->i_d.di_mode));
  1229. if (XFS_FORCED_SHUTDOWN(mp))
  1230. return XFS_ERROR(EIO);
  1231. error = xfs_qm_dqattach(sip, 0);
  1232. if (error)
  1233. goto std_return;
  1234. error = xfs_qm_dqattach(tdp, 0);
  1235. if (error)
  1236. goto std_return;
  1237. tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
  1238. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  1239. resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
  1240. error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
  1241. XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
  1242. if (error == ENOSPC) {
  1243. resblks = 0;
  1244. error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
  1245. XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
  1246. }
  1247. if (error) {
  1248. cancel_flags = 0;
  1249. goto error_return;
  1250. }
  1251. xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
  1252. xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
  1253. xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
  1254. /*
  1255. * If we are using project inheritance, we only allow hard link
  1256. * creation in our tree when the project IDs are the same; else
  1257. * the tree quota mechanism could be circumvented.
  1258. */
  1259. if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
  1260. (xfs_get_projid(tdp) != xfs_get_projid(sip)))) {
  1261. error = XFS_ERROR(EXDEV);
  1262. goto error_return;
  1263. }
  1264. error = xfs_dir_canenter(tp, tdp, target_name, resblks);
  1265. if (error)
  1266. goto error_return;
  1267. xfs_bmap_init(&free_list, &first_block);
  1268. error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
  1269. &first_block, &free_list, resblks);
  1270. if (error)
  1271. goto abort_return;
  1272. xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1273. xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
  1274. error = xfs_bumplink(tp, sip);
  1275. if (error)
  1276. goto abort_return;
  1277. /*
  1278. * If this is a synchronous mount, make sure that the
  1279. * link transaction goes to disk before returning to
  1280. * the user.
  1281. */
  1282. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  1283. xfs_trans_set_sync(tp);
  1284. }
  1285. error = xfs_bmap_finish (&tp, &free_list, &committed);
  1286. if (error) {
  1287. xfs_bmap_cancel(&free_list);
  1288. goto abort_return;
  1289. }
  1290. return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1291. abort_return:
  1292. cancel_flags |= XFS_TRANS_ABORT;
  1293. error_return:
  1294. xfs_trans_cancel(tp, cancel_flags);
  1295. std_return:
  1296. return error;
  1297. }
  1298. int
  1299. xfs_symlink(
  1300. xfs_inode_t *dp,
  1301. struct xfs_name *link_name,
  1302. const char *target_path,
  1303. umode_t mode,
  1304. xfs_inode_t **ipp)
  1305. {
  1306. xfs_mount_t *mp = dp->i_mount;
  1307. xfs_trans_t *tp;
  1308. xfs_inode_t *ip;
  1309. int error;
  1310. int pathlen;
  1311. xfs_bmap_free_t free_list;
  1312. xfs_fsblock_t first_block;
  1313. boolean_t unlock_dp_on_error = B_FALSE;
  1314. uint cancel_flags;
  1315. int committed;
  1316. xfs_fileoff_t first_fsb;
  1317. xfs_filblks_t fs_blocks;
  1318. int nmaps;
  1319. xfs_bmbt_irec_t mval[SYMLINK_MAPS];
  1320. xfs_daddr_t d;
  1321. const char *cur_chunk;
  1322. int byte_cnt;
  1323. int n;
  1324. xfs_buf_t *bp;
  1325. prid_t prid;
  1326. struct xfs_dquot *udqp, *gdqp;
  1327. uint resblks;
  1328. *ipp = NULL;
  1329. error = 0;
  1330. ip = NULL;
  1331. tp = NULL;
  1332. trace_xfs_symlink(dp, link_name);
  1333. if (XFS_FORCED_SHUTDOWN(mp))
  1334. return XFS_ERROR(EIO);
  1335. /*
  1336. * Check component lengths of the target path name.
  1337. */
  1338. pathlen = strlen(target_path);
  1339. if (pathlen >= MAXPATHLEN) /* total string too long */
  1340. return XFS_ERROR(ENAMETOOLONG);
  1341. udqp = gdqp = NULL;
  1342. if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  1343. prid = xfs_get_projid(dp);
  1344. else
  1345. prid = XFS_PROJID_DEFAULT;
  1346. /*
  1347. * Make sure that we have allocated dquot(s) on disk.
  1348. */
  1349. error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
  1350. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
  1351. if (error)
  1352. goto std_return;
  1353. tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
  1354. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  1355. /*
  1356. * The symlink will fit into the inode data fork?
  1357. * There can't be any attributes so we get the whole variable part.
  1358. */
  1359. if (pathlen <= XFS_LITINO(mp))
  1360. fs_blocks = 0;
  1361. else
  1362. fs_blocks = XFS_B_TO_FSB(mp, pathlen);
  1363. resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
  1364. error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
  1365. XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
  1366. if (error == ENOSPC && fs_blocks == 0) {
  1367. resblks = 0;
  1368. error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
  1369. XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
  1370. }
  1371. if (error) {
  1372. cancel_flags = 0;
  1373. goto error_return;
  1374. }
  1375. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  1376. unlock_dp_on_error = B_TRUE;
  1377. /*
  1378. * Check whether the directory allows new symlinks or not.
  1379. */
  1380. if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
  1381. error = XFS_ERROR(EPERM);
  1382. goto error_return;
  1383. }
  1384. /*
  1385. * Reserve disk quota : blocks and inode.
  1386. */
  1387. error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
  1388. if (error)
  1389. goto error_return;
  1390. /*
  1391. * Check for ability to enter directory entry, if no space reserved.
  1392. */
  1393. error = xfs_dir_canenter(tp, dp, link_name, resblks);
  1394. if (error)
  1395. goto error_return;
  1396. /*
  1397. * Initialize the bmap freelist prior to calling either
  1398. * bmapi or the directory create code.
  1399. */
  1400. xfs_bmap_init(&free_list, &first_block);
  1401. /*
  1402. * Allocate an inode for the symlink.
  1403. */
  1404. error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT), 1, 0,
  1405. prid, resblks > 0, &ip, NULL);
  1406. if (error) {
  1407. if (error == ENOSPC)
  1408. goto error_return;
  1409. goto error1;
  1410. }
  1411. /*
  1412. * An error after we've joined dp to the transaction will result in the
  1413. * transaction cancel unlocking dp so don't do it explicitly in the
  1414. * error path.
  1415. */
  1416. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  1417. unlock_dp_on_error = B_FALSE;
  1418. /*
  1419. * Also attach the dquot(s) to it, if applicable.
  1420. */
  1421. xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
  1422. if (resblks)
  1423. resblks -= XFS_IALLOC_SPACE_RES(mp);
  1424. /*
  1425. * If the symlink will fit into the inode, write it inline.
  1426. */
  1427. if (pathlen <= XFS_IFORK_DSIZE(ip)) {
  1428. xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
  1429. memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
  1430. ip->i_d.di_size = pathlen;
  1431. /*
  1432. * The inode was initially created in extent format.
  1433. */
  1434. ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
  1435. ip->i_df.if_flags |= XFS_IFINLINE;
  1436. ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
  1437. xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
  1438. } else {
  1439. first_fsb = 0;
  1440. nmaps = SYMLINK_MAPS;
  1441. error = xfs_bmapi_write(tp, ip, first_fsb, fs_blocks,
  1442. XFS_BMAPI_METADATA, &first_block, resblks,
  1443. mval, &nmaps, &free_list);
  1444. if (error)
  1445. goto error2;
  1446. if (resblks)
  1447. resblks -= fs_blocks;
  1448. ip->i_d.di_size = pathlen;
  1449. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1450. cur_chunk = target_path;
  1451. for (n = 0; n < nmaps; n++) {
  1452. d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
  1453. byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
  1454. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
  1455. BTOBB(byte_cnt), 0);
  1456. if (!bp) {
  1457. error = ENOMEM;
  1458. goto error2;
  1459. }
  1460. if (pathlen < byte_cnt) {
  1461. byte_cnt = pathlen;
  1462. }
  1463. pathlen -= byte_cnt;
  1464. memcpy(bp->b_addr, cur_chunk, byte_cnt);
  1465. cur_chunk += byte_cnt;
  1466. xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
  1467. }
  1468. }
  1469. /*
  1470. * Create the directory entry for the symlink.
  1471. */
  1472. error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
  1473. &first_block, &free_list, resblks);
  1474. if (error)
  1475. goto error2;
  1476. xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1477. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  1478. /*
  1479. * If this is a synchronous mount, make sure that the
  1480. * symlink transaction goes to disk before returning to
  1481. * the user.
  1482. */
  1483. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  1484. xfs_trans_set_sync(tp);
  1485. }
  1486. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1487. if (error) {
  1488. goto error2;
  1489. }
  1490. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1491. xfs_qm_dqrele(udqp);
  1492. xfs_qm_dqrele(gdqp);
  1493. *ipp = ip;
  1494. return 0;
  1495. error2:
  1496. IRELE(ip);
  1497. error1:
  1498. xfs_bmap_cancel(&free_list);
  1499. cancel_flags |= XFS_TRANS_ABORT;
  1500. error_return:
  1501. xfs_trans_cancel(tp, cancel_flags);
  1502. xfs_qm_dqrele(udqp);
  1503. xfs_qm_dqrele(gdqp);
  1504. if (unlock_dp_on_error)
  1505. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  1506. std_return:
  1507. return error;
  1508. }
  1509. int
  1510. xfs_set_dmattrs(
  1511. xfs_inode_t *ip,
  1512. u_int evmask,
  1513. u_int16_t state)
  1514. {
  1515. xfs_mount_t *mp = ip->i_mount;
  1516. xfs_trans_t *tp;
  1517. int error;
  1518. if (!capable(CAP_SYS_ADMIN))
  1519. return XFS_ERROR(EPERM);
  1520. if (XFS_FORCED_SHUTDOWN(mp))
  1521. return XFS_ERROR(EIO);
  1522. tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
  1523. error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
  1524. if (error) {
  1525. xfs_trans_cancel(tp, 0);
  1526. return error;
  1527. }
  1528. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1529. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  1530. ip->i_d.di_dmevmask = evmask;
  1531. ip->i_d.di_dmstate = state;
  1532. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1533. error = xfs_trans_commit(tp, 0);
  1534. return error;
  1535. }
  1536. /*
  1537. * xfs_alloc_file_space()
  1538. * This routine allocates disk space for the given file.
  1539. *
  1540. * If alloc_type == 0, this request is for an ALLOCSP type
  1541. * request which will change the file size. In this case, no
  1542. * DMAPI event will be generated by the call. A TRUNCATE event
  1543. * will be generated later by xfs_setattr.
  1544. *
  1545. * If alloc_type != 0, this request is for a RESVSP type
  1546. * request, and a DMAPI DM_EVENT_WRITE will be generated if the
  1547. * lower block boundary byte address is less than the file's
  1548. * length.
  1549. *
  1550. * RETURNS:
  1551. * 0 on success
  1552. * errno on error
  1553. *
  1554. */
  1555. STATIC int
  1556. xfs_alloc_file_space(
  1557. xfs_inode_t *ip,
  1558. xfs_off_t offset,
  1559. xfs_off_t len,
  1560. int alloc_type,
  1561. int attr_flags)
  1562. {
  1563. xfs_mount_t *mp = ip->i_mount;
  1564. xfs_off_t count;
  1565. xfs_filblks_t allocated_fsb;
  1566. xfs_filblks_t allocatesize_fsb;
  1567. xfs_extlen_t extsz, temp;
  1568. xfs_fileoff_t startoffset_fsb;
  1569. xfs_fsblock_t firstfsb;
  1570. int nimaps;
  1571. int quota_flag;
  1572. int rt;
  1573. xfs_trans_t *tp;
  1574. xfs_bmbt_irec_t imaps[1], *imapp;
  1575. xfs_bmap_free_t free_list;
  1576. uint qblocks, resblks, resrtextents;
  1577. int committed;
  1578. int error;
  1579. trace_xfs_alloc_file_space(ip);
  1580. if (XFS_FORCED_SHUTDOWN(mp))
  1581. return XFS_ERROR(EIO);
  1582. error = xfs_qm_dqattach(ip, 0);
  1583. if (error)
  1584. return error;
  1585. if (len <= 0)
  1586. return XFS_ERROR(EINVAL);
  1587. rt = XFS_IS_REALTIME_INODE(ip);
  1588. extsz = xfs_get_extsz_hint(ip);
  1589. count = len;
  1590. imapp = &imaps[0];
  1591. nimaps = 1;
  1592. startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
  1593. allocatesize_fsb = XFS_B_TO_FSB(mp, count);
  1594. /*
  1595. * Allocate file space until done or until there is an error
  1596. */
  1597. while (allocatesize_fsb && !error) {
  1598. xfs_fileoff_t s, e;
  1599. /*
  1600. * Determine space reservations for data/realtime.
  1601. */
  1602. if (unlikely(extsz)) {
  1603. s = startoffset_fsb;
  1604. do_div(s, extsz);
  1605. s *= extsz;
  1606. e = startoffset_fsb + allocatesize_fsb;
  1607. if ((temp = do_mod(startoffset_fsb, extsz)))
  1608. e += temp;
  1609. if ((temp = do_mod(e, extsz)))
  1610. e += extsz - temp;
  1611. } else {
  1612. s = 0;
  1613. e = allocatesize_fsb;
  1614. }
  1615. /*
  1616. * The transaction reservation is limited to a 32-bit block
  1617. * count, hence we need to limit the number of blocks we are
  1618. * trying to reserve to avoid an overflow. We can't allocate
  1619. * more than @nimaps extents, and an extent is limited on disk
  1620. * to MAXEXTLEN (21 bits), so use that to enforce the limit.
  1621. */
  1622. resblks = min_t(xfs_fileoff_t, (e - s), (MAXEXTLEN * nimaps));
  1623. if (unlikely(rt)) {
  1624. resrtextents = qblocks = resblks;
  1625. resrtextents /= mp->m_sb.sb_rextsize;
  1626. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  1627. quota_flag = XFS_QMOPT_RES_RTBLKS;
  1628. } else {
  1629. resrtextents = 0;
  1630. resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
  1631. quota_flag = XFS_QMOPT_RES_REGBLKS;
  1632. }
  1633. /*
  1634. * Allocate and setup the transaction.
  1635. */
  1636. tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
  1637. error = xfs_trans_reserve(tp, resblks,
  1638. XFS_WRITE_LOG_RES(mp), resrtextents,
  1639. XFS_TRANS_PERM_LOG_RES,
  1640. XFS_WRITE_LOG_COUNT);
  1641. /*
  1642. * Check for running out of space
  1643. */
  1644. if (error) {
  1645. /*
  1646. * Free the transaction structure.
  1647. */
  1648. ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  1649. xfs_trans_cancel(tp, 0);
  1650. break;
  1651. }
  1652. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1653. error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
  1654. 0, quota_flag);
  1655. if (error)
  1656. goto error1;
  1657. xfs_trans_ijoin(tp, ip, 0);
  1658. xfs_bmap_init(&free_list, &firstfsb);
  1659. error = xfs_bmapi_write(tp, ip, startoffset_fsb,
  1660. allocatesize_fsb, alloc_type, &firstfsb,
  1661. 0, imapp, &nimaps, &free_list);
  1662. if (error) {
  1663. goto error0;
  1664. }
  1665. /*
  1666. * Complete the transaction
  1667. */
  1668. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1669. if (error) {
  1670. goto error0;
  1671. }
  1672. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1673. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1674. if (error) {
  1675. break;
  1676. }
  1677. allocated_fsb = imapp->br_blockcount;
  1678. if (nimaps == 0) {
  1679. error = XFS_ERROR(ENOSPC);
  1680. break;
  1681. }
  1682. startoffset_fsb += allocated_fsb;
  1683. allocatesize_fsb -= allocated_fsb;
  1684. }
  1685. return error;
  1686. error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
  1687. xfs_bmap_cancel(&free_list);
  1688. xfs_trans_unreserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
  1689. error1: /* Just cancel transaction */
  1690. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  1691. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1692. return error;
  1693. }
  1694. /*
  1695. * Zero file bytes between startoff and endoff inclusive.
  1696. * The iolock is held exclusive and no blocks are buffered.
  1697. *
  1698. * This function is used by xfs_free_file_space() to zero
  1699. * partial blocks when the range to free is not block aligned.
  1700. * When unreserving space with boundaries that are not block
  1701. * aligned we round up the start and round down the end
  1702. * boundaries and then use this function to zero the parts of
  1703. * the blocks that got dropped during the rounding.
  1704. */
  1705. STATIC int
  1706. xfs_zero_remaining_bytes(
  1707. xfs_inode_t *ip,
  1708. xfs_off_t startoff,
  1709. xfs_off_t endoff)
  1710. {
  1711. xfs_bmbt_irec_t imap;
  1712. xfs_fileoff_t offset_fsb;
  1713. xfs_off_t lastoffset;
  1714. xfs_off_t offset;
  1715. xfs_buf_t *bp;
  1716. xfs_mount_t *mp = ip->i_mount;
  1717. int nimap;
  1718. int error = 0;
  1719. /*
  1720. * Avoid doing I/O beyond eof - it's not necessary
  1721. * since nothing can read beyond eof. The space will
  1722. * be zeroed when the file is extended anyway.
  1723. */
  1724. if (startoff >= XFS_ISIZE(ip))
  1725. return 0;
  1726. if (endoff > XFS_ISIZE(ip))
  1727. endoff = XFS_ISIZE(ip);
  1728. bp = xfs_buf_get_uncached(XFS_IS_REALTIME_INODE(ip) ?
  1729. mp->m_rtdev_targp : mp->m_ddev_targp,
  1730. BTOBB(mp->m_sb.sb_blocksize), 0);
  1731. if (!bp)
  1732. return XFS_ERROR(ENOMEM);
  1733. xfs_buf_unlock(bp);
  1734. for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
  1735. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  1736. nimap = 1;
  1737. error = xfs_bmapi_read(ip, offset_fsb, 1, &imap, &nimap, 0);
  1738. if (error || nimap < 1)
  1739. break;
  1740. ASSERT(imap.br_blockcount >= 1);
  1741. ASSERT(imap.br_startoff == offset_fsb);
  1742. lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
  1743. if (lastoffset > endoff)
  1744. lastoffset = endoff;
  1745. if (imap.br_startblock == HOLESTARTBLOCK)
  1746. continue;
  1747. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  1748. if (imap.br_state == XFS_EXT_UNWRITTEN)
  1749. continue;
  1750. XFS_BUF_UNDONE(bp);
  1751. XFS_BUF_UNWRITE(bp);
  1752. XFS_BUF_READ(bp);
  1753. XFS_BUF_SET_ADDR(bp, xfs_fsb_to_db(ip, imap.br_startblock));
  1754. xfsbdstrat(mp, bp);
  1755. error = xfs_buf_iowait(bp);
  1756. if (error) {
  1757. xfs_buf_ioerror_alert(bp,
  1758. "xfs_zero_remaining_bytes(read)");
  1759. break;
  1760. }
  1761. memset(bp->b_addr +
  1762. (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
  1763. 0, lastoffset - offset + 1);
  1764. XFS_BUF_UNDONE(bp);
  1765. XFS_BUF_UNREAD(bp);
  1766. XFS_BUF_WRITE(bp);
  1767. xfsbdstrat(mp, bp);
  1768. error = xfs_buf_iowait(bp);
  1769. if (error) {
  1770. xfs_buf_ioerror_alert(bp,
  1771. "xfs_zero_remaining_bytes(write)");
  1772. break;
  1773. }
  1774. }
  1775. xfs_buf_free(bp);
  1776. return error;
  1777. }
  1778. /*
  1779. * xfs_free_file_space()
  1780. * This routine frees disk space for the given file.
  1781. *
  1782. * This routine is only called by xfs_change_file_space
  1783. * for an UNRESVSP type call.
  1784. *
  1785. * RETURNS:
  1786. * 0 on success
  1787. * errno on error
  1788. *
  1789. */
  1790. STATIC int
  1791. xfs_free_file_space(
  1792. xfs_inode_t *ip,
  1793. xfs_off_t offset,
  1794. xfs_off_t len,
  1795. int attr_flags)
  1796. {
  1797. int committed;
  1798. int done;
  1799. xfs_fileoff_t endoffset_fsb;
  1800. int error;
  1801. xfs_fsblock_t firstfsb;
  1802. xfs_bmap_free_t free_list;
  1803. xfs_bmbt_irec_t imap;
  1804. xfs_off_t ioffset;
  1805. xfs_extlen_t mod=0;
  1806. xfs_mount_t *mp;
  1807. int nimap;
  1808. uint resblks;
  1809. uint rounding;
  1810. int rt;
  1811. xfs_fileoff_t startoffset_fsb;
  1812. xfs_trans_t *tp;
  1813. int need_iolock = 1;
  1814. mp = ip->i_mount;
  1815. trace_xfs_free_file_space(ip);
  1816. error = xfs_qm_dqattach(ip, 0);
  1817. if (error)
  1818. return error;
  1819. error = 0;
  1820. if (len <= 0) /* if nothing being freed */
  1821. return error;
  1822. rt = XFS_IS_REALTIME_INODE(ip);
  1823. startoffset_fsb = XFS_B_TO_FSB(mp, offset);
  1824. endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
  1825. if (attr_flags & XFS_ATTR_NOLOCK)
  1826. need_iolock = 0;
  1827. if (need_iolock) {
  1828. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  1829. /* wait for the completion of any pending DIOs */
  1830. inode_dio_wait(VFS_I(ip));
  1831. }
  1832. rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
  1833. ioffset = offset & ~(rounding - 1);
  1834. if (VN_CACHED(VFS_I(ip)) != 0) {
  1835. error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
  1836. if (error)
  1837. goto out_unlock_iolock;
  1838. }
  1839. /*
  1840. * Need to zero the stuff we're not freeing, on disk.
  1841. * If it's a realtime file & can't use unwritten extents then we
  1842. * actually need to zero the extent edges. Otherwise xfs_bunmapi
  1843. * will take care of it for us.
  1844. */
  1845. if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
  1846. nimap = 1;
  1847. error = xfs_bmapi_read(ip, startoffset_fsb, 1,
  1848. &imap, &nimap, 0);
  1849. if (error)
  1850. goto out_unlock_iolock;
  1851. ASSERT(nimap == 0 || nimap == 1);
  1852. if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
  1853. xfs_daddr_t block;
  1854. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  1855. block = imap.br_startblock;
  1856. mod = do_div(block, mp->m_sb.sb_rextsize);
  1857. if (mod)
  1858. startoffset_fsb += mp->m_sb.sb_rextsize - mod;
  1859. }
  1860. nimap = 1;
  1861. error = xfs_bmapi_read(ip, endoffset_fsb - 1, 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. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  1868. mod++;
  1869. if (mod && (mod != mp->m_sb.sb_rextsize))
  1870. endoffset_fsb -= mod;
  1871. }
  1872. }
  1873. if ((done = (endoffset_fsb <= startoffset_fsb)))
  1874. /*
  1875. * One contiguous piece to clear
  1876. */
  1877. error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
  1878. else {
  1879. /*
  1880. * Some full blocks, possibly two pieces to clear
  1881. */
  1882. if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
  1883. error = xfs_zero_remaining_bytes(ip, offset,
  1884. XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
  1885. if (!error &&
  1886. XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
  1887. error = xfs_zero_remaining_bytes(ip,
  1888. XFS_FSB_TO_B(mp, endoffset_fsb),
  1889. offset + len - 1);
  1890. }
  1891. /*
  1892. * free file space until done or until there is an error
  1893. */
  1894. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  1895. while (!error && !done) {
  1896. /*
  1897. * allocate and setup the transaction. Allow this
  1898. * transaction to dip into the reserve blocks to ensure
  1899. * the freeing of the space succeeds at ENOSPC.
  1900. */
  1901. tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
  1902. tp->t_flags |= XFS_TRANS_RESERVE;
  1903. error = xfs_trans_reserve(tp,
  1904. resblks,
  1905. XFS_WRITE_LOG_RES(mp),
  1906. 0,
  1907. XFS_TRANS_PERM_LOG_RES,
  1908. XFS_WRITE_LOG_COUNT);
  1909. /*
  1910. * check for running out of space
  1911. */
  1912. if (error) {
  1913. /*
  1914. * Free the transaction structure.
  1915. */
  1916. ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  1917. xfs_trans_cancel(tp, 0);
  1918. break;
  1919. }
  1920. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1921. error = xfs_trans_reserve_quota(tp, mp,
  1922. ip->i_udquot, ip->i_gdquot,
  1923. resblks, 0, XFS_QMOPT_RES_REGBLKS);
  1924. if (error)
  1925. goto error1;
  1926. xfs_trans_ijoin(tp, ip, 0);
  1927. /*
  1928. * issue the bunmapi() call to free the blocks
  1929. */
  1930. xfs_bmap_init(&free_list, &firstfsb);
  1931. error = xfs_bunmapi(tp, ip, startoffset_fsb,
  1932. endoffset_fsb - startoffset_fsb,
  1933. 0, 2, &firstfsb, &free_list, &done);
  1934. if (error) {
  1935. goto error0;
  1936. }
  1937. /*
  1938. * complete the transaction
  1939. */
  1940. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1941. if (error) {
  1942. goto error0;
  1943. }
  1944. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1945. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1946. }
  1947. out_unlock_iolock:
  1948. if (need_iolock)
  1949. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1950. return error;
  1951. error0:
  1952. xfs_bmap_cancel(&free_list);
  1953. error1:
  1954. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  1955. xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
  1956. XFS_ILOCK_EXCL);
  1957. return error;
  1958. }
  1959. /*
  1960. * xfs_change_file_space()
  1961. * This routine allocates or frees disk space for the given file.
  1962. * The user specified parameters are checked for alignment and size
  1963. * limitations.
  1964. *
  1965. * RETURNS:
  1966. * 0 on success
  1967. * errno on error
  1968. *
  1969. */
  1970. int
  1971. xfs_change_file_space(
  1972. xfs_inode_t *ip,
  1973. int cmd,
  1974. xfs_flock64_t *bf,
  1975. xfs_off_t offset,
  1976. int attr_flags)
  1977. {
  1978. xfs_mount_t *mp = ip->i_mount;
  1979. int clrprealloc;
  1980. int error;
  1981. xfs_fsize_t fsize;
  1982. int setprealloc;
  1983. xfs_off_t startoffset;
  1984. xfs_off_t llen;
  1985. xfs_trans_t *tp;
  1986. struct iattr iattr;
  1987. int prealloc_type;
  1988. if (!S_ISREG(ip->i_d.di_mode))
  1989. return XFS_ERROR(EINVAL);
  1990. switch (bf->l_whence) {
  1991. case 0: /*SEEK_SET*/
  1992. break;
  1993. case 1: /*SEEK_CUR*/
  1994. bf->l_start += offset;
  1995. break;
  1996. case 2: /*SEEK_END*/
  1997. bf->l_start += XFS_ISIZE(ip);
  1998. break;
  1999. default:
  2000. return XFS_ERROR(EINVAL);
  2001. }
  2002. llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
  2003. if ( (bf->l_start < 0)
  2004. || (bf->l_start > XFS_MAXIOFFSET(mp))
  2005. || (bf->l_start + llen < 0)
  2006. || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
  2007. return XFS_ERROR(EINVAL);
  2008. bf->l_whence = 0;
  2009. startoffset = bf->l_start;
  2010. fsize = XFS_ISIZE(ip);
  2011. /*
  2012. * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
  2013. * file space.
  2014. * These calls do NOT zero the data space allocated to the file,
  2015. * nor do they change the file size.
  2016. *
  2017. * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
  2018. * space.
  2019. * These calls cause the new file data to be zeroed and the file
  2020. * size to be changed.
  2021. */
  2022. setprealloc = clrprealloc = 0;
  2023. prealloc_type = XFS_BMAPI_PREALLOC;
  2024. switch (cmd) {
  2025. case XFS_IOC_ZERO_RANGE:
  2026. prealloc_type |= XFS_BMAPI_CONVERT;
  2027. xfs_tosspages(ip, startoffset, startoffset + bf->l_len, 0);
  2028. /* FALLTHRU */
  2029. case XFS_IOC_RESVSP:
  2030. case XFS_IOC_RESVSP64:
  2031. error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
  2032. prealloc_type, attr_flags);
  2033. if (error)
  2034. return error;
  2035. setprealloc = 1;
  2036. break;
  2037. case XFS_IOC_UNRESVSP:
  2038. case XFS_IOC_UNRESVSP64:
  2039. if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
  2040. attr_flags)))
  2041. return error;
  2042. break;
  2043. case XFS_IOC_ALLOCSP:
  2044. case XFS_IOC_ALLOCSP64:
  2045. case XFS_IOC_FREESP:
  2046. case XFS_IOC_FREESP64:
  2047. /*
  2048. * These operations actually do IO when extending the file, but
  2049. * the allocation is done seperately to the zeroing that is
  2050. * done. This set of operations need to be serialised against
  2051. * other IO operations, such as truncate and buffered IO. We
  2052. * need to take the IOLOCK here to serialise the allocation and
  2053. * zeroing IO to prevent other IOLOCK holders (e.g. getbmap,
  2054. * truncate, direct IO) from racing against the transient
  2055. * allocated but not written state we can have here.
  2056. */
  2057. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  2058. if (startoffset > fsize) {
  2059. error = xfs_alloc_file_space(ip, fsize,
  2060. startoffset - fsize, 0,
  2061. attr_flags | XFS_ATTR_NOLOCK);
  2062. if (error) {
  2063. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  2064. break;
  2065. }
  2066. }
  2067. iattr.ia_valid = ATTR_SIZE;
  2068. iattr.ia_size = startoffset;
  2069. error = xfs_setattr_size(ip, &iattr,
  2070. attr_flags | XFS_ATTR_NOLOCK);
  2071. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  2072. if (error)
  2073. return error;
  2074. clrprealloc = 1;
  2075. break;
  2076. default:
  2077. ASSERT(0);
  2078. return XFS_ERROR(EINVAL);
  2079. }
  2080. /*
  2081. * update the inode timestamp, mode, and prealloc flag bits
  2082. */
  2083. tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
  2084. if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
  2085. 0, 0, 0))) {
  2086. /* ASSERT(0); */
  2087. xfs_trans_cancel(tp, 0);
  2088. return error;
  2089. }
  2090. xfs_ilock(ip, XFS_ILOCK_EXCL);
  2091. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  2092. if ((attr_flags & XFS_ATTR_DMI) == 0) {
  2093. ip->i_d.di_mode &= ~S_ISUID;
  2094. /*
  2095. * Note that we don't have to worry about mandatory
  2096. * file locking being disabled here because we only
  2097. * clear the S_ISGID bit if the Group execute bit is
  2098. * on, but if it was on then mandatory locking wouldn't
  2099. * have been enabled.
  2100. */
  2101. if (ip->i_d.di_mode & S_IXGRP)
  2102. ip->i_d.di_mode &= ~S_ISGID;
  2103. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2104. }
  2105. if (setprealloc)
  2106. ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
  2107. else if (clrprealloc)
  2108. ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
  2109. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  2110. if (attr_flags & XFS_ATTR_SYNC)
  2111. xfs_trans_set_sync(tp);
  2112. return xfs_trans_commit(tp, 0);
  2113. }