xfs_vnodeops.c 45 KB

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