xfs_vnodeops.c 57 KB

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