xfs_vnodeops.c 57 KB

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