xfs_vnodeops.c 55 KB

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