xfs_symlink.c 14 KB

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