xfs_vnodeops.c 55 KB

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