xfs_vnodeops.c 59 KB

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