xfs_vnodeops.c 57 KB

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