xfs_vnodeops.c 55 KB

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