xfs_symlink.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. #include "xfs_ialloc.h"
  25. #include "xfs_error.h"
  26. #include "xfs_health.h"
  27. #include "xfs_symlink_remote.h"
  28. #include "xfs_parent.h"
  29. #include "xfs_defer.h"
  30. int
  31. xfs_readlink(
  32. struct xfs_inode *ip,
  33. char *link)
  34. {
  35. struct xfs_mount *mp = ip->i_mount;
  36. xfs_fsize_t pathlen;
  37. int error;
  38. trace_xfs_readlink(ip);
  39. if (xfs_is_shutdown(mp))
  40. return -EIO;
  41. if (xfs_ifork_zapped(ip, XFS_DATA_FORK))
  42. return -EIO;
  43. xfs_ilock(ip, XFS_ILOCK_SHARED);
  44. pathlen = ip->i_disk_size;
  45. if (!pathlen)
  46. goto out_corrupt;
  47. if (pathlen < 0 || pathlen > XFS_SYMLINK_MAXLEN) {
  48. xfs_alert(mp, "%s: inode (%llu) bad symlink length (%lld)",
  49. __func__, (unsigned long long) ip->i_ino,
  50. (long long) pathlen);
  51. ASSERT(0);
  52. goto out_corrupt;
  53. }
  54. if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
  55. /*
  56. * The VFS crashes on a NULL pointer, so return -EFSCORRUPTED
  57. * if if_data is junk.
  58. */
  59. if (XFS_IS_CORRUPT(ip->i_mount, !ip->i_df.if_data))
  60. goto out_corrupt;
  61. memcpy(link, ip->i_df.if_data, pathlen + 1);
  62. error = 0;
  63. } else {
  64. error = xfs_symlink_remote_read(ip, link);
  65. }
  66. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  67. return error;
  68. out_corrupt:
  69. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  70. xfs_inode_mark_sick(ip, XFS_SICK_INO_SYMLINK);
  71. return -EFSCORRUPTED;
  72. }
  73. int
  74. xfs_symlink(
  75. struct mnt_idmap *idmap,
  76. struct xfs_inode *dp,
  77. struct xfs_name *link_name,
  78. const char *target_path,
  79. umode_t mode,
  80. struct xfs_inode **ipp)
  81. {
  82. struct xfs_mount *mp = dp->i_mount;
  83. struct xfs_icreate_args args = {
  84. .idmap = idmap,
  85. .pip = dp,
  86. .mode = S_IFLNK | (mode & ~S_IFMT),
  87. };
  88. struct xfs_dir_update du = {
  89. .dp = dp,
  90. .name = link_name,
  91. };
  92. struct xfs_trans *tp = NULL;
  93. int error = 0;
  94. int pathlen;
  95. bool unlock_dp_on_error = false;
  96. xfs_filblks_t fs_blocks;
  97. struct xfs_dquot *udqp;
  98. struct xfs_dquot *gdqp;
  99. struct xfs_dquot *pdqp;
  100. uint resblks;
  101. xfs_ino_t ino;
  102. *ipp = NULL;
  103. trace_xfs_symlink(dp, link_name);
  104. if (xfs_is_shutdown(mp))
  105. return -EIO;
  106. /*
  107. * Check component lengths of the target path name.
  108. */
  109. pathlen = strlen(target_path);
  110. if (pathlen >= XFS_SYMLINK_MAXLEN) /* total string too long */
  111. return -ENAMETOOLONG;
  112. ASSERT(pathlen > 0);
  113. /* Make sure that we have allocated dquot(s) on disk. */
  114. error = xfs_icreate_dqalloc(&args, &udqp, &gdqp, &pdqp);
  115. if (error)
  116. return error;
  117. /*
  118. * The symlink will fit into the inode data fork?
  119. * If there are no parent pointers, then there wont't be any attributes.
  120. * So we get the whole variable part, and do not need to reserve extra
  121. * blocks. Otherwise, we need to reserve the blocks.
  122. */
  123. if (pathlen <= XFS_LITINO(mp) && !xfs_has_parent(mp))
  124. fs_blocks = 0;
  125. else
  126. fs_blocks = xfs_symlink_blocks(mp, pathlen);
  127. resblks = xfs_symlink_space_res(mp, link_name->len, fs_blocks);
  128. error = xfs_parent_start(mp, &du.ppargs);
  129. if (error)
  130. goto out_release_dquots;
  131. error = xfs_trans_alloc_icreate(mp, &M_RES(mp)->tr_symlink, udqp, gdqp,
  132. pdqp, resblks, &tp);
  133. if (error)
  134. goto out_parent;
  135. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  136. unlock_dp_on_error = true;
  137. /*
  138. * Check whether the directory allows new symlinks or not.
  139. */
  140. if (dp->i_diflags & XFS_DIFLAG_NOSYMLINKS) {
  141. error = -EPERM;
  142. goto out_trans_cancel;
  143. }
  144. /*
  145. * Allocate an inode for the symlink.
  146. */
  147. error = xfs_dialloc(&tp, &args, &ino);
  148. if (!error)
  149. error = xfs_icreate(tp, ino, &args, &du.ip);
  150. if (error)
  151. goto out_trans_cancel;
  152. /*
  153. * Now we join the directory inode to the transaction. We do not do it
  154. * earlier because xfs_dir_ialloc might commit the previous transaction
  155. * (and release all the locks). An error from here on will result in
  156. * the transaction cancel unlocking dp so don't do it explicitly in the
  157. * error path.
  158. */
  159. xfs_trans_ijoin(tp, dp, 0);
  160. /*
  161. * Also attach the dquot(s) to it, if applicable.
  162. */
  163. xfs_qm_vop_create_dqattach(tp, du.ip, udqp, gdqp, pdqp);
  164. resblks -= XFS_IALLOC_SPACE_RES(mp);
  165. error = xfs_symlink_write_target(tp, du.ip, du.ip->i_ino, target_path,
  166. pathlen, fs_blocks, resblks);
  167. if (error)
  168. goto out_trans_cancel;
  169. resblks -= fs_blocks;
  170. i_size_write(VFS_I(du.ip), du.ip->i_disk_size);
  171. /*
  172. * Create the directory entry for the symlink.
  173. */
  174. error = xfs_dir_create_child(tp, resblks, &du);
  175. if (error)
  176. goto out_trans_cancel;
  177. /*
  178. * If this is a synchronous mount, make sure that the
  179. * symlink transaction goes to disk before returning to
  180. * the user.
  181. */
  182. if (xfs_has_wsync(mp) || xfs_has_dirsync(mp))
  183. xfs_trans_set_sync(tp);
  184. error = xfs_trans_commit(tp);
  185. if (error)
  186. goto out_release_inode;
  187. xfs_qm_dqrele(udqp);
  188. xfs_qm_dqrele(gdqp);
  189. xfs_qm_dqrele(pdqp);
  190. *ipp = du.ip;
  191. xfs_iunlock(du.ip, XFS_ILOCK_EXCL);
  192. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  193. xfs_parent_finish(mp, du.ppargs);
  194. return 0;
  195. out_trans_cancel:
  196. xfs_trans_cancel(tp);
  197. out_release_inode:
  198. /*
  199. * Wait until after the current transaction is aborted to finish the
  200. * setup of the inode and release the inode. This prevents recursive
  201. * transactions and deadlocks from xfs_inactive.
  202. */
  203. if (du.ip) {
  204. xfs_iunlock(du.ip, XFS_ILOCK_EXCL);
  205. xfs_finish_inode_setup(du.ip);
  206. xfs_irele(du.ip);
  207. }
  208. out_parent:
  209. xfs_parent_finish(mp, du.ppargs);
  210. out_release_dquots:
  211. xfs_qm_dqrele(udqp);
  212. xfs_qm_dqrele(gdqp);
  213. xfs_qm_dqrele(pdqp);
  214. if (unlock_dp_on_error)
  215. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  216. return error;
  217. }
  218. /*
  219. * Free a symlink that has blocks associated with it.
  220. *
  221. * Note: zero length symlinks are not allowed to exist. When we set the size to
  222. * zero, also change it to a regular file so that it does not get written to
  223. * disk as a zero length symlink. The inode is on the unlinked list already, so
  224. * userspace cannot find this inode anymore, so this change is not user visible
  225. * but allows us to catch corrupt zero-length symlinks in the verifiers.
  226. */
  227. STATIC int
  228. xfs_inactive_symlink_rmt(
  229. struct xfs_inode *ip)
  230. {
  231. struct xfs_mount *mp = ip->i_mount;
  232. struct xfs_trans *tp;
  233. int error;
  234. ASSERT(!xfs_need_iread_extents(&ip->i_df));
  235. /*
  236. * We're freeing a symlink that has some
  237. * blocks allocated to it. Free the
  238. * blocks here. We know that we've got
  239. * either 1 or 2 extents and that we can
  240. * free them all in one bunmapi call.
  241. */
  242. ASSERT(ip->i_df.if_nextents > 0 && ip->i_df.if_nextents <= 2);
  243. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
  244. if (error)
  245. return error;
  246. xfs_ilock(ip, XFS_ILOCK_EXCL);
  247. xfs_trans_ijoin(tp, ip, 0);
  248. /*
  249. * Lock the inode, fix the size, turn it into a regular file and join it
  250. * to the transaction. Hold it so in the normal path, we still have it
  251. * locked for the second transaction. In the error paths we need it
  252. * held so the cancel won't rele it, see below.
  253. */
  254. ip->i_disk_size = 0;
  255. VFS_I(ip)->i_mode = (VFS_I(ip)->i_mode & ~S_IFMT) | S_IFREG;
  256. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  257. error = xfs_symlink_remote_truncate(tp, ip);
  258. if (error)
  259. goto error_trans_cancel;
  260. error = xfs_trans_commit(tp);
  261. if (error) {
  262. ASSERT(xfs_is_shutdown(mp));
  263. goto error_unlock;
  264. }
  265. /*
  266. * Remove the memory for extent descriptions (just bookkeeping).
  267. */
  268. if (ip->i_df.if_bytes)
  269. xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
  270. ASSERT(ip->i_df.if_bytes == 0);
  271. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  272. return 0;
  273. error_trans_cancel:
  274. xfs_trans_cancel(tp);
  275. error_unlock:
  276. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  277. return error;
  278. }
  279. /*
  280. * xfs_inactive_symlink - free a symlink
  281. */
  282. int
  283. xfs_inactive_symlink(
  284. struct xfs_inode *ip)
  285. {
  286. struct xfs_mount *mp = ip->i_mount;
  287. int pathlen;
  288. trace_xfs_inactive_symlink(ip);
  289. if (xfs_is_shutdown(mp))
  290. return -EIO;
  291. xfs_ilock(ip, XFS_ILOCK_EXCL);
  292. pathlen = (int)ip->i_disk_size;
  293. ASSERT(pathlen);
  294. if (pathlen <= 0 || pathlen > XFS_SYMLINK_MAXLEN) {
  295. xfs_alert(mp, "%s: inode (0x%llx) bad symlink length (%d)",
  296. __func__, (unsigned long long)ip->i_ino, pathlen);
  297. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  298. ASSERT(0);
  299. xfs_inode_mark_sick(ip, XFS_SICK_INO_SYMLINK);
  300. return -EFSCORRUPTED;
  301. }
  302. /*
  303. * Inline fork state gets removed by xfs_difree() so we have nothing to
  304. * do here in that case.
  305. */
  306. if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
  307. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  308. return 0;
  309. }
  310. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  311. /* remove the remote symlink */
  312. return xfs_inactive_symlink_rmt(ip);
  313. }