xfs_symlink.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. /*
  2. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  3. * Copyright (c) 2012-2013 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it would be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write the Free Software Foundation,
  17. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "xfs.h"
  20. #include "xfs_shared.h"
  21. #include "xfs_fs.h"
  22. #include "xfs_format.h"
  23. #include "xfs_log_format.h"
  24. #include "xfs_trans_resv.h"
  25. #include "xfs_bit.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_da_format.h"
  28. #include "xfs_da_btree.h"
  29. #include "xfs_defer.h"
  30. #include "xfs_dir2.h"
  31. #include "xfs_inode.h"
  32. #include "xfs_ialloc.h"
  33. #include "xfs_alloc.h"
  34. #include "xfs_bmap.h"
  35. #include "xfs_bmap_btree.h"
  36. #include "xfs_bmap_util.h"
  37. #include "xfs_error.h"
  38. #include "xfs_quota.h"
  39. #include "xfs_trans_space.h"
  40. #include "xfs_trace.h"
  41. #include "xfs_symlink.h"
  42. #include "xfs_trans.h"
  43. #include "xfs_log.h"
  44. /* ----- Kernel only functions below ----- */
  45. int
  46. xfs_readlink_bmap_ilocked(
  47. struct xfs_inode *ip,
  48. char *link)
  49. {
  50. struct xfs_mount *mp = ip->i_mount;
  51. struct xfs_bmbt_irec mval[XFS_SYMLINK_MAPS];
  52. struct xfs_buf *bp;
  53. xfs_daddr_t d;
  54. char *cur_chunk;
  55. int pathlen = ip->i_d.di_size;
  56. int nmaps = XFS_SYMLINK_MAPS;
  57. int byte_cnt;
  58. int n;
  59. int error = 0;
  60. int fsblocks = 0;
  61. int offset;
  62. ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  63. fsblocks = xfs_symlink_blocks(mp, pathlen);
  64. error = xfs_bmapi_read(ip, 0, fsblocks, mval, &nmaps, 0);
  65. if (error)
  66. goto out;
  67. offset = 0;
  68. for (n = 0; n < nmaps; n++) {
  69. d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
  70. byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
  71. bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0,
  72. &xfs_symlink_buf_ops);
  73. if (!bp)
  74. return -ENOMEM;
  75. error = bp->b_error;
  76. if (error) {
  77. xfs_buf_ioerror_alert(bp, __func__);
  78. xfs_buf_relse(bp);
  79. /* bad CRC means corrupted metadata */
  80. if (error == -EFSBADCRC)
  81. error = -EFSCORRUPTED;
  82. goto out;
  83. }
  84. byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt);
  85. if (pathlen < byte_cnt)
  86. byte_cnt = pathlen;
  87. cur_chunk = bp->b_addr;
  88. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  89. if (!xfs_symlink_hdr_ok(ip->i_ino, offset,
  90. byte_cnt, bp)) {
  91. error = -EFSCORRUPTED;
  92. xfs_alert(mp,
  93. "symlink header does not match required off/len/owner (0x%x/Ox%x,0x%llx)",
  94. offset, byte_cnt, ip->i_ino);
  95. xfs_buf_relse(bp);
  96. goto out;
  97. }
  98. cur_chunk += sizeof(struct xfs_dsymlink_hdr);
  99. }
  100. memcpy(link + offset, cur_chunk, byte_cnt);
  101. pathlen -= byte_cnt;
  102. offset += byte_cnt;
  103. xfs_buf_relse(bp);
  104. }
  105. ASSERT(pathlen == 0);
  106. link[ip->i_d.di_size] = '\0';
  107. error = 0;
  108. out:
  109. return error;
  110. }
  111. int
  112. xfs_readlink(
  113. struct xfs_inode *ip,
  114. char *link)
  115. {
  116. struct xfs_mount *mp = ip->i_mount;
  117. xfs_fsize_t pathlen;
  118. int error = 0;
  119. trace_xfs_readlink(ip);
  120. ASSERT(!(ip->i_df.if_flags & XFS_IFINLINE));
  121. if (XFS_FORCED_SHUTDOWN(mp))
  122. return -EIO;
  123. xfs_ilock(ip, XFS_ILOCK_SHARED);
  124. pathlen = ip->i_d.di_size;
  125. if (!pathlen)
  126. goto out;
  127. if (pathlen < 0 || pathlen > XFS_SYMLINK_MAXLEN) {
  128. xfs_alert(mp, "%s: inode (%llu) bad symlink length (%lld)",
  129. __func__, (unsigned long long) ip->i_ino,
  130. (long long) pathlen);
  131. ASSERT(0);
  132. error = -EFSCORRUPTED;
  133. goto out;
  134. }
  135. error = xfs_readlink_bmap_ilocked(ip, link);
  136. out:
  137. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  138. return error;
  139. }
  140. int
  141. xfs_symlink(
  142. struct xfs_inode *dp,
  143. struct xfs_name *link_name,
  144. const char *target_path,
  145. umode_t mode,
  146. struct xfs_inode **ipp)
  147. {
  148. struct xfs_mount *mp = dp->i_mount;
  149. struct xfs_trans *tp = NULL;
  150. struct xfs_inode *ip = NULL;
  151. int error = 0;
  152. int pathlen;
  153. struct xfs_defer_ops dfops;
  154. xfs_fsblock_t first_block;
  155. bool unlock_dp_on_error = false;
  156. xfs_fileoff_t first_fsb;
  157. xfs_filblks_t fs_blocks;
  158. int nmaps;
  159. struct xfs_bmbt_irec mval[XFS_SYMLINK_MAPS];
  160. xfs_daddr_t d;
  161. const char *cur_chunk;
  162. int byte_cnt;
  163. int n;
  164. xfs_buf_t *bp;
  165. prid_t prid;
  166. struct xfs_dquot *udqp = NULL;
  167. struct xfs_dquot *gdqp = NULL;
  168. struct xfs_dquot *pdqp = NULL;
  169. uint resblks;
  170. *ipp = NULL;
  171. trace_xfs_symlink(dp, link_name);
  172. if (XFS_FORCED_SHUTDOWN(mp))
  173. return -EIO;
  174. /*
  175. * Check component lengths of the target path name.
  176. */
  177. pathlen = strlen(target_path);
  178. if (pathlen >= XFS_SYMLINK_MAXLEN) /* total string too long */
  179. return -ENAMETOOLONG;
  180. udqp = gdqp = NULL;
  181. prid = xfs_get_initial_prid(dp);
  182. /*
  183. * Make sure that we have allocated dquot(s) on disk.
  184. */
  185. error = xfs_qm_vop_dqalloc(dp,
  186. xfs_kuid_to_uid(current_fsuid()),
  187. xfs_kgid_to_gid(current_fsgid()), prid,
  188. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
  189. &udqp, &gdqp, &pdqp);
  190. if (error)
  191. return error;
  192. /*
  193. * The symlink will fit into the inode data fork?
  194. * There can't be any attributes so we get the whole variable part.
  195. */
  196. if (pathlen <= XFS_LITINO(mp, dp->i_d.di_version))
  197. fs_blocks = 0;
  198. else
  199. fs_blocks = xfs_symlink_blocks(mp, pathlen);
  200. resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
  201. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_symlink, resblks, 0, 0, &tp);
  202. if (error == -ENOSPC && fs_blocks == 0) {
  203. resblks = 0;
  204. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_symlink, 0, 0, 0,
  205. &tp);
  206. }
  207. if (error)
  208. goto out_release_inode;
  209. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  210. unlock_dp_on_error = true;
  211. /*
  212. * Check whether the directory allows new symlinks or not.
  213. */
  214. if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
  215. error = -EPERM;
  216. goto out_trans_cancel;
  217. }
  218. /*
  219. * Reserve disk quota : blocks and inode.
  220. */
  221. error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
  222. pdqp, resblks, 1, 0);
  223. if (error)
  224. goto out_trans_cancel;
  225. /*
  226. * Check for ability to enter directory entry, if no space reserved.
  227. */
  228. if (!resblks) {
  229. error = xfs_dir_canenter(tp, dp, link_name);
  230. if (error)
  231. goto out_trans_cancel;
  232. }
  233. /*
  234. * Initialize the bmap freelist prior to calling either
  235. * bmapi or the directory create code.
  236. */
  237. xfs_defer_init(&dfops, &first_block);
  238. /*
  239. * Allocate an inode for the symlink.
  240. */
  241. error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT), 1, 0,
  242. prid, resblks > 0, &ip, NULL);
  243. if (error)
  244. goto out_trans_cancel;
  245. /*
  246. * Now we join the directory inode to the transaction. We do not do it
  247. * earlier because xfs_dir_ialloc might commit the previous transaction
  248. * (and release all the locks). An error from here on will result in
  249. * the transaction cancel unlocking dp so don't do it explicitly in the
  250. * error path.
  251. */
  252. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  253. unlock_dp_on_error = false;
  254. /*
  255. * Also attach the dquot(s) to it, if applicable.
  256. */
  257. xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
  258. if (resblks)
  259. resblks -= XFS_IALLOC_SPACE_RES(mp);
  260. /*
  261. * If the symlink will fit into the inode, write it inline.
  262. */
  263. if (pathlen <= XFS_IFORK_DSIZE(ip)) {
  264. xfs_init_local_fork(ip, XFS_DATA_FORK, target_path, pathlen);
  265. ip->i_d.di_size = pathlen;
  266. ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
  267. xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
  268. } else {
  269. int offset;
  270. first_fsb = 0;
  271. nmaps = XFS_SYMLINK_MAPS;
  272. error = xfs_bmapi_write(tp, ip, first_fsb, fs_blocks,
  273. XFS_BMAPI_METADATA, &first_block, resblks,
  274. mval, &nmaps, &dfops);
  275. if (error)
  276. goto out_bmap_cancel;
  277. if (resblks)
  278. resblks -= fs_blocks;
  279. ip->i_d.di_size = pathlen;
  280. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  281. cur_chunk = target_path;
  282. offset = 0;
  283. for (n = 0; n < nmaps; n++) {
  284. char *buf;
  285. d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
  286. byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
  287. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
  288. BTOBB(byte_cnt), 0);
  289. if (!bp) {
  290. error = -ENOMEM;
  291. goto out_bmap_cancel;
  292. }
  293. bp->b_ops = &xfs_symlink_buf_ops;
  294. byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt);
  295. byte_cnt = min(byte_cnt, pathlen);
  296. buf = bp->b_addr;
  297. buf += xfs_symlink_hdr_set(mp, ip->i_ino, offset,
  298. byte_cnt, bp);
  299. memcpy(buf, cur_chunk, byte_cnt);
  300. cur_chunk += byte_cnt;
  301. pathlen -= byte_cnt;
  302. offset += byte_cnt;
  303. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SYMLINK_BUF);
  304. xfs_trans_log_buf(tp, bp, 0, (buf + byte_cnt - 1) -
  305. (char *)bp->b_addr);
  306. }
  307. ASSERT(pathlen == 0);
  308. }
  309. /*
  310. * Create the directory entry for the symlink.
  311. */
  312. error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
  313. &first_block, &dfops, resblks);
  314. if (error)
  315. goto out_bmap_cancel;
  316. xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  317. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  318. /*
  319. * If this is a synchronous mount, make sure that the
  320. * symlink transaction goes to disk before returning to
  321. * the user.
  322. */
  323. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  324. xfs_trans_set_sync(tp);
  325. }
  326. error = xfs_defer_finish(&tp, &dfops);
  327. if (error)
  328. goto out_bmap_cancel;
  329. error = xfs_trans_commit(tp);
  330. if (error)
  331. goto out_release_inode;
  332. xfs_qm_dqrele(udqp);
  333. xfs_qm_dqrele(gdqp);
  334. xfs_qm_dqrele(pdqp);
  335. *ipp = ip;
  336. return 0;
  337. out_bmap_cancel:
  338. xfs_defer_cancel(&dfops);
  339. out_trans_cancel:
  340. xfs_trans_cancel(tp);
  341. out_release_inode:
  342. /*
  343. * Wait until after the current transaction is aborted to finish the
  344. * setup of the inode and release the inode. This prevents recursive
  345. * transactions and deadlocks from xfs_inactive.
  346. */
  347. if (ip) {
  348. xfs_finish_inode_setup(ip);
  349. IRELE(ip);
  350. }
  351. xfs_qm_dqrele(udqp);
  352. xfs_qm_dqrele(gdqp);
  353. xfs_qm_dqrele(pdqp);
  354. if (unlock_dp_on_error)
  355. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  356. return error;
  357. }
  358. /*
  359. * Free a symlink that has blocks associated with it.
  360. */
  361. STATIC int
  362. xfs_inactive_symlink_rmt(
  363. struct xfs_inode *ip)
  364. {
  365. xfs_buf_t *bp;
  366. int done;
  367. int error;
  368. xfs_fsblock_t first_block;
  369. struct xfs_defer_ops dfops;
  370. int i;
  371. xfs_mount_t *mp;
  372. xfs_bmbt_irec_t mval[XFS_SYMLINK_MAPS];
  373. int nmaps;
  374. int size;
  375. xfs_trans_t *tp;
  376. mp = ip->i_mount;
  377. ASSERT(ip->i_df.if_flags & XFS_IFEXTENTS);
  378. /*
  379. * We're freeing a symlink that has some
  380. * blocks allocated to it. Free the
  381. * blocks here. We know that we've got
  382. * either 1 or 2 extents and that we can
  383. * free them all in one bunmapi call.
  384. */
  385. ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
  386. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
  387. if (error)
  388. return error;
  389. xfs_ilock(ip, XFS_ILOCK_EXCL);
  390. xfs_trans_ijoin(tp, ip, 0);
  391. /*
  392. * Lock the inode, fix the size, and join it to the transaction.
  393. * Hold it so in the normal path, we still have it locked for
  394. * the second transaction. In the error paths we need it
  395. * held so the cancel won't rele it, see below.
  396. */
  397. size = (int)ip->i_d.di_size;
  398. ip->i_d.di_size = 0;
  399. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  400. /*
  401. * Find the block(s) so we can inval and unmap them.
  402. */
  403. done = 0;
  404. xfs_defer_init(&dfops, &first_block);
  405. nmaps = ARRAY_SIZE(mval);
  406. error = xfs_bmapi_read(ip, 0, xfs_symlink_blocks(mp, size),
  407. mval, &nmaps, 0);
  408. if (error)
  409. goto error_trans_cancel;
  410. /*
  411. * Invalidate the block(s). No validation is done.
  412. */
  413. for (i = 0; i < nmaps; i++) {
  414. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
  415. XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
  416. XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
  417. if (!bp) {
  418. error = -ENOMEM;
  419. goto error_bmap_cancel;
  420. }
  421. xfs_trans_binval(tp, bp);
  422. }
  423. /*
  424. * Unmap the dead block(s) to the dfops.
  425. */
  426. error = xfs_bunmapi(tp, ip, 0, size, 0, nmaps,
  427. &first_block, &dfops, &done);
  428. if (error)
  429. goto error_bmap_cancel;
  430. ASSERT(done);
  431. /*
  432. * Commit the first transaction. This logs the EFI and the inode.
  433. */
  434. xfs_defer_ijoin(&dfops, ip);
  435. error = xfs_defer_finish(&tp, &dfops);
  436. if (error)
  437. goto error_bmap_cancel;
  438. /*
  439. * The first xact was committed, so add the inode to the new one.
  440. * Mark it dirty so it will be logged and moved forward in the log as
  441. * part of every commit.
  442. */
  443. xfs_trans_ijoin(tp, ip, 0);
  444. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  445. /*
  446. * Commit the transaction containing extent freeing and EFDs.
  447. */
  448. error = xfs_trans_commit(tp);
  449. if (error) {
  450. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  451. goto error_unlock;
  452. }
  453. /*
  454. * Remove the memory for extent descriptions (just bookkeeping).
  455. */
  456. if (ip->i_df.if_bytes)
  457. xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
  458. ASSERT(ip->i_df.if_bytes == 0);
  459. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  460. return 0;
  461. error_bmap_cancel:
  462. xfs_defer_cancel(&dfops);
  463. error_trans_cancel:
  464. xfs_trans_cancel(tp);
  465. error_unlock:
  466. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  467. return error;
  468. }
  469. /*
  470. * xfs_inactive_symlink - free a symlink
  471. */
  472. int
  473. xfs_inactive_symlink(
  474. struct xfs_inode *ip)
  475. {
  476. struct xfs_mount *mp = ip->i_mount;
  477. int pathlen;
  478. trace_xfs_inactive_symlink(ip);
  479. if (XFS_FORCED_SHUTDOWN(mp))
  480. return -EIO;
  481. xfs_ilock(ip, XFS_ILOCK_EXCL);
  482. /*
  483. * Zero length symlinks _can_ exist.
  484. */
  485. pathlen = (int)ip->i_d.di_size;
  486. if (!pathlen) {
  487. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  488. return 0;
  489. }
  490. if (pathlen < 0 || pathlen > XFS_SYMLINK_MAXLEN) {
  491. xfs_alert(mp, "%s: inode (0x%llx) bad symlink length (%d)",
  492. __func__, (unsigned long long)ip->i_ino, pathlen);
  493. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  494. ASSERT(0);
  495. return -EFSCORRUPTED;
  496. }
  497. if (ip->i_df.if_flags & XFS_IFINLINE) {
  498. if (ip->i_df.if_bytes > 0)
  499. xfs_idata_realloc(ip, -(ip->i_df.if_bytes),
  500. XFS_DATA_FORK);
  501. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  502. ASSERT(ip->i_df.if_bytes == 0);
  503. return 0;
  504. }
  505. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  506. /* remove the remote symlink */
  507. return xfs_inactive_symlink_rmt(ip);
  508. }