xfs_vnodeops.c 58 KB

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