xfs_vnodeops.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865
  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. uint resblks;
  437. uint log_res;
  438. uint log_count;
  439. trace_xfs_create(dp, name);
  440. if (XFS_FORCED_SHUTDOWN(mp))
  441. return XFS_ERROR(EIO);
  442. if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  443. prid = xfs_get_projid(dp);
  444. else
  445. prid = XFS_PROJID_DEFAULT;
  446. /*
  447. * Make sure that we have allocated dquot(s) on disk.
  448. */
  449. error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
  450. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
  451. if (error)
  452. return error;
  453. if (is_dir) {
  454. rdev = 0;
  455. resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
  456. log_res = XFS_MKDIR_LOG_RES(mp);
  457. log_count = XFS_MKDIR_LOG_COUNT;
  458. tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
  459. } else {
  460. resblks = XFS_CREATE_SPACE_RES(mp, name->len);
  461. log_res = XFS_CREATE_LOG_RES(mp);
  462. log_count = XFS_CREATE_LOG_COUNT;
  463. tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
  464. }
  465. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  466. /*
  467. * Initially assume that the file does not exist and
  468. * reserve the resources for that case. If that is not
  469. * the case we'll drop the one we have and get a more
  470. * appropriate transaction later.
  471. */
  472. error = xfs_trans_reserve(tp, resblks, log_res, 0,
  473. XFS_TRANS_PERM_LOG_RES, log_count);
  474. if (error == ENOSPC) {
  475. /* flush outstanding delalloc blocks and retry */
  476. xfs_flush_inodes(mp);
  477. error = xfs_trans_reserve(tp, resblks, log_res, 0,
  478. XFS_TRANS_PERM_LOG_RES, log_count);
  479. }
  480. if (error == ENOSPC) {
  481. /* No space at all so try a "no-allocation" reservation */
  482. resblks = 0;
  483. error = xfs_trans_reserve(tp, 0, log_res, 0,
  484. XFS_TRANS_PERM_LOG_RES, log_count);
  485. }
  486. if (error) {
  487. cancel_flags = 0;
  488. goto out_trans_cancel;
  489. }
  490. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  491. unlock_dp_on_error = true;
  492. xfs_bmap_init(&free_list, &first_block);
  493. /*
  494. * Reserve disk quota and the inode.
  495. */
  496. error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
  497. if (error)
  498. goto out_trans_cancel;
  499. error = xfs_dir_canenter(tp, dp, name, resblks);
  500. if (error)
  501. goto out_trans_cancel;
  502. /*
  503. * A newly created regular or special file just has one directory
  504. * entry pointing to them, but a directory also the "." entry
  505. * pointing to itself.
  506. */
  507. error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev,
  508. prid, resblks > 0, &ip, &committed);
  509. if (error) {
  510. if (error == ENOSPC)
  511. goto out_trans_cancel;
  512. goto out_trans_abort;
  513. }
  514. /*
  515. * Now we join the directory inode to the transaction. We do not do it
  516. * earlier because xfs_dir_ialloc might commit the previous transaction
  517. * (and release all the locks). An error from here on will result in
  518. * the transaction cancel unlocking dp so don't do it explicitly in the
  519. * error path.
  520. */
  521. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  522. unlock_dp_on_error = false;
  523. error = xfs_dir_createname(tp, dp, name, ip->i_ino,
  524. &first_block, &free_list, resblks ?
  525. resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
  526. if (error) {
  527. ASSERT(error != ENOSPC);
  528. goto out_trans_abort;
  529. }
  530. xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  531. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  532. if (is_dir) {
  533. error = xfs_dir_init(tp, ip, dp);
  534. if (error)
  535. goto out_bmap_cancel;
  536. error = xfs_bumplink(tp, dp);
  537. if (error)
  538. goto out_bmap_cancel;
  539. }
  540. /*
  541. * If this is a synchronous mount, make sure that the
  542. * create transaction goes to disk before returning to
  543. * the user.
  544. */
  545. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
  546. xfs_trans_set_sync(tp);
  547. /*
  548. * Attach the dquot(s) to the inodes and modify them incore.
  549. * These ids of the inode couldn't have changed since the new
  550. * inode has been locked ever since it was created.
  551. */
  552. xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
  553. error = xfs_bmap_finish(&tp, &free_list, &committed);
  554. if (error)
  555. goto out_bmap_cancel;
  556. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  557. if (error)
  558. goto out_release_inode;
  559. xfs_qm_dqrele(udqp);
  560. xfs_qm_dqrele(gdqp);
  561. *ipp = ip;
  562. return 0;
  563. out_bmap_cancel:
  564. xfs_bmap_cancel(&free_list);
  565. out_trans_abort:
  566. cancel_flags |= XFS_TRANS_ABORT;
  567. out_trans_cancel:
  568. xfs_trans_cancel(tp, cancel_flags);
  569. out_release_inode:
  570. /*
  571. * Wait until after the current transaction is aborted to
  572. * release the inode. This prevents recursive transactions
  573. * and deadlocks from xfs_inactive.
  574. */
  575. if (ip)
  576. IRELE(ip);
  577. xfs_qm_dqrele(udqp);
  578. xfs_qm_dqrele(gdqp);
  579. if (unlock_dp_on_error)
  580. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  581. return error;
  582. }
  583. #ifdef DEBUG
  584. int xfs_locked_n;
  585. int xfs_small_retries;
  586. int xfs_middle_retries;
  587. int xfs_lots_retries;
  588. int xfs_lock_delays;
  589. #endif
  590. /*
  591. * Bump the subclass so xfs_lock_inodes() acquires each lock with
  592. * a different value
  593. */
  594. static inline int
  595. xfs_lock_inumorder(int lock_mode, int subclass)
  596. {
  597. if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
  598. lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
  599. if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
  600. lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
  601. return lock_mode;
  602. }
  603. /*
  604. * The following routine will lock n inodes in exclusive mode.
  605. * We assume the caller calls us with the inodes in i_ino order.
  606. *
  607. * We need to detect deadlock where an inode that we lock
  608. * is in the AIL and we start waiting for another inode that is locked
  609. * by a thread in a long running transaction (such as truncate). This can
  610. * result in deadlock since the long running trans might need to wait
  611. * for the inode we just locked in order to push the tail and free space
  612. * in the log.
  613. */
  614. void
  615. xfs_lock_inodes(
  616. xfs_inode_t **ips,
  617. int inodes,
  618. uint lock_mode)
  619. {
  620. int attempts = 0, i, j, try_lock;
  621. xfs_log_item_t *lp;
  622. ASSERT(ips && (inodes >= 2)); /* we need at least two */
  623. try_lock = 0;
  624. i = 0;
  625. again:
  626. for (; i < inodes; i++) {
  627. ASSERT(ips[i]);
  628. if (i && (ips[i] == ips[i-1])) /* Already locked */
  629. continue;
  630. /*
  631. * If try_lock is not set yet, make sure all locked inodes
  632. * are not in the AIL.
  633. * If any are, set try_lock to be used later.
  634. */
  635. if (!try_lock) {
  636. for (j = (i - 1); j >= 0 && !try_lock; j--) {
  637. lp = (xfs_log_item_t *)ips[j]->i_itemp;
  638. if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
  639. try_lock++;
  640. }
  641. }
  642. }
  643. /*
  644. * If any of the previous locks we have locked is in the AIL,
  645. * we must TRY to get the second and subsequent locks. If
  646. * we can't get any, we must release all we have
  647. * and try again.
  648. */
  649. if (try_lock) {
  650. /* try_lock must be 0 if i is 0. */
  651. /*
  652. * try_lock means we have an inode locked
  653. * that is in the AIL.
  654. */
  655. ASSERT(i != 0);
  656. if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
  657. attempts++;
  658. /*
  659. * Unlock all previous guys and try again.
  660. * xfs_iunlock will try to push the tail
  661. * if the inode is in the AIL.
  662. */
  663. for(j = i - 1; j >= 0; j--) {
  664. /*
  665. * Check to see if we've already
  666. * unlocked this one.
  667. * Not the first one going back,
  668. * and the inode ptr is the same.
  669. */
  670. if ((j != (i - 1)) && ips[j] ==
  671. ips[j+1])
  672. continue;
  673. xfs_iunlock(ips[j], lock_mode);
  674. }
  675. if ((attempts % 5) == 0) {
  676. delay(1); /* Don't just spin the CPU */
  677. #ifdef DEBUG
  678. xfs_lock_delays++;
  679. #endif
  680. }
  681. i = 0;
  682. try_lock = 0;
  683. goto again;
  684. }
  685. } else {
  686. xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
  687. }
  688. }
  689. #ifdef DEBUG
  690. if (attempts) {
  691. if (attempts < 5) xfs_small_retries++;
  692. else if (attempts < 100) xfs_middle_retries++;
  693. else xfs_lots_retries++;
  694. } else {
  695. xfs_locked_n++;
  696. }
  697. #endif
  698. }
  699. /*
  700. * xfs_lock_two_inodes() can only be used to lock one type of lock
  701. * at a time - the iolock or the ilock, but not both at once. If
  702. * we lock both at once, lockdep will report false positives saying
  703. * we have violated locking orders.
  704. */
  705. void
  706. xfs_lock_two_inodes(
  707. xfs_inode_t *ip0,
  708. xfs_inode_t *ip1,
  709. uint lock_mode)
  710. {
  711. xfs_inode_t *temp;
  712. int attempts = 0;
  713. xfs_log_item_t *lp;
  714. if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
  715. ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
  716. ASSERT(ip0->i_ino != ip1->i_ino);
  717. if (ip0->i_ino > ip1->i_ino) {
  718. temp = ip0;
  719. ip0 = ip1;
  720. ip1 = temp;
  721. }
  722. again:
  723. xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
  724. /*
  725. * If the first lock we have locked is in the AIL, we must TRY to get
  726. * the second lock. If we can't get it, we must release the first one
  727. * and try again.
  728. */
  729. lp = (xfs_log_item_t *)ip0->i_itemp;
  730. if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
  731. if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
  732. xfs_iunlock(ip0, lock_mode);
  733. if ((++attempts % 5) == 0)
  734. delay(1); /* Don't just spin the CPU */
  735. goto again;
  736. }
  737. } else {
  738. xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
  739. }
  740. }
  741. int
  742. xfs_remove(
  743. xfs_inode_t *dp,
  744. struct xfs_name *name,
  745. xfs_inode_t *ip)
  746. {
  747. xfs_mount_t *mp = dp->i_mount;
  748. xfs_trans_t *tp = NULL;
  749. int is_dir = S_ISDIR(ip->i_d.di_mode);
  750. int error = 0;
  751. xfs_bmap_free_t free_list;
  752. xfs_fsblock_t first_block;
  753. int cancel_flags;
  754. int committed;
  755. int link_zero;
  756. uint resblks;
  757. uint log_count;
  758. trace_xfs_remove(dp, name);
  759. if (XFS_FORCED_SHUTDOWN(mp))
  760. return XFS_ERROR(EIO);
  761. error = xfs_qm_dqattach(dp, 0);
  762. if (error)
  763. goto std_return;
  764. error = xfs_qm_dqattach(ip, 0);
  765. if (error)
  766. goto std_return;
  767. if (is_dir) {
  768. tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
  769. log_count = XFS_DEFAULT_LOG_COUNT;
  770. } else {
  771. tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
  772. log_count = XFS_REMOVE_LOG_COUNT;
  773. }
  774. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  775. /*
  776. * We try to get the real space reservation first,
  777. * allowing for directory btree deletion(s) implying
  778. * possible bmap insert(s). If we can't get the space
  779. * reservation then we use 0 instead, and avoid the bmap
  780. * btree insert(s) in the directory code by, if the bmap
  781. * insert tries to happen, instead trimming the LAST
  782. * block from the directory.
  783. */
  784. resblks = XFS_REMOVE_SPACE_RES(mp);
  785. error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
  786. XFS_TRANS_PERM_LOG_RES, log_count);
  787. if (error == ENOSPC) {
  788. resblks = 0;
  789. error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
  790. XFS_TRANS_PERM_LOG_RES, log_count);
  791. }
  792. if (error) {
  793. ASSERT(error != ENOSPC);
  794. cancel_flags = 0;
  795. goto out_trans_cancel;
  796. }
  797. xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
  798. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  799. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  800. /*
  801. * If we're removing a directory perform some additional validation.
  802. */
  803. if (is_dir) {
  804. ASSERT(ip->i_d.di_nlink >= 2);
  805. if (ip->i_d.di_nlink != 2) {
  806. error = XFS_ERROR(ENOTEMPTY);
  807. goto out_trans_cancel;
  808. }
  809. if (!xfs_dir_isempty(ip)) {
  810. error = XFS_ERROR(ENOTEMPTY);
  811. goto out_trans_cancel;
  812. }
  813. }
  814. xfs_bmap_init(&free_list, &first_block);
  815. error = xfs_dir_removename(tp, dp, name, ip->i_ino,
  816. &first_block, &free_list, resblks);
  817. if (error) {
  818. ASSERT(error != ENOENT);
  819. goto out_bmap_cancel;
  820. }
  821. xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  822. if (is_dir) {
  823. /*
  824. * Drop the link from ip's "..".
  825. */
  826. error = xfs_droplink(tp, dp);
  827. if (error)
  828. goto out_bmap_cancel;
  829. /*
  830. * Drop the "." link from ip to self.
  831. */
  832. error = xfs_droplink(tp, ip);
  833. if (error)
  834. goto out_bmap_cancel;
  835. } else {
  836. /*
  837. * When removing a non-directory we need to log the parent
  838. * inode here. For a directory this is done implicitly
  839. * by the xfs_droplink call for the ".." entry.
  840. */
  841. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  842. }
  843. /*
  844. * Drop the link from dp to ip.
  845. */
  846. error = xfs_droplink(tp, ip);
  847. if (error)
  848. goto out_bmap_cancel;
  849. /*
  850. * Determine if this is the last link while
  851. * we are in the transaction.
  852. */
  853. link_zero = (ip->i_d.di_nlink == 0);
  854. /*
  855. * If this is a synchronous mount, make sure that the
  856. * remove transaction goes to disk before returning to
  857. * the user.
  858. */
  859. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
  860. xfs_trans_set_sync(tp);
  861. error = xfs_bmap_finish(&tp, &free_list, &committed);
  862. if (error)
  863. goto out_bmap_cancel;
  864. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  865. if (error)
  866. goto std_return;
  867. /*
  868. * If we are using filestreams, kill the stream association.
  869. * If the file is still open it may get a new one but that
  870. * will get killed on last close in xfs_close() so we don't
  871. * have to worry about that.
  872. */
  873. if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
  874. xfs_filestream_deassociate(ip);
  875. return 0;
  876. out_bmap_cancel:
  877. xfs_bmap_cancel(&free_list);
  878. cancel_flags |= XFS_TRANS_ABORT;
  879. out_trans_cancel:
  880. xfs_trans_cancel(tp, cancel_flags);
  881. std_return:
  882. return error;
  883. }
  884. int
  885. xfs_link(
  886. xfs_inode_t *tdp,
  887. xfs_inode_t *sip,
  888. struct xfs_name *target_name)
  889. {
  890. xfs_mount_t *mp = tdp->i_mount;
  891. xfs_trans_t *tp;
  892. int error;
  893. xfs_bmap_free_t free_list;
  894. xfs_fsblock_t first_block;
  895. int cancel_flags;
  896. int committed;
  897. int resblks;
  898. trace_xfs_link(tdp, target_name);
  899. ASSERT(!S_ISDIR(sip->i_d.di_mode));
  900. if (XFS_FORCED_SHUTDOWN(mp))
  901. return XFS_ERROR(EIO);
  902. error = xfs_qm_dqattach(sip, 0);
  903. if (error)
  904. goto std_return;
  905. error = xfs_qm_dqattach(tdp, 0);
  906. if (error)
  907. goto std_return;
  908. tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
  909. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  910. resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
  911. error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
  912. XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
  913. if (error == ENOSPC) {
  914. resblks = 0;
  915. error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
  916. XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
  917. }
  918. if (error) {
  919. cancel_flags = 0;
  920. goto error_return;
  921. }
  922. xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
  923. xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
  924. xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
  925. /*
  926. * If we are using project inheritance, we only allow hard link
  927. * creation in our tree when the project IDs are the same; else
  928. * the tree quota mechanism could be circumvented.
  929. */
  930. if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
  931. (xfs_get_projid(tdp) != xfs_get_projid(sip)))) {
  932. error = XFS_ERROR(EXDEV);
  933. goto error_return;
  934. }
  935. error = xfs_dir_canenter(tp, tdp, target_name, resblks);
  936. if (error)
  937. goto error_return;
  938. xfs_bmap_init(&free_list, &first_block);
  939. error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
  940. &first_block, &free_list, resblks);
  941. if (error)
  942. goto abort_return;
  943. xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  944. xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
  945. error = xfs_bumplink(tp, sip);
  946. if (error)
  947. goto abort_return;
  948. /*
  949. * If this is a synchronous mount, make sure that the
  950. * link transaction goes to disk before returning to
  951. * the user.
  952. */
  953. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  954. xfs_trans_set_sync(tp);
  955. }
  956. error = xfs_bmap_finish (&tp, &free_list, &committed);
  957. if (error) {
  958. xfs_bmap_cancel(&free_list);
  959. goto abort_return;
  960. }
  961. return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  962. abort_return:
  963. cancel_flags |= XFS_TRANS_ABORT;
  964. error_return:
  965. xfs_trans_cancel(tp, cancel_flags);
  966. std_return:
  967. return error;
  968. }
  969. int
  970. xfs_set_dmattrs(
  971. xfs_inode_t *ip,
  972. u_int evmask,
  973. u_int16_t state)
  974. {
  975. xfs_mount_t *mp = ip->i_mount;
  976. xfs_trans_t *tp;
  977. int error;
  978. if (!capable(CAP_SYS_ADMIN))
  979. return XFS_ERROR(EPERM);
  980. if (XFS_FORCED_SHUTDOWN(mp))
  981. return XFS_ERROR(EIO);
  982. tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
  983. error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
  984. if (error) {
  985. xfs_trans_cancel(tp, 0);
  986. return error;
  987. }
  988. xfs_ilock(ip, XFS_ILOCK_EXCL);
  989. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  990. ip->i_d.di_dmevmask = evmask;
  991. ip->i_d.di_dmstate = state;
  992. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  993. error = xfs_trans_commit(tp, 0);
  994. return error;
  995. }
  996. /*
  997. * xfs_alloc_file_space()
  998. * This routine allocates disk space for the given file.
  999. *
  1000. * If alloc_type == 0, this request is for an ALLOCSP type
  1001. * request which will change the file size. In this case, no
  1002. * DMAPI event will be generated by the call. A TRUNCATE event
  1003. * will be generated later by xfs_setattr.
  1004. *
  1005. * If alloc_type != 0, this request is for a RESVSP type
  1006. * request, and a DMAPI DM_EVENT_WRITE will be generated if the
  1007. * lower block boundary byte address is less than the file's
  1008. * length.
  1009. *
  1010. * RETURNS:
  1011. * 0 on success
  1012. * errno on error
  1013. *
  1014. */
  1015. STATIC int
  1016. xfs_alloc_file_space(
  1017. xfs_inode_t *ip,
  1018. xfs_off_t offset,
  1019. xfs_off_t len,
  1020. int alloc_type,
  1021. int attr_flags)
  1022. {
  1023. xfs_mount_t *mp = ip->i_mount;
  1024. xfs_off_t count;
  1025. xfs_filblks_t allocated_fsb;
  1026. xfs_filblks_t allocatesize_fsb;
  1027. xfs_extlen_t extsz, temp;
  1028. xfs_fileoff_t startoffset_fsb;
  1029. xfs_fsblock_t firstfsb;
  1030. int nimaps;
  1031. int quota_flag;
  1032. int rt;
  1033. xfs_trans_t *tp;
  1034. xfs_bmbt_irec_t imaps[1], *imapp;
  1035. xfs_bmap_free_t free_list;
  1036. uint qblocks, resblks, resrtextents;
  1037. int committed;
  1038. int error;
  1039. trace_xfs_alloc_file_space(ip);
  1040. if (XFS_FORCED_SHUTDOWN(mp))
  1041. return XFS_ERROR(EIO);
  1042. error = xfs_qm_dqattach(ip, 0);
  1043. if (error)
  1044. return error;
  1045. if (len <= 0)
  1046. return XFS_ERROR(EINVAL);
  1047. rt = XFS_IS_REALTIME_INODE(ip);
  1048. extsz = xfs_get_extsz_hint(ip);
  1049. count = len;
  1050. imapp = &imaps[0];
  1051. nimaps = 1;
  1052. startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
  1053. allocatesize_fsb = XFS_B_TO_FSB(mp, count);
  1054. /*
  1055. * Allocate file space until done or until there is an error
  1056. */
  1057. while (allocatesize_fsb && !error) {
  1058. xfs_fileoff_t s, e;
  1059. /*
  1060. * Determine space reservations for data/realtime.
  1061. */
  1062. if (unlikely(extsz)) {
  1063. s = startoffset_fsb;
  1064. do_div(s, extsz);
  1065. s *= extsz;
  1066. e = startoffset_fsb + allocatesize_fsb;
  1067. if ((temp = do_mod(startoffset_fsb, extsz)))
  1068. e += temp;
  1069. if ((temp = do_mod(e, extsz)))
  1070. e += extsz - temp;
  1071. } else {
  1072. s = 0;
  1073. e = allocatesize_fsb;
  1074. }
  1075. /*
  1076. * The transaction reservation is limited to a 32-bit block
  1077. * count, hence we need to limit the number of blocks we are
  1078. * trying to reserve to avoid an overflow. We can't allocate
  1079. * more than @nimaps extents, and an extent is limited on disk
  1080. * to MAXEXTLEN (21 bits), so use that to enforce the limit.
  1081. */
  1082. resblks = min_t(xfs_fileoff_t, (e - s), (MAXEXTLEN * nimaps));
  1083. if (unlikely(rt)) {
  1084. resrtextents = qblocks = resblks;
  1085. resrtextents /= mp->m_sb.sb_rextsize;
  1086. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  1087. quota_flag = XFS_QMOPT_RES_RTBLKS;
  1088. } else {
  1089. resrtextents = 0;
  1090. resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
  1091. quota_flag = XFS_QMOPT_RES_REGBLKS;
  1092. }
  1093. /*
  1094. * Allocate and setup the transaction.
  1095. */
  1096. tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
  1097. error = xfs_trans_reserve(tp, resblks,
  1098. XFS_WRITE_LOG_RES(mp), resrtextents,
  1099. XFS_TRANS_PERM_LOG_RES,
  1100. XFS_WRITE_LOG_COUNT);
  1101. /*
  1102. * Check for running out of space
  1103. */
  1104. if (error) {
  1105. /*
  1106. * Free the transaction structure.
  1107. */
  1108. ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  1109. xfs_trans_cancel(tp, 0);
  1110. break;
  1111. }
  1112. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1113. error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
  1114. 0, quota_flag);
  1115. if (error)
  1116. goto error1;
  1117. xfs_trans_ijoin(tp, ip, 0);
  1118. xfs_bmap_init(&free_list, &firstfsb);
  1119. error = xfs_bmapi_write(tp, ip, startoffset_fsb,
  1120. allocatesize_fsb, alloc_type, &firstfsb,
  1121. 0, imapp, &nimaps, &free_list);
  1122. if (error) {
  1123. goto error0;
  1124. }
  1125. /*
  1126. * Complete the transaction
  1127. */
  1128. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1129. if (error) {
  1130. goto error0;
  1131. }
  1132. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1133. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1134. if (error) {
  1135. break;
  1136. }
  1137. allocated_fsb = imapp->br_blockcount;
  1138. if (nimaps == 0) {
  1139. error = XFS_ERROR(ENOSPC);
  1140. break;
  1141. }
  1142. startoffset_fsb += allocated_fsb;
  1143. allocatesize_fsb -= allocated_fsb;
  1144. }
  1145. return error;
  1146. error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
  1147. xfs_bmap_cancel(&free_list);
  1148. xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag);
  1149. error1: /* Just cancel transaction */
  1150. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  1151. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1152. return error;
  1153. }
  1154. /*
  1155. * Zero file bytes between startoff and endoff inclusive.
  1156. * The iolock is held exclusive and no blocks are buffered.
  1157. *
  1158. * This function is used by xfs_free_file_space() to zero
  1159. * partial blocks when the range to free is not block aligned.
  1160. * When unreserving space with boundaries that are not block
  1161. * aligned we round up the start and round down the end
  1162. * boundaries and then use this function to zero the parts of
  1163. * the blocks that got dropped during the rounding.
  1164. */
  1165. STATIC int
  1166. xfs_zero_remaining_bytes(
  1167. xfs_inode_t *ip,
  1168. xfs_off_t startoff,
  1169. xfs_off_t endoff)
  1170. {
  1171. xfs_bmbt_irec_t imap;
  1172. xfs_fileoff_t offset_fsb;
  1173. xfs_off_t lastoffset;
  1174. xfs_off_t offset;
  1175. xfs_buf_t *bp;
  1176. xfs_mount_t *mp = ip->i_mount;
  1177. int nimap;
  1178. int error = 0;
  1179. /*
  1180. * Avoid doing I/O beyond eof - it's not necessary
  1181. * since nothing can read beyond eof. The space will
  1182. * be zeroed when the file is extended anyway.
  1183. */
  1184. if (startoff >= XFS_ISIZE(ip))
  1185. return 0;
  1186. if (endoff > XFS_ISIZE(ip))
  1187. endoff = XFS_ISIZE(ip);
  1188. bp = xfs_buf_get_uncached(XFS_IS_REALTIME_INODE(ip) ?
  1189. mp->m_rtdev_targp : mp->m_ddev_targp,
  1190. BTOBB(mp->m_sb.sb_blocksize), 0);
  1191. if (!bp)
  1192. return XFS_ERROR(ENOMEM);
  1193. xfs_buf_unlock(bp);
  1194. for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
  1195. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  1196. nimap = 1;
  1197. error = xfs_bmapi_read(ip, offset_fsb, 1, &imap, &nimap, 0);
  1198. if (error || nimap < 1)
  1199. break;
  1200. ASSERT(imap.br_blockcount >= 1);
  1201. ASSERT(imap.br_startoff == offset_fsb);
  1202. lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
  1203. if (lastoffset > endoff)
  1204. lastoffset = endoff;
  1205. if (imap.br_startblock == HOLESTARTBLOCK)
  1206. continue;
  1207. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  1208. if (imap.br_state == XFS_EXT_UNWRITTEN)
  1209. continue;
  1210. XFS_BUF_UNDONE(bp);
  1211. XFS_BUF_UNWRITE(bp);
  1212. XFS_BUF_READ(bp);
  1213. XFS_BUF_SET_ADDR(bp, xfs_fsb_to_db(ip, imap.br_startblock));
  1214. xfsbdstrat(mp, bp);
  1215. error = xfs_buf_iowait(bp);
  1216. if (error) {
  1217. xfs_buf_ioerror_alert(bp,
  1218. "xfs_zero_remaining_bytes(read)");
  1219. break;
  1220. }
  1221. memset(bp->b_addr +
  1222. (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
  1223. 0, lastoffset - offset + 1);
  1224. XFS_BUF_UNDONE(bp);
  1225. XFS_BUF_UNREAD(bp);
  1226. XFS_BUF_WRITE(bp);
  1227. xfsbdstrat(mp, bp);
  1228. error = xfs_buf_iowait(bp);
  1229. if (error) {
  1230. xfs_buf_ioerror_alert(bp,
  1231. "xfs_zero_remaining_bytes(write)");
  1232. break;
  1233. }
  1234. }
  1235. xfs_buf_free(bp);
  1236. return error;
  1237. }
  1238. /*
  1239. * xfs_free_file_space()
  1240. * This routine frees disk space for the given file.
  1241. *
  1242. * This routine is only called by xfs_change_file_space
  1243. * for an UNRESVSP type call.
  1244. *
  1245. * RETURNS:
  1246. * 0 on success
  1247. * errno on error
  1248. *
  1249. */
  1250. STATIC int
  1251. xfs_free_file_space(
  1252. xfs_inode_t *ip,
  1253. xfs_off_t offset,
  1254. xfs_off_t len,
  1255. int attr_flags)
  1256. {
  1257. int committed;
  1258. int done;
  1259. xfs_fileoff_t endoffset_fsb;
  1260. int error;
  1261. xfs_fsblock_t firstfsb;
  1262. xfs_bmap_free_t free_list;
  1263. xfs_bmbt_irec_t imap;
  1264. xfs_off_t ioffset;
  1265. xfs_extlen_t mod=0;
  1266. xfs_mount_t *mp;
  1267. int nimap;
  1268. uint resblks;
  1269. xfs_off_t rounding;
  1270. int rt;
  1271. xfs_fileoff_t startoffset_fsb;
  1272. xfs_trans_t *tp;
  1273. int need_iolock = 1;
  1274. mp = ip->i_mount;
  1275. trace_xfs_free_file_space(ip);
  1276. error = xfs_qm_dqattach(ip, 0);
  1277. if (error)
  1278. return error;
  1279. error = 0;
  1280. if (len <= 0) /* if nothing being freed */
  1281. return error;
  1282. rt = XFS_IS_REALTIME_INODE(ip);
  1283. startoffset_fsb = XFS_B_TO_FSB(mp, offset);
  1284. endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
  1285. if (attr_flags & XFS_ATTR_NOLOCK)
  1286. need_iolock = 0;
  1287. if (need_iolock) {
  1288. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  1289. /* wait for the completion of any pending DIOs */
  1290. inode_dio_wait(VFS_I(ip));
  1291. }
  1292. rounding = max_t(xfs_off_t, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
  1293. ioffset = offset & ~(rounding - 1);
  1294. error = -filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
  1295. ioffset, -1);
  1296. if (error)
  1297. goto out_unlock_iolock;
  1298. truncate_pagecache_range(VFS_I(ip), ioffset, -1);
  1299. /*
  1300. * Need to zero the stuff we're not freeing, on disk.
  1301. * If it's a realtime file & can't use unwritten extents then we
  1302. * actually need to zero the extent edges. Otherwise xfs_bunmapi
  1303. * will take care of it for us.
  1304. */
  1305. if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
  1306. nimap = 1;
  1307. error = xfs_bmapi_read(ip, startoffset_fsb, 1,
  1308. &imap, &nimap, 0);
  1309. if (error)
  1310. goto out_unlock_iolock;
  1311. ASSERT(nimap == 0 || nimap == 1);
  1312. if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
  1313. xfs_daddr_t block;
  1314. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  1315. block = imap.br_startblock;
  1316. mod = do_div(block, mp->m_sb.sb_rextsize);
  1317. if (mod)
  1318. startoffset_fsb += mp->m_sb.sb_rextsize - mod;
  1319. }
  1320. nimap = 1;
  1321. error = xfs_bmapi_read(ip, endoffset_fsb - 1, 1,
  1322. &imap, &nimap, 0);
  1323. if (error)
  1324. goto out_unlock_iolock;
  1325. ASSERT(nimap == 0 || nimap == 1);
  1326. if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
  1327. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  1328. mod++;
  1329. if (mod && (mod != mp->m_sb.sb_rextsize))
  1330. endoffset_fsb -= mod;
  1331. }
  1332. }
  1333. if ((done = (endoffset_fsb <= startoffset_fsb)))
  1334. /*
  1335. * One contiguous piece to clear
  1336. */
  1337. error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
  1338. else {
  1339. /*
  1340. * Some full blocks, possibly two pieces to clear
  1341. */
  1342. if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
  1343. error = xfs_zero_remaining_bytes(ip, offset,
  1344. XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
  1345. if (!error &&
  1346. XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
  1347. error = xfs_zero_remaining_bytes(ip,
  1348. XFS_FSB_TO_B(mp, endoffset_fsb),
  1349. offset + len - 1);
  1350. }
  1351. /*
  1352. * free file space until done or until there is an error
  1353. */
  1354. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  1355. while (!error && !done) {
  1356. /*
  1357. * allocate and setup the transaction. Allow this
  1358. * transaction to dip into the reserve blocks to ensure
  1359. * the freeing of the space succeeds at ENOSPC.
  1360. */
  1361. tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
  1362. tp->t_flags |= XFS_TRANS_RESERVE;
  1363. error = xfs_trans_reserve(tp,
  1364. resblks,
  1365. XFS_WRITE_LOG_RES(mp),
  1366. 0,
  1367. XFS_TRANS_PERM_LOG_RES,
  1368. XFS_WRITE_LOG_COUNT);
  1369. /*
  1370. * check for running out of space
  1371. */
  1372. if (error) {
  1373. /*
  1374. * Free the transaction structure.
  1375. */
  1376. ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  1377. xfs_trans_cancel(tp, 0);
  1378. break;
  1379. }
  1380. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1381. error = xfs_trans_reserve_quota(tp, mp,
  1382. ip->i_udquot, ip->i_gdquot,
  1383. resblks, 0, XFS_QMOPT_RES_REGBLKS);
  1384. if (error)
  1385. goto error1;
  1386. xfs_trans_ijoin(tp, ip, 0);
  1387. /*
  1388. * issue the bunmapi() call to free the blocks
  1389. */
  1390. xfs_bmap_init(&free_list, &firstfsb);
  1391. error = xfs_bunmapi(tp, ip, startoffset_fsb,
  1392. endoffset_fsb - startoffset_fsb,
  1393. 0, 2, &firstfsb, &free_list, &done);
  1394. if (error) {
  1395. goto error0;
  1396. }
  1397. /*
  1398. * complete the transaction
  1399. */
  1400. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1401. if (error) {
  1402. goto error0;
  1403. }
  1404. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1405. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1406. }
  1407. out_unlock_iolock:
  1408. if (need_iolock)
  1409. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1410. return error;
  1411. error0:
  1412. xfs_bmap_cancel(&free_list);
  1413. error1:
  1414. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  1415. xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
  1416. XFS_ILOCK_EXCL);
  1417. return error;
  1418. }
  1419. STATIC int
  1420. xfs_zero_file_space(
  1421. struct xfs_inode *ip,
  1422. xfs_off_t offset,
  1423. xfs_off_t len,
  1424. int attr_flags)
  1425. {
  1426. struct xfs_mount *mp = ip->i_mount;
  1427. uint granularity;
  1428. xfs_off_t start_boundary;
  1429. xfs_off_t end_boundary;
  1430. int error;
  1431. granularity = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
  1432. /*
  1433. * Round the range of extents we are going to convert inwards. If the
  1434. * offset is aligned, then it doesn't get changed so we zero from the
  1435. * start of the block offset points to.
  1436. */
  1437. start_boundary = round_up(offset, granularity);
  1438. end_boundary = round_down(offset + len, granularity);
  1439. ASSERT(start_boundary >= offset);
  1440. ASSERT(end_boundary <= offset + len);
  1441. if (!(attr_flags & XFS_ATTR_NOLOCK))
  1442. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  1443. if (start_boundary < end_boundary - 1) {
  1444. /* punch out the page cache over the conversion range */
  1445. truncate_pagecache_range(VFS_I(ip), start_boundary,
  1446. end_boundary - 1);
  1447. /* convert the blocks */
  1448. error = xfs_alloc_file_space(ip, start_boundary,
  1449. end_boundary - start_boundary - 1,
  1450. XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT,
  1451. attr_flags);
  1452. if (error)
  1453. goto out_unlock;
  1454. /* We've handled the interior of the range, now for the edges */
  1455. if (start_boundary != offset)
  1456. error = xfs_iozero(ip, offset, start_boundary - offset);
  1457. if (error)
  1458. goto out_unlock;
  1459. if (end_boundary != offset + len)
  1460. error = xfs_iozero(ip, end_boundary,
  1461. offset + len - end_boundary);
  1462. } else {
  1463. /*
  1464. * It's either a sub-granularity range or the range spanned lies
  1465. * partially across two adjacent blocks.
  1466. */
  1467. error = xfs_iozero(ip, offset, len);
  1468. }
  1469. out_unlock:
  1470. if (!(attr_flags & XFS_ATTR_NOLOCK))
  1471. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1472. return error;
  1473. }
  1474. /*
  1475. * xfs_change_file_space()
  1476. * This routine allocates or frees disk space for the given file.
  1477. * The user specified parameters are checked for alignment and size
  1478. * limitations.
  1479. *
  1480. * RETURNS:
  1481. * 0 on success
  1482. * errno on error
  1483. *
  1484. */
  1485. int
  1486. xfs_change_file_space(
  1487. xfs_inode_t *ip,
  1488. int cmd,
  1489. xfs_flock64_t *bf,
  1490. xfs_off_t offset,
  1491. int attr_flags)
  1492. {
  1493. xfs_mount_t *mp = ip->i_mount;
  1494. int clrprealloc;
  1495. int error;
  1496. xfs_fsize_t fsize;
  1497. int setprealloc;
  1498. xfs_off_t startoffset;
  1499. xfs_trans_t *tp;
  1500. struct iattr iattr;
  1501. if (!S_ISREG(ip->i_d.di_mode))
  1502. return XFS_ERROR(EINVAL);
  1503. switch (bf->l_whence) {
  1504. case 0: /*SEEK_SET*/
  1505. break;
  1506. case 1: /*SEEK_CUR*/
  1507. bf->l_start += offset;
  1508. break;
  1509. case 2: /*SEEK_END*/
  1510. bf->l_start += XFS_ISIZE(ip);
  1511. break;
  1512. default:
  1513. return XFS_ERROR(EINVAL);
  1514. }
  1515. /*
  1516. * length of <= 0 for resv/unresv/zero is invalid. length for
  1517. * alloc/free is ignored completely and we have no idea what userspace
  1518. * might have set it to, so set it to zero to allow range
  1519. * checks to pass.
  1520. */
  1521. switch (cmd) {
  1522. case XFS_IOC_ZERO_RANGE:
  1523. case XFS_IOC_RESVSP:
  1524. case XFS_IOC_RESVSP64:
  1525. case XFS_IOC_UNRESVSP:
  1526. case XFS_IOC_UNRESVSP64:
  1527. if (bf->l_len <= 0)
  1528. return XFS_ERROR(EINVAL);
  1529. break;
  1530. default:
  1531. bf->l_len = 0;
  1532. break;
  1533. }
  1534. if (bf->l_start < 0 ||
  1535. bf->l_start > mp->m_super->s_maxbytes ||
  1536. bf->l_start + bf->l_len < 0 ||
  1537. bf->l_start + bf->l_len >= mp->m_super->s_maxbytes)
  1538. return XFS_ERROR(EINVAL);
  1539. bf->l_whence = 0;
  1540. startoffset = bf->l_start;
  1541. fsize = XFS_ISIZE(ip);
  1542. setprealloc = clrprealloc = 0;
  1543. switch (cmd) {
  1544. case XFS_IOC_ZERO_RANGE:
  1545. error = xfs_zero_file_space(ip, startoffset, bf->l_len,
  1546. attr_flags);
  1547. if (error)
  1548. return error;
  1549. setprealloc = 1;
  1550. break;
  1551. case XFS_IOC_RESVSP:
  1552. case XFS_IOC_RESVSP64:
  1553. error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
  1554. XFS_BMAPI_PREALLOC, attr_flags);
  1555. if (error)
  1556. return error;
  1557. setprealloc = 1;
  1558. break;
  1559. case XFS_IOC_UNRESVSP:
  1560. case XFS_IOC_UNRESVSP64:
  1561. if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
  1562. attr_flags)))
  1563. return error;
  1564. break;
  1565. case XFS_IOC_ALLOCSP:
  1566. case XFS_IOC_ALLOCSP64:
  1567. case XFS_IOC_FREESP:
  1568. case XFS_IOC_FREESP64:
  1569. /*
  1570. * These operations actually do IO when extending the file, but
  1571. * the allocation is done seperately to the zeroing that is
  1572. * done. This set of operations need to be serialised against
  1573. * other IO operations, such as truncate and buffered IO. We
  1574. * need to take the IOLOCK here to serialise the allocation and
  1575. * zeroing IO to prevent other IOLOCK holders (e.g. getbmap,
  1576. * truncate, direct IO) from racing against the transient
  1577. * allocated but not written state we can have here.
  1578. */
  1579. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  1580. if (startoffset > fsize) {
  1581. error = xfs_alloc_file_space(ip, fsize,
  1582. startoffset - fsize, 0,
  1583. attr_flags | XFS_ATTR_NOLOCK);
  1584. if (error) {
  1585. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1586. break;
  1587. }
  1588. }
  1589. iattr.ia_valid = ATTR_SIZE;
  1590. iattr.ia_size = startoffset;
  1591. error = xfs_setattr_size(ip, &iattr,
  1592. attr_flags | XFS_ATTR_NOLOCK);
  1593. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1594. if (error)
  1595. return error;
  1596. clrprealloc = 1;
  1597. break;
  1598. default:
  1599. ASSERT(0);
  1600. return XFS_ERROR(EINVAL);
  1601. }
  1602. /*
  1603. * update the inode timestamp, mode, and prealloc flag bits
  1604. */
  1605. tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
  1606. if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
  1607. 0, 0, 0))) {
  1608. /* ASSERT(0); */
  1609. xfs_trans_cancel(tp, 0);
  1610. return error;
  1611. }
  1612. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1613. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  1614. if ((attr_flags & XFS_ATTR_DMI) == 0) {
  1615. ip->i_d.di_mode &= ~S_ISUID;
  1616. /*
  1617. * Note that we don't have to worry about mandatory
  1618. * file locking being disabled here because we only
  1619. * clear the S_ISGID bit if the Group execute bit is
  1620. * on, but if it was on then mandatory locking wouldn't
  1621. * have been enabled.
  1622. */
  1623. if (ip->i_d.di_mode & S_IXGRP)
  1624. ip->i_d.di_mode &= ~S_ISGID;
  1625. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1626. }
  1627. if (setprealloc)
  1628. ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
  1629. else if (clrprealloc)
  1630. ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
  1631. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1632. if (attr_flags & XFS_ATTR_SYNC)
  1633. xfs_trans_set_sync(tp);
  1634. return xfs_trans_commit(tp, 0);
  1635. }