xfs_symlink.c 12 KB

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