xfs_vnodeops.c 57 KB

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