xfs_vnodeops.c 55 KB

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