xfs_vnodeops.c 45 KB

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