xfs_symlink.c 12 KB

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