PageRenderTime 64ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/linux-2.6.33/kernel/linux-2.6.33/fs/xfs/xfs_attr.c

https://bitbucket.org/microcreat/cortexm_uclinux
C | 2265 lines | 1418 code | 234 blank | 613 comment | 329 complexity | b3505562027595ed7bdd046256a76f02 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_types.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_dir2.h"
  28. #include "xfs_dmapi.h"
  29. #include "xfs_mount.h"
  30. #include "xfs_da_btree.h"
  31. #include "xfs_bmap_btree.h"
  32. #include "xfs_alloc_btree.h"
  33. #include "xfs_ialloc_btree.h"
  34. #include "xfs_dir2_sf.h"
  35. #include "xfs_attr_sf.h"
  36. #include "xfs_dinode.h"
  37. #include "xfs_inode.h"
  38. #include "xfs_alloc.h"
  39. #include "xfs_btree.h"
  40. #include "xfs_inode_item.h"
  41. #include "xfs_bmap.h"
  42. #include "xfs_attr.h"
  43. #include "xfs_attr_leaf.h"
  44. #include "xfs_error.h"
  45. #include "xfs_quota.h"
  46. #include "xfs_trans_space.h"
  47. #include "xfs_rw.h"
  48. #include "xfs_vnodeops.h"
  49. #include "xfs_trace.h"
  50. /*
  51. * xfs_attr.c
  52. *
  53. * Provide the external interfaces to manage attribute lists.
  54. */
  55. /*========================================================================
  56. * Function prototypes for the kernel.
  57. *========================================================================*/
  58. /*
  59. * Internal routines when attribute list fits inside the inode.
  60. */
  61. STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
  62. /*
  63. * Internal routines when attribute list is one block.
  64. */
  65. STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
  66. STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
  67. STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
  68. STATIC int xfs_attr_leaf_list(xfs_attr_list_context_t *context);
  69. /*
  70. * Internal routines when attribute list is more than one block.
  71. */
  72. STATIC int xfs_attr_node_get(xfs_da_args_t *args);
  73. STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
  74. STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
  75. STATIC int xfs_attr_node_list(xfs_attr_list_context_t *context);
  76. STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
  77. STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
  78. /*
  79. * Routines to manipulate out-of-line attribute values.
  80. */
  81. STATIC int xfs_attr_rmtval_set(xfs_da_args_t *args);
  82. STATIC int xfs_attr_rmtval_remove(xfs_da_args_t *args);
  83. #define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
  84. STATIC int
  85. xfs_attr_name_to_xname(
  86. struct xfs_name *xname,
  87. const char *aname)
  88. {
  89. if (!aname)
  90. return EINVAL;
  91. xname->name = aname;
  92. xname->len = strlen(aname);
  93. if (xname->len >= MAXNAMELEN)
  94. return EFAULT; /* match IRIX behaviour */
  95. return 0;
  96. }
  97. STATIC int
  98. xfs_inode_hasattr(
  99. struct xfs_inode *ip)
  100. {
  101. if (!XFS_IFORK_Q(ip) ||
  102. (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
  103. ip->i_d.di_anextents == 0))
  104. return 0;
  105. return 1;
  106. }
  107. /*========================================================================
  108. * Overall external interface routines.
  109. *========================================================================*/
  110. STATIC int
  111. xfs_attr_get_int(
  112. struct xfs_inode *ip,
  113. struct xfs_name *name,
  114. char *value,
  115. int *valuelenp,
  116. int flags)
  117. {
  118. xfs_da_args_t args;
  119. int error;
  120. if (!xfs_inode_hasattr(ip))
  121. return ENOATTR;
  122. /*
  123. * Fill in the arg structure for this request.
  124. */
  125. memset((char *)&args, 0, sizeof(args));
  126. args.name = name->name;
  127. args.namelen = name->len;
  128. args.value = value;
  129. args.valuelen = *valuelenp;
  130. args.flags = flags;
  131. args.hashval = xfs_da_hashname(args.name, args.namelen);
  132. args.dp = ip;
  133. args.whichfork = XFS_ATTR_FORK;
  134. /*
  135. * Decide on what work routines to call based on the inode size.
  136. */
  137. if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
  138. error = xfs_attr_shortform_getvalue(&args);
  139. } else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK)) {
  140. error = xfs_attr_leaf_get(&args);
  141. } else {
  142. error = xfs_attr_node_get(&args);
  143. }
  144. /*
  145. * Return the number of bytes in the value to the caller.
  146. */
  147. *valuelenp = args.valuelen;
  148. if (error == EEXIST)
  149. error = 0;
  150. return(error);
  151. }
  152. int
  153. xfs_attr_get(
  154. xfs_inode_t *ip,
  155. const char *name,
  156. char *value,
  157. int *valuelenp,
  158. int flags)
  159. {
  160. int error;
  161. struct xfs_name xname;
  162. XFS_STATS_INC(xs_attr_get);
  163. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  164. return(EIO);
  165. error = xfs_attr_name_to_xname(&xname, name);
  166. if (error)
  167. return error;
  168. xfs_ilock(ip, XFS_ILOCK_SHARED);
  169. error = xfs_attr_get_int(ip, &xname, value, valuelenp, flags);
  170. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  171. return(error);
  172. }
  173. /*
  174. * Calculate how many blocks we need for the new attribute,
  175. */
  176. int
  177. xfs_attr_calc_size(
  178. struct xfs_inode *ip,
  179. int namelen,
  180. int valuelen,
  181. int *local)
  182. {
  183. struct xfs_mount *mp = ip->i_mount;
  184. int size;
  185. int nblks;
  186. /*
  187. * Determine space new attribute will use, and if it would be
  188. * "local" or "remote" (note: local != inline).
  189. */
  190. size = xfs_attr_leaf_newentsize(namelen, valuelen,
  191. mp->m_sb.sb_blocksize, local);
  192. nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
  193. if (*local) {
  194. if (size > (mp->m_sb.sb_blocksize >> 1)) {
  195. /* Double split possible */
  196. nblks *= 2;
  197. }
  198. } else {
  199. /*
  200. * Out of line attribute, cannot double split, but
  201. * make room for the attribute value itself.
  202. */
  203. uint dblocks = XFS_B_TO_FSB(mp, valuelen);
  204. nblks += dblocks;
  205. nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
  206. }
  207. return nblks;
  208. }
  209. STATIC int
  210. xfs_attr_set_int(xfs_inode_t *dp, struct xfs_name *name,
  211. char *value, int valuelen, int flags)
  212. {
  213. xfs_da_args_t args;
  214. xfs_fsblock_t firstblock;
  215. xfs_bmap_free_t flist;
  216. int error, err2, committed;
  217. xfs_mount_t *mp = dp->i_mount;
  218. int rsvd = (flags & ATTR_ROOT) != 0;
  219. int local;
  220. /*
  221. * Attach the dquots to the inode.
  222. */
  223. error = xfs_qm_dqattach(dp, 0);
  224. if (error)
  225. return error;
  226. /*
  227. * If the inode doesn't have an attribute fork, add one.
  228. * (inode must not be locked when we call this routine)
  229. */
  230. if (XFS_IFORK_Q(dp) == 0) {
  231. int sf_size = sizeof(xfs_attr_sf_hdr_t) +
  232. XFS_ATTR_SF_ENTSIZE_BYNAME(name->len, valuelen);
  233. if ((error = xfs_bmap_add_attrfork(dp, sf_size, rsvd)))
  234. return(error);
  235. }
  236. /*
  237. * Fill in the arg structure for this request.
  238. */
  239. memset((char *)&args, 0, sizeof(args));
  240. args.name = name->name;
  241. args.namelen = name->len;
  242. args.value = value;
  243. args.valuelen = valuelen;
  244. args.flags = flags;
  245. args.hashval = xfs_da_hashname(args.name, args.namelen);
  246. args.dp = dp;
  247. args.firstblock = &firstblock;
  248. args.flist = &flist;
  249. args.whichfork = XFS_ATTR_FORK;
  250. args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
  251. /* Size is now blocks for attribute data */
  252. args.total = xfs_attr_calc_size(dp, name->len, valuelen, &local);
  253. /*
  254. * Start our first transaction of the day.
  255. *
  256. * All future transactions during this code must be "chained" off
  257. * this one via the trans_dup() call. All transactions will contain
  258. * the inode, and the inode will always be marked with trans_ihold().
  259. * Since the inode will be locked in all transactions, we must log
  260. * the inode in every transaction to let it float upward through
  261. * the log.
  262. */
  263. args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);
  264. /*
  265. * Root fork attributes can use reserved data blocks for this
  266. * operation if necessary
  267. */
  268. if (rsvd)
  269. args.trans->t_flags |= XFS_TRANS_RESERVE;
  270. if ((error = xfs_trans_reserve(args.trans, args.total,
  271. XFS_ATTRSET_LOG_RES(mp, args.total), 0,
  272. XFS_TRANS_PERM_LOG_RES, XFS_ATTRSET_LOG_COUNT))) {
  273. xfs_trans_cancel(args.trans, 0);
  274. return(error);
  275. }
  276. xfs_ilock(dp, XFS_ILOCK_EXCL);
  277. error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
  278. rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
  279. XFS_QMOPT_RES_REGBLKS);
  280. if (error) {
  281. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  282. xfs_trans_cancel(args.trans, XFS_TRANS_RELEASE_LOG_RES);
  283. return (error);
  284. }
  285. xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
  286. xfs_trans_ihold(args.trans, dp);
  287. /*
  288. * If the attribute list is non-existent or a shortform list,
  289. * upgrade it to a single-leaf-block attribute list.
  290. */
  291. if ((dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
  292. ((dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) &&
  293. (dp->i_d.di_anextents == 0))) {
  294. /*
  295. * Build initial attribute list (if required).
  296. */
  297. if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
  298. xfs_attr_shortform_create(&args);
  299. /*
  300. * Try to add the attr to the attribute list in
  301. * the inode.
  302. */
  303. error = xfs_attr_shortform_addname(&args);
  304. if (error != ENOSPC) {
  305. /*
  306. * Commit the shortform mods, and we're done.
  307. * NOTE: this is also the error path (EEXIST, etc).
  308. */
  309. ASSERT(args.trans != NULL);
  310. /*
  311. * If this is a synchronous mount, make sure that
  312. * the transaction goes to disk before returning
  313. * to the user.
  314. */
  315. if (mp->m_flags & XFS_MOUNT_WSYNC) {
  316. xfs_trans_set_sync(args.trans);
  317. }
  318. err2 = xfs_trans_commit(args.trans,
  319. XFS_TRANS_RELEASE_LOG_RES);
  320. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  321. /*
  322. * Hit the inode change time.
  323. */
  324. if (!error && (flags & ATTR_KERNOTIME) == 0) {
  325. xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
  326. }
  327. return(error == 0 ? err2 : error);
  328. }
  329. /*
  330. * It won't fit in the shortform, transform to a leaf block.
  331. * GROT: another possible req'mt for a double-split btree op.
  332. */
  333. xfs_bmap_init(args.flist, args.firstblock);
  334. error = xfs_attr_shortform_to_leaf(&args);
  335. if (!error) {
  336. error = xfs_bmap_finish(&args.trans, args.flist,
  337. &committed);
  338. }
  339. if (error) {
  340. ASSERT(committed);
  341. args.trans = NULL;
  342. xfs_bmap_cancel(&flist);
  343. goto out;
  344. }
  345. /*
  346. * bmap_finish() may have committed the last trans and started
  347. * a new one. We need the inode to be in all transactions.
  348. */
  349. if (committed) {
  350. xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
  351. xfs_trans_ihold(args.trans, dp);
  352. }
  353. /*
  354. * Commit the leaf transformation. We'll need another (linked)
  355. * transaction to add the new attribute to the leaf.
  356. */
  357. error = xfs_trans_roll(&args.trans, dp);
  358. if (error)
  359. goto out;
  360. }
  361. if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  362. error = xfs_attr_leaf_addname(&args);
  363. } else {
  364. error = xfs_attr_node_addname(&args);
  365. }
  366. if (error) {
  367. goto out;
  368. }
  369. /*
  370. * If this is a synchronous mount, make sure that the
  371. * transaction goes to disk before returning to the user.
  372. */
  373. if (mp->m_flags & XFS_MOUNT_WSYNC) {
  374. xfs_trans_set_sync(args.trans);
  375. }
  376. /*
  377. * Commit the last in the sequence of transactions.
  378. */
  379. xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
  380. error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
  381. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  382. /*
  383. * Hit the inode change time.
  384. */
  385. if (!error && (flags & ATTR_KERNOTIME) == 0) {
  386. xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
  387. }
  388. return(error);
  389. out:
  390. if (args.trans)
  391. xfs_trans_cancel(args.trans,
  392. XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  393. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  394. return(error);
  395. }
  396. int
  397. xfs_attr_set(
  398. xfs_inode_t *dp,
  399. const char *name,
  400. char *value,
  401. int valuelen,
  402. int flags)
  403. {
  404. int error;
  405. struct xfs_name xname;
  406. XFS_STATS_INC(xs_attr_set);
  407. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  408. return (EIO);
  409. error = xfs_attr_name_to_xname(&xname, name);
  410. if (error)
  411. return error;
  412. return xfs_attr_set_int(dp, &xname, value, valuelen, flags);
  413. }
  414. /*
  415. * Generic handler routine to remove a name from an attribute list.
  416. * Transitions attribute list from Btree to shortform as necessary.
  417. */
  418. STATIC int
  419. xfs_attr_remove_int(xfs_inode_t *dp, struct xfs_name *name, int flags)
  420. {
  421. xfs_da_args_t args;
  422. xfs_fsblock_t firstblock;
  423. xfs_bmap_free_t flist;
  424. int error;
  425. xfs_mount_t *mp = dp->i_mount;
  426. /*
  427. * Fill in the arg structure for this request.
  428. */
  429. memset((char *)&args, 0, sizeof(args));
  430. args.name = name->name;
  431. args.namelen = name->len;
  432. args.flags = flags;
  433. args.hashval = xfs_da_hashname(args.name, args.namelen);
  434. args.dp = dp;
  435. args.firstblock = &firstblock;
  436. args.flist = &flist;
  437. args.total = 0;
  438. args.whichfork = XFS_ATTR_FORK;
  439. /*
  440. * Attach the dquots to the inode.
  441. */
  442. error = xfs_qm_dqattach(dp, 0);
  443. if (error)
  444. return error;
  445. /*
  446. * Start our first transaction of the day.
  447. *
  448. * All future transactions during this code must be "chained" off
  449. * this one via the trans_dup() call. All transactions will contain
  450. * the inode, and the inode will always be marked with trans_ihold().
  451. * Since the inode will be locked in all transactions, we must log
  452. * the inode in every transaction to let it float upward through
  453. * the log.
  454. */
  455. args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
  456. /*
  457. * Root fork attributes can use reserved data blocks for this
  458. * operation if necessary
  459. */
  460. if (flags & ATTR_ROOT)
  461. args.trans->t_flags |= XFS_TRANS_RESERVE;
  462. if ((error = xfs_trans_reserve(args.trans,
  463. XFS_ATTRRM_SPACE_RES(mp),
  464. XFS_ATTRRM_LOG_RES(mp),
  465. 0, XFS_TRANS_PERM_LOG_RES,
  466. XFS_ATTRRM_LOG_COUNT))) {
  467. xfs_trans_cancel(args.trans, 0);
  468. return(error);
  469. }
  470. xfs_ilock(dp, XFS_ILOCK_EXCL);
  471. /*
  472. * No need to make quota reservations here. We expect to release some
  473. * blocks not allocate in the common case.
  474. */
  475. xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
  476. xfs_trans_ihold(args.trans, dp);
  477. /*
  478. * Decide on what work routines to call based on the inode size.
  479. */
  480. if (!xfs_inode_hasattr(dp)) {
  481. error = XFS_ERROR(ENOATTR);
  482. goto out;
  483. }
  484. if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
  485. ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
  486. error = xfs_attr_shortform_remove(&args);
  487. if (error) {
  488. goto out;
  489. }
  490. } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  491. error = xfs_attr_leaf_removename(&args);
  492. } else {
  493. error = xfs_attr_node_removename(&args);
  494. }
  495. if (error) {
  496. goto out;
  497. }
  498. /*
  499. * If this is a synchronous mount, make sure that the
  500. * transaction goes to disk before returning to the user.
  501. */
  502. if (mp->m_flags & XFS_MOUNT_WSYNC) {
  503. xfs_trans_set_sync(args.trans);
  504. }
  505. /*
  506. * Commit the last in the sequence of transactions.
  507. */
  508. xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
  509. error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
  510. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  511. /*
  512. * Hit the inode change time.
  513. */
  514. if (!error && (flags & ATTR_KERNOTIME) == 0) {
  515. xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
  516. }
  517. return(error);
  518. out:
  519. if (args.trans)
  520. xfs_trans_cancel(args.trans,
  521. XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  522. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  523. return(error);
  524. }
  525. int
  526. xfs_attr_remove(
  527. xfs_inode_t *dp,
  528. const char *name,
  529. int flags)
  530. {
  531. int error;
  532. struct xfs_name xname;
  533. XFS_STATS_INC(xs_attr_remove);
  534. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  535. return (EIO);
  536. error = xfs_attr_name_to_xname(&xname, name);
  537. if (error)
  538. return error;
  539. xfs_ilock(dp, XFS_ILOCK_SHARED);
  540. if (!xfs_inode_hasattr(dp)) {
  541. xfs_iunlock(dp, XFS_ILOCK_SHARED);
  542. return XFS_ERROR(ENOATTR);
  543. }
  544. xfs_iunlock(dp, XFS_ILOCK_SHARED);
  545. return xfs_attr_remove_int(dp, &xname, flags);
  546. }
  547. int
  548. xfs_attr_list_int(xfs_attr_list_context_t *context)
  549. {
  550. int error;
  551. xfs_inode_t *dp = context->dp;
  552. XFS_STATS_INC(xs_attr_list);
  553. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  554. return EIO;
  555. xfs_ilock(dp, XFS_ILOCK_SHARED);
  556. /*
  557. * Decide on what work routines to call based on the inode size.
  558. */
  559. if (!xfs_inode_hasattr(dp)) {
  560. error = 0;
  561. } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
  562. error = xfs_attr_shortform_list(context);
  563. } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  564. error = xfs_attr_leaf_list(context);
  565. } else {
  566. error = xfs_attr_node_list(context);
  567. }
  568. xfs_iunlock(dp, XFS_ILOCK_SHARED);
  569. return error;
  570. }
  571. #define ATTR_ENTBASESIZE /* minimum bytes used by an attr */ \
  572. (((struct attrlist_ent *) 0)->a_name - (char *) 0)
  573. #define ATTR_ENTSIZE(namelen) /* actual bytes used by an attr */ \
  574. ((ATTR_ENTBASESIZE + (namelen) + 1 + sizeof(u_int32_t)-1) \
  575. & ~(sizeof(u_int32_t)-1))
  576. /*
  577. * Format an attribute and copy it out to the user's buffer.
  578. * Take care to check values and protect against them changing later,
  579. * we may be reading them directly out of a user buffer.
  580. */
  581. /*ARGSUSED*/
  582. STATIC int
  583. xfs_attr_put_listent(xfs_attr_list_context_t *context, int flags,
  584. char *name, int namelen,
  585. int valuelen, char *value)
  586. {
  587. struct attrlist *alist = (struct attrlist *)context->alist;
  588. attrlist_ent_t *aep;
  589. int arraytop;
  590. ASSERT(!(context->flags & ATTR_KERNOVAL));
  591. ASSERT(context->count >= 0);
  592. ASSERT(context->count < (ATTR_MAX_VALUELEN/8));
  593. ASSERT(context->firstu >= sizeof(*alist));
  594. ASSERT(context->firstu <= context->bufsize);
  595. /*
  596. * Only list entries in the right namespace.
  597. */
  598. if (((context->flags & ATTR_SECURE) == 0) !=
  599. ((flags & XFS_ATTR_SECURE) == 0))
  600. return 0;
  601. if (((context->flags & ATTR_ROOT) == 0) !=
  602. ((flags & XFS_ATTR_ROOT) == 0))
  603. return 0;
  604. arraytop = sizeof(*alist) +
  605. context->count * sizeof(alist->al_offset[0]);
  606. context->firstu -= ATTR_ENTSIZE(namelen);
  607. if (context->firstu < arraytop) {
  608. trace_xfs_attr_list_full(context);
  609. alist->al_more = 1;
  610. context->seen_enough = 1;
  611. return 1;
  612. }
  613. aep = (attrlist_ent_t *)&context->alist[context->firstu];
  614. aep->a_valuelen = valuelen;
  615. memcpy(aep->a_name, name, namelen);
  616. aep->a_name[namelen] = 0;
  617. alist->al_offset[context->count++] = context->firstu;
  618. alist->al_count = context->count;
  619. trace_xfs_attr_list_add(context);
  620. return 0;
  621. }
  622. /*
  623. * Generate a list of extended attribute names and optionally
  624. * also value lengths. Positive return value follows the XFS
  625. * convention of being an error, zero or negative return code
  626. * is the length of the buffer returned (negated), indicating
  627. * success.
  628. */
  629. int
  630. xfs_attr_list(
  631. xfs_inode_t *dp,
  632. char *buffer,
  633. int bufsize,
  634. int flags,
  635. attrlist_cursor_kern_t *cursor)
  636. {
  637. xfs_attr_list_context_t context;
  638. struct attrlist *alist;
  639. int error;
  640. /*
  641. * Validate the cursor.
  642. */
  643. if (cursor->pad1 || cursor->pad2)
  644. return(XFS_ERROR(EINVAL));
  645. if ((cursor->initted == 0) &&
  646. (cursor->hashval || cursor->blkno || cursor->offset))
  647. return XFS_ERROR(EINVAL);
  648. /*
  649. * Check for a properly aligned buffer.
  650. */
  651. if (((long)buffer) & (sizeof(int)-1))
  652. return XFS_ERROR(EFAULT);
  653. if (flags & ATTR_KERNOVAL)
  654. bufsize = 0;
  655. /*
  656. * Initialize the output buffer.
  657. */
  658. memset(&context, 0, sizeof(context));
  659. context.dp = dp;
  660. context.cursor = cursor;
  661. context.resynch = 1;
  662. context.flags = flags;
  663. context.alist = buffer;
  664. context.bufsize = (bufsize & ~(sizeof(int)-1)); /* align */
  665. context.firstu = context.bufsize;
  666. context.put_listent = xfs_attr_put_listent;
  667. alist = (struct attrlist *)context.alist;
  668. alist->al_count = 0;
  669. alist->al_more = 0;
  670. alist->al_offset[0] = context.bufsize;
  671. error = xfs_attr_list_int(&context);
  672. ASSERT(error >= 0);
  673. return error;
  674. }
  675. int /* error */
  676. xfs_attr_inactive(xfs_inode_t *dp)
  677. {
  678. xfs_trans_t *trans;
  679. xfs_mount_t *mp;
  680. int error;
  681. mp = dp->i_mount;
  682. ASSERT(! XFS_NOT_DQATTACHED(mp, dp));
  683. xfs_ilock(dp, XFS_ILOCK_SHARED);
  684. if (!xfs_inode_hasattr(dp) ||
  685. dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
  686. xfs_iunlock(dp, XFS_ILOCK_SHARED);
  687. return 0;
  688. }
  689. xfs_iunlock(dp, XFS_ILOCK_SHARED);
  690. /*
  691. * Start our first transaction of the day.
  692. *
  693. * All future transactions during this code must be "chained" off
  694. * this one via the trans_dup() call. All transactions will contain
  695. * the inode, and the inode will always be marked with trans_ihold().
  696. * Since the inode will be locked in all transactions, we must log
  697. * the inode in every transaction to let it float upward through
  698. * the log.
  699. */
  700. trans = xfs_trans_alloc(mp, XFS_TRANS_ATTRINVAL);
  701. if ((error = xfs_trans_reserve(trans, 0, XFS_ATTRINVAL_LOG_RES(mp), 0,
  702. XFS_TRANS_PERM_LOG_RES,
  703. XFS_ATTRINVAL_LOG_COUNT))) {
  704. xfs_trans_cancel(trans, 0);
  705. return(error);
  706. }
  707. xfs_ilock(dp, XFS_ILOCK_EXCL);
  708. /*
  709. * No need to make quota reservations here. We expect to release some
  710. * blocks, not allocate, in the common case.
  711. */
  712. xfs_trans_ijoin(trans, dp, XFS_ILOCK_EXCL);
  713. xfs_trans_ihold(trans, dp);
  714. /*
  715. * Decide on what work routines to call based on the inode size.
  716. */
  717. if (!xfs_inode_hasattr(dp) ||
  718. dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
  719. error = 0;
  720. goto out;
  721. }
  722. error = xfs_attr_root_inactive(&trans, dp);
  723. if (error)
  724. goto out;
  725. /*
  726. * signal synchronous inactive transactions unless this
  727. * is a synchronous mount filesystem in which case we
  728. * know that we're here because we've been called out of
  729. * xfs_inactive which means that the last reference is gone
  730. * and the unlink transaction has already hit the disk so
  731. * async inactive transactions are safe.
  732. */
  733. if ((error = xfs_itruncate_finish(&trans, dp, 0LL, XFS_ATTR_FORK,
  734. (!(mp->m_flags & XFS_MOUNT_WSYNC)
  735. ? 1 : 0))))
  736. goto out;
  737. /*
  738. * Commit the last in the sequence of transactions.
  739. */
  740. xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
  741. error = xfs_trans_commit(trans, XFS_TRANS_RELEASE_LOG_RES);
  742. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  743. return(error);
  744. out:
  745. xfs_trans_cancel(trans, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  746. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  747. return(error);
  748. }
  749. /*========================================================================
  750. * External routines when attribute list is inside the inode
  751. *========================================================================*/
  752. /*
  753. * Add a name to the shortform attribute list structure
  754. * This is the external routine.
  755. */
  756. STATIC int
  757. xfs_attr_shortform_addname(xfs_da_args_t *args)
  758. {
  759. int newsize, forkoff, retval;
  760. retval = xfs_attr_shortform_lookup(args);
  761. if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
  762. return(retval);
  763. } else if (retval == EEXIST) {
  764. if (args->flags & ATTR_CREATE)
  765. return(retval);
  766. retval = xfs_attr_shortform_remove(args);
  767. ASSERT(retval == 0);
  768. }
  769. if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
  770. args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
  771. return(XFS_ERROR(ENOSPC));
  772. newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
  773. newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
  774. forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
  775. if (!forkoff)
  776. return(XFS_ERROR(ENOSPC));
  777. xfs_attr_shortform_add(args, forkoff);
  778. return(0);
  779. }
  780. /*========================================================================
  781. * External routines when attribute list is one block
  782. *========================================================================*/
  783. /*
  784. * Add a name to the leaf attribute list structure
  785. *
  786. * This leaf block cannot have a "remote" value, we only call this routine
  787. * if bmap_one_block() says there is only one block (ie: no remote blks).
  788. */
  789. STATIC int
  790. xfs_attr_leaf_addname(xfs_da_args_t *args)
  791. {
  792. xfs_inode_t *dp;
  793. xfs_dabuf_t *bp;
  794. int retval, error, committed, forkoff;
  795. /*
  796. * Read the (only) block in the attribute list in.
  797. */
  798. dp = args->dp;
  799. args->blkno = 0;
  800. error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
  801. XFS_ATTR_FORK);
  802. if (error)
  803. return(error);
  804. ASSERT(bp != NULL);
  805. /*
  806. * Look up the given attribute in the leaf block. Figure out if
  807. * the given flags produce an error or call for an atomic rename.
  808. */
  809. retval = xfs_attr_leaf_lookup_int(bp, args);
  810. if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
  811. xfs_da_brelse(args->trans, bp);
  812. return(retval);
  813. } else if (retval == EEXIST) {
  814. if (args->flags & ATTR_CREATE) { /* pure create op */
  815. xfs_da_brelse(args->trans, bp);
  816. return(retval);
  817. }
  818. args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
  819. args->blkno2 = args->blkno; /* set 2nd entry info*/
  820. args->index2 = args->index;
  821. args->rmtblkno2 = args->rmtblkno;
  822. args->rmtblkcnt2 = args->rmtblkcnt;
  823. }
  824. /*
  825. * Add the attribute to the leaf block, transitioning to a Btree
  826. * if required.
  827. */
  828. retval = xfs_attr_leaf_add(bp, args);
  829. xfs_da_buf_done(bp);
  830. if (retval == ENOSPC) {
  831. /*
  832. * Promote the attribute list to the Btree format, then
  833. * Commit that transaction so that the node_addname() call
  834. * can manage its own transactions.
  835. */
  836. xfs_bmap_init(args->flist, args->firstblock);
  837. error = xfs_attr_leaf_to_node(args);
  838. if (!error) {
  839. error = xfs_bmap_finish(&args->trans, args->flist,
  840. &committed);
  841. }
  842. if (error) {
  843. ASSERT(committed);
  844. args->trans = NULL;
  845. xfs_bmap_cancel(args->flist);
  846. return(error);
  847. }
  848. /*
  849. * bmap_finish() may have committed the last trans and started
  850. * a new one. We need the inode to be in all transactions.
  851. */
  852. if (committed) {
  853. xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
  854. xfs_trans_ihold(args->trans, dp);
  855. }
  856. /*
  857. * Commit the current trans (including the inode) and start
  858. * a new one.
  859. */
  860. error = xfs_trans_roll(&args->trans, dp);
  861. if (error)
  862. return (error);
  863. /*
  864. * Fob the whole rest of the problem off on the Btree code.
  865. */
  866. error = xfs_attr_node_addname(args);
  867. return(error);
  868. }
  869. /*
  870. * Commit the transaction that added the attr name so that
  871. * later routines can manage their own transactions.
  872. */
  873. error = xfs_trans_roll(&args->trans, dp);
  874. if (error)
  875. return (error);
  876. /*
  877. * If there was an out-of-line value, allocate the blocks we
  878. * identified for its storage and copy the value. This is done
  879. * after we create the attribute so that we don't overflow the
  880. * maximum size of a transaction and/or hit a deadlock.
  881. */
  882. if (args->rmtblkno > 0) {
  883. error = xfs_attr_rmtval_set(args);
  884. if (error)
  885. return(error);
  886. }
  887. /*
  888. * If this is an atomic rename operation, we must "flip" the
  889. * incomplete flags on the "new" and "old" attribute/value pairs
  890. * so that one disappears and one appears atomically. Then we
  891. * must remove the "old" attribute/value pair.
  892. */
  893. if (args->op_flags & XFS_DA_OP_RENAME) {
  894. /*
  895. * In a separate transaction, set the incomplete flag on the
  896. * "old" attr and clear the incomplete flag on the "new" attr.
  897. */
  898. error = xfs_attr_leaf_flipflags(args);
  899. if (error)
  900. return(error);
  901. /*
  902. * Dismantle the "old" attribute/value pair by removing
  903. * a "remote" value (if it exists).
  904. */
  905. args->index = args->index2;
  906. args->blkno = args->blkno2;
  907. args->rmtblkno = args->rmtblkno2;
  908. args->rmtblkcnt = args->rmtblkcnt2;
  909. if (args->rmtblkno) {
  910. error = xfs_attr_rmtval_remove(args);
  911. if (error)
  912. return(error);
  913. }
  914. /*
  915. * Read in the block containing the "old" attr, then
  916. * remove the "old" attr from that block (neat, huh!)
  917. */
  918. error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1,
  919. &bp, XFS_ATTR_FORK);
  920. if (error)
  921. return(error);
  922. ASSERT(bp != NULL);
  923. (void)xfs_attr_leaf_remove(bp, args);
  924. /*
  925. * If the result is small enough, shrink it all into the inode.
  926. */
  927. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  928. xfs_bmap_init(args->flist, args->firstblock);
  929. error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
  930. /* bp is gone due to xfs_da_shrink_inode */
  931. if (!error) {
  932. error = xfs_bmap_finish(&args->trans,
  933. args->flist,
  934. &committed);
  935. }
  936. if (error) {
  937. ASSERT(committed);
  938. args->trans = NULL;
  939. xfs_bmap_cancel(args->flist);
  940. return(error);
  941. }
  942. /*
  943. * bmap_finish() may have committed the last trans
  944. * and started a new one. We need the inode to be
  945. * in all transactions.
  946. */
  947. if (committed) {
  948. xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
  949. xfs_trans_ihold(args->trans, dp);
  950. }
  951. } else
  952. xfs_da_buf_done(bp);
  953. /*
  954. * Commit the remove and start the next trans in series.
  955. */
  956. error = xfs_trans_roll(&args->trans, dp);
  957. } else if (args->rmtblkno > 0) {
  958. /*
  959. * Added a "remote" value, just clear the incomplete flag.
  960. */
  961. error = xfs_attr_leaf_clearflag(args);
  962. }
  963. return(error);
  964. }
  965. /*
  966. * Remove a name from the leaf attribute list structure
  967. *
  968. * This leaf block cannot have a "remote" value, we only call this routine
  969. * if bmap_one_block() says there is only one block (ie: no remote blks).
  970. */
  971. STATIC int
  972. xfs_attr_leaf_removename(xfs_da_args_t *args)
  973. {
  974. xfs_inode_t *dp;
  975. xfs_dabuf_t *bp;
  976. int error, committed, forkoff;
  977. /*
  978. * Remove the attribute.
  979. */
  980. dp = args->dp;
  981. args->blkno = 0;
  982. error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
  983. XFS_ATTR_FORK);
  984. if (error) {
  985. return(error);
  986. }
  987. ASSERT(bp != NULL);
  988. error = xfs_attr_leaf_lookup_int(bp, args);
  989. if (error == ENOATTR) {
  990. xfs_da_brelse(args->trans, bp);
  991. return(error);
  992. }
  993. (void)xfs_attr_leaf_remove(bp, args);
  994. /*
  995. * If the result is small enough, shrink it all into the inode.
  996. */
  997. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  998. xfs_bmap_init(args->flist, args->firstblock);
  999. error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
  1000. /* bp is gone due to xfs_da_shrink_inode */
  1001. if (!error) {
  1002. error = xfs_bmap_finish(&args->trans, args->flist,
  1003. &committed);
  1004. }
  1005. if (error) {
  1006. ASSERT(committed);
  1007. args->trans = NULL;
  1008. xfs_bmap_cancel(args->flist);
  1009. return(error);
  1010. }
  1011. /*
  1012. * bmap_finish() may have committed the last trans and started
  1013. * a new one. We need the inode to be in all transactions.
  1014. */
  1015. if (committed) {
  1016. xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
  1017. xfs_trans_ihold(args->trans, dp);
  1018. }
  1019. } else
  1020. xfs_da_buf_done(bp);
  1021. return(0);
  1022. }
  1023. /*
  1024. * Look up a name in a leaf attribute list structure.
  1025. *
  1026. * This leaf block cannot have a "remote" value, we only call this routine
  1027. * if bmap_one_block() says there is only one block (ie: no remote blks).
  1028. */
  1029. STATIC int
  1030. xfs_attr_leaf_get(xfs_da_args_t *args)
  1031. {
  1032. xfs_dabuf_t *bp;
  1033. int error;
  1034. args->blkno = 0;
  1035. error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
  1036. XFS_ATTR_FORK);
  1037. if (error)
  1038. return(error);
  1039. ASSERT(bp != NULL);
  1040. error = xfs_attr_leaf_lookup_int(bp, args);
  1041. if (error != EEXIST) {
  1042. xfs_da_brelse(args->trans, bp);
  1043. return(error);
  1044. }
  1045. error = xfs_attr_leaf_getvalue(bp, args);
  1046. xfs_da_brelse(args->trans, bp);
  1047. if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
  1048. error = xfs_attr_rmtval_get(args);
  1049. }
  1050. return(error);
  1051. }
  1052. /*
  1053. * Copy out attribute entries for attr_list(), for leaf attribute lists.
  1054. */
  1055. STATIC int
  1056. xfs_attr_leaf_list(xfs_attr_list_context_t *context)
  1057. {
  1058. xfs_attr_leafblock_t *leaf;
  1059. int error;
  1060. xfs_dabuf_t *bp;
  1061. context->cursor->blkno = 0;
  1062. error = xfs_da_read_buf(NULL, context->dp, 0, -1, &bp, XFS_ATTR_FORK);
  1063. if (error)
  1064. return XFS_ERROR(error);
  1065. ASSERT(bp != NULL);
  1066. leaf = bp->data;
  1067. if (unlikely(be16_to_cpu(leaf->hdr.info.magic) != XFS_ATTR_LEAF_MAGIC)) {
  1068. XFS_CORRUPTION_ERROR("xfs_attr_leaf_list", XFS_ERRLEVEL_LOW,
  1069. context->dp->i_mount, leaf);
  1070. xfs_da_brelse(NULL, bp);
  1071. return XFS_ERROR(EFSCORRUPTED);
  1072. }
  1073. error = xfs_attr_leaf_list_int(bp, context);
  1074. xfs_da_brelse(NULL, bp);
  1075. return XFS_ERROR(error);
  1076. }
  1077. /*========================================================================
  1078. * External routines when attribute list size > XFS_LBSIZE(mp).
  1079. *========================================================================*/
  1080. /*
  1081. * Add a name to a Btree-format attribute list.
  1082. *
  1083. * This will involve walking down the Btree, and may involve splitting
  1084. * leaf nodes and even splitting intermediate nodes up to and including
  1085. * the root node (a special case of an intermediate node).
  1086. *
  1087. * "Remote" attribute values confuse the issue and atomic rename operations
  1088. * add a whole extra layer of confusion on top of that.
  1089. */
  1090. STATIC int
  1091. xfs_attr_node_addname(xfs_da_args_t *args)
  1092. {
  1093. xfs_da_state_t *state;
  1094. xfs_da_state_blk_t *blk;
  1095. xfs_inode_t *dp;
  1096. xfs_mount_t *mp;
  1097. int committed, retval, error;
  1098. /*
  1099. * Fill in bucket of arguments/results/context to carry around.
  1100. */
  1101. dp = args->dp;
  1102. mp = dp->i_mount;
  1103. restart:
  1104. state = xfs_da_state_alloc();
  1105. state->args = args;
  1106. state->mp = mp;
  1107. state->blocksize = state->mp->m_sb.sb_blocksize;
  1108. state->node_ents = state->mp->m_attr_node_ents;
  1109. /*
  1110. * Search to see if name already exists, and get back a pointer
  1111. * to where it should go.
  1112. */
  1113. error = xfs_da_node_lookup_int(state, &retval);
  1114. if (error)
  1115. goto out;
  1116. blk = &state->path.blk[ state->path.active-1 ];
  1117. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1118. if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
  1119. goto out;
  1120. } else if (retval == EEXIST) {
  1121. if (args->flags & ATTR_CREATE)
  1122. goto out;
  1123. args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
  1124. args->blkno2 = args->blkno; /* set 2nd entry info*/
  1125. args->index2 = args->index;
  1126. args->rmtblkno2 = args->rmtblkno;
  1127. args->rmtblkcnt2 = args->rmtblkcnt;
  1128. args->rmtblkno = 0;
  1129. args->rmtblkcnt = 0;
  1130. }
  1131. retval = xfs_attr_leaf_add(blk->bp, state->args);
  1132. if (retval == ENOSPC) {
  1133. if (state->path.active == 1) {
  1134. /*
  1135. * Its really a single leaf node, but it had
  1136. * out-of-line values so it looked like it *might*
  1137. * have been a b-tree.
  1138. */
  1139. xfs_da_state_free(state);
  1140. xfs_bmap_init(args->flist, args->firstblock);
  1141. error = xfs_attr_leaf_to_node(args);
  1142. if (!error) {
  1143. error = xfs_bmap_finish(&args->trans,
  1144. args->flist,
  1145. &committed);
  1146. }
  1147. if (error) {
  1148. ASSERT(committed);
  1149. args->trans = NULL;
  1150. xfs_bmap_cancel(args->flist);
  1151. goto out;
  1152. }
  1153. /*
  1154. * bmap_finish() may have committed the last trans
  1155. * and started a new one. We need the inode to be
  1156. * in all transactions.
  1157. */
  1158. if (committed) {
  1159. xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
  1160. xfs_trans_ihold(args->trans, dp);
  1161. }
  1162. /*
  1163. * Commit the node conversion and start the next
  1164. * trans in the chain.
  1165. */
  1166. error = xfs_trans_roll(&args->trans, dp);
  1167. if (error)
  1168. goto out;
  1169. goto restart;
  1170. }
  1171. /*
  1172. * Split as many Btree elements as required.
  1173. * This code tracks the new and old attr's location
  1174. * in the index/blkno/rmtblkno/rmtblkcnt fields and
  1175. * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
  1176. */
  1177. xfs_bmap_init(args->flist, args->firstblock);
  1178. error = xfs_da_split(state);
  1179. if (!error) {
  1180. error = xfs_bmap_finish(&args->trans, args->flist,
  1181. &committed);
  1182. }
  1183. if (error) {
  1184. ASSERT(committed);
  1185. args->trans = NULL;
  1186. xfs_bmap_cancel(args->flist);
  1187. goto out;
  1188. }
  1189. /*
  1190. * bmap_finish() may have committed the last trans and started
  1191. * a new one. We need the inode to be in all transactions.
  1192. */
  1193. if (committed) {
  1194. xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
  1195. xfs_trans_ihold(args->trans, dp);
  1196. }
  1197. } else {
  1198. /*
  1199. * Addition succeeded, update Btree hashvals.
  1200. */
  1201. xfs_da_fixhashpath(state, &state->path);
  1202. }
  1203. /*
  1204. * Kill the state structure, we're done with it and need to
  1205. * allow the buffers to come back later.
  1206. */
  1207. xfs_da_state_free(state);
  1208. state = NULL;
  1209. /*
  1210. * Commit the leaf addition or btree split and start the next
  1211. * trans in the chain.
  1212. */
  1213. error = xfs_trans_roll(&args->trans, dp);
  1214. if (error)
  1215. goto out;
  1216. /*
  1217. * If there was an out-of-line value, allocate the blocks we
  1218. * identified for its storage and copy the value. This is done
  1219. * after we create the attribute so that we don't overflow the
  1220. * maximum size of a transaction and/or hit a deadlock.
  1221. */
  1222. if (args->rmtblkno > 0) {
  1223. error = xfs_attr_rmtval_set(args);
  1224. if (error)
  1225. return(error);
  1226. }
  1227. /*
  1228. * If this is an atomic rename operation, we must "flip" the
  1229. * incomplete flags on the "new" and "old" attribute/value pairs
  1230. * so that one disappears and one appears atomically. Then we
  1231. * must remove the "old" attribute/value pair.
  1232. */
  1233. if (args->op_flags & XFS_DA_OP_RENAME) {
  1234. /*
  1235. * In a separate transaction, set the incomplete flag on the
  1236. * "old" attr and clear the incomplete flag on the "new" attr.
  1237. */
  1238. error = xfs_attr_leaf_flipflags(args);
  1239. if (error)
  1240. goto out;
  1241. /*
  1242. * Dismantle the "old" attribute/value pair by removing
  1243. * a "remote" value (if it exists).
  1244. */
  1245. args->index = args->index2;
  1246. args->blkno = args->blkno2;
  1247. args->rmtblkno = args->rmtblkno2;
  1248. args->rmtblkcnt = args->rmtblkcnt2;
  1249. if (args->rmtblkno) {
  1250. error = xfs_attr_rmtval_remove(args);
  1251. if (error)
  1252. return(error);
  1253. }
  1254. /*
  1255. * Re-find the "old" attribute entry after any split ops.
  1256. * The INCOMPLETE flag means that we will find the "old"
  1257. * attr, not the "new" one.
  1258. */
  1259. args->flags |= XFS_ATTR_INCOMPLETE;
  1260. state = xfs_da_state_alloc();
  1261. state->args = args;
  1262. state->mp = mp;
  1263. state->blocksize = state->mp->m_sb.sb_blocksize;
  1264. state->node_ents = state->mp->m_attr_node_ents;
  1265. state->inleaf = 0;
  1266. error = xfs_da_node_lookup_int(state, &retval);
  1267. if (error)
  1268. goto out;
  1269. /*
  1270. * Remove the name and update the hashvals in the tree.
  1271. */
  1272. blk = &state->path.blk[ state->path.active-1 ];
  1273. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1274. error = xfs_attr_leaf_remove(blk->bp, args);
  1275. xfs_da_fixhashpath(state, &state->path);
  1276. /*
  1277. * Check to see if the tree needs to be collapsed.
  1278. */
  1279. if (retval && (state->path.active > 1)) {
  1280. xfs_bmap_init(args->flist, args->firstblock);
  1281. error = xfs_da_join(state);
  1282. if (!error) {
  1283. error = xfs_bmap_finish(&args->trans,
  1284. args->flist,
  1285. &committed);
  1286. }
  1287. if (error) {
  1288. ASSERT(committed);
  1289. args->trans = NULL;
  1290. xfs_bmap_cancel(args->flist);
  1291. goto out;
  1292. }
  1293. /*
  1294. * bmap_finish() may have committed the last trans
  1295. * and started a new one. We need the inode to be
  1296. * in all transactions.
  1297. */
  1298. if (committed) {
  1299. xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
  1300. xfs_trans_ihold(args->trans, dp);
  1301. }
  1302. }
  1303. /*
  1304. * Commit and start the next trans in the chain.
  1305. */
  1306. error = xfs_trans_roll(&args->trans, dp);
  1307. if (error)
  1308. goto out;
  1309. } else if (args->rmtblkno > 0) {
  1310. /*
  1311. * Added a "remote" value, just clear the incomplete flag.
  1312. */
  1313. error = xfs_attr_leaf_clearflag(args);
  1314. if (error)
  1315. goto out;
  1316. }
  1317. retval = error = 0;
  1318. out:
  1319. if (state)
  1320. xfs_da_state_free(state);
  1321. if (error)
  1322. return(error);
  1323. return(retval);
  1324. }
  1325. /*
  1326. * Remove a name from a B-tree attribute list.
  1327. *
  1328. * This will involve walking down the Btree, and may involve joining
  1329. * leaf nodes and even joining intermediate nodes up to and including
  1330. * the root node (a special case of an intermediate node).
  1331. */
  1332. STATIC int
  1333. xfs_attr_node_removename(xfs_da_args_t *args)
  1334. {
  1335. xfs_da_state_t *state;
  1336. xfs_da_state_blk_t *blk;
  1337. xfs_inode_t *dp;
  1338. xfs_dabuf_t *bp;
  1339. int retval, error, committed, forkoff;
  1340. /*
  1341. * Tie a string around our finger to remind us where we are.
  1342. */
  1343. dp = args->dp;
  1344. state = xfs_da_state_alloc();
  1345. state->args = args;
  1346. state->mp = dp->i_mount;
  1347. state->blocksize = state->mp->m_sb.sb_blocksize;
  1348. state->node_ents = state->mp->m_attr_node_ents;
  1349. /*
  1350. * Search to see if name exists, and get back a pointer to it.
  1351. */
  1352. error = xfs_da_node_lookup_int(state, &retval);
  1353. if (error || (retval != EEXIST)) {
  1354. if (error == 0)
  1355. error = retval;
  1356. goto out;
  1357. }
  1358. /*
  1359. * If there is an out-of-line value, de-allocate the blocks.
  1360. * This is done before we remove the attribute so that we don't
  1361. * overflow the maximum size of a transaction and/or hit a deadlock.
  1362. */
  1363. blk = &state->path.blk[ state->path.active-1 ];
  1364. ASSERT(blk->bp != NULL);
  1365. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1366. if (args->rmtblkno > 0) {
  1367. /*
  1368. * Fill in disk block numbers in the state structure
  1369. * so that we can get the buffers back after we commit
  1370. * several transactions in the following calls.
  1371. */
  1372. error = xfs_attr_fillstate(state);
  1373. if (error)
  1374. goto out;
  1375. /*
  1376. * Mark the attribute as INCOMPLETE, then bunmapi() the
  1377. * remote value.
  1378. */
  1379. error = xfs_attr_leaf_setflag(args);
  1380. if (error)
  1381. goto out;
  1382. error = xfs_attr_rmtval_remove(args);
  1383. if (error)
  1384. goto out;
  1385. /*
  1386. * Refill the state structure with buffers, the prior calls
  1387. * released our buffers.
  1388. */
  1389. error = xfs_attr_refillstate(state);
  1390. if (error)
  1391. goto out;
  1392. }
  1393. /*
  1394. * Remove the name and update the hashvals in the tree.
  1395. */
  1396. blk = &state->path.blk[ state->path.active-1 ];
  1397. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1398. retval = xfs_attr_leaf_remove(blk->bp, args);
  1399. xfs_da_fixhashpath(state, &state->path);
  1400. /*
  1401. * Check to see if the tree needs to be collapsed.
  1402. */
  1403. if (retval && (state->path.active > 1)) {
  1404. xfs_bmap_init(args->flist, args->firstblock);
  1405. error = xfs_da_join(state);
  1406. if (!error) {
  1407. error = xfs_bmap_finish(&args->trans, args->flist,
  1408. &committed);
  1409. }
  1410. if (error) {
  1411. ASSERT(committed);
  1412. args->trans = NULL;
  1413. xfs_bmap_cancel(args->flist);
  1414. goto out;
  1415. }
  1416. /*
  1417. * bmap_finish() may have committed the last trans and started
  1418. * a new one. We need the inode to be in all transactions.
  1419. */
  1420. if (committed) {
  1421. xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
  1422. xfs_trans_ihold(args->trans, dp);
  1423. }
  1424. /*
  1425. * Commit the Btree join operation and start a new trans.
  1426. */
  1427. error = xfs_trans_roll(&args->trans, dp);
  1428. if (error)
  1429. goto out;
  1430. }
  1431. /*
  1432. * If the result is small enough, push it all into the inode.
  1433. */
  1434. if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  1435. /*
  1436. * Have to get rid of the copy of this dabuf in the state.
  1437. */
  1438. ASSERT(state->path.active == 1);
  1439. ASSERT(state->path.blk[0].bp);
  1440. xfs_da_buf_done(state->path.blk[0].bp);
  1441. state->path.blk[0].bp = NULL;
  1442. error = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp,
  1443. XFS_ATTR_FORK);
  1444. if (error)
  1445. goto out;
  1446. ASSERT(be16_to_cpu(((xfs_attr_leafblock_t *)
  1447. bp->data)->hdr.info.magic)
  1448. == XFS_ATTR_LEAF_MAGIC);
  1449. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  1450. xfs_bmap_init(args->flist, args->firstblock);
  1451. error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
  1452. /* bp is gone due to xfs_da_shrink_inode */
  1453. if (!error) {
  1454. error = xfs_bmap_finish(&args->trans,
  1455. args->flist,
  1456. &committed);
  1457. }
  1458. if (error) {
  1459. ASSERT(committed);
  1460. args->trans = NULL;
  1461. xfs_bmap_cancel(args->flist);
  1462. goto out;
  1463. }
  1464. /*
  1465. * bmap_finish() may have committed the last trans
  1466. * and started a new one. We need the inode to be
  1467. * in all transactions.
  1468. */
  1469. if (committed) {
  1470. xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
  1471. xfs_trans_ihold(args->trans, dp);
  1472. }
  1473. } else
  1474. xfs_da_brelse(args->trans, bp);
  1475. }
  1476. error = 0;
  1477. out:
  1478. xfs_da_state_free(state);
  1479. return(error);
  1480. }
  1481. /*
  1482. * Fill in the disk block numbers in the state structure for the buffers
  1483. * that are attached to the state structure.
  1484. * This is done so that we can quickly reattach ourselves to those buffers
  1485. * after some set of transaction commits have released these buffers.
  1486. */
  1487. STATIC int
  1488. xfs_attr_fillstate(xfs_da_state_t *state)
  1489. {
  1490. xfs_da_state_path_t *path;
  1491. xfs_da_state_blk_t *blk;
  1492. int level;
  1493. /*
  1494. * Roll down the "path" in the state structure, storing the on-disk
  1495. * block number for those buffers in the "path".
  1496. */
  1497. path = &state->path;
  1498. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1499. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1500. if (blk->bp) {
  1501. blk->disk_blkno = xfs_da_blkno(blk->bp);
  1502. xfs_da_buf_done(blk->bp);
  1503. blk->bp = NULL;
  1504. } else {
  1505. blk->disk_blkno = 0;
  1506. }
  1507. }
  1508. /*
  1509. * Roll down the "altpath" in the state structure, storing the on-disk
  1510. * block number for those buffers in the "altpath".
  1511. */
  1512. path = &state->altpath;
  1513. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1514. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1515. if (blk->bp) {
  1516. blk->disk_blkno = xfs_da_blkno(blk->bp);
  1517. xfs_da_buf_done(blk->bp);
  1518. blk->bp = NULL;
  1519. } else {
  1520. blk->disk_blkno = 0;
  1521. }
  1522. }
  1523. return(0);
  1524. }
  1525. /*
  1526. * Reattach the buffers to the state structure based on the disk block
  1527. * numbers stored in the state structure.
  1528. * This is done after some set of transaction commits have released those
  1529. * buffers from our grip.
  1530. */
  1531. STATIC int
  1532. xfs_attr_refillstate(xfs_da_state_t *state)
  1533. {
  1534. xfs_da_state_path_t *path;
  1535. xfs_da_state_blk_t *blk;
  1536. int level, error;
  1537. /*
  1538. * Roll down the "path" in the state structure, storing the on-disk
  1539. * block number for those buffers in the "path".
  1540. */
  1541. path = &state->path;
  1542. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1543. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1544. if (blk->disk_blkno) {
  1545. error = xfs_da_read_buf(state->args->trans,
  1546. state->args->dp,
  1547. blk->blkno, blk->disk_blkno,
  1548. &blk->bp, XFS_ATTR_FORK);
  1549. if (error)
  1550. return(error);
  1551. } else {
  1552. blk->bp = NULL;
  1553. }
  1554. }
  1555. /*
  1556. * Roll down the "altpath" in the state structure, storing the on-disk
  1557. * block number for those buffers in the "altpath".
  1558. */
  1559. path = &state->altpath;
  1560. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1561. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1562. if (blk->disk_blkno) {
  1563. error = xfs_da_read_buf(state->args->trans,
  1564. state->args->dp,
  1565. blk->blkno, blk->disk_blkno,
  1566. &blk->bp, XFS_ATTR_FORK);
  1567. if (error)
  1568. return(error);
  1569. } else {
  1570. blk->bp = NULL;
  1571. }
  1572. }
  1573. return(0);
  1574. }
  1575. /*
  1576. * Look up a filename in a node attribute list.
  1577. *
  1578. * This routine gets called for any attribute fork that has more than one
  1579. * block, ie: both true Btree attr lists and for single-leaf-blocks with
  1580. * "remote" values taking up more blocks.
  1581. */
  1582. STATIC int
  1583. xfs_attr_node_get(xfs_da_args_t *args)
  1584. {
  1585. xfs_da_state_t *state;
  1586. xfs_da_state_blk_t *blk;
  1587. int error, retval;
  1588. int i;
  1589. state = xfs_da_state_alloc();
  1590. state->args = args;
  1591. state->mp = args->dp->i_mount;
  1592. state->blocksize = state->mp->m_sb.sb_blocksize;
  1593. state->node_ents = state->mp->m_attr_node_ents;
  1594. /*
  1595. * Search to see if name exists, and get back a pointer to it.
  1596. */
  1597. error = xfs_da_node_lookup_int(state, &retval);
  1598. if (error) {
  1599. retval = error;
  1600. } else if (retval == EEXIST) {
  1601. blk = &state->path.blk[ state->path.active-1 ];
  1602. ASSERT(blk->bp != NULL);
  1603. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1604. /*
  1605. * Get the value, local or "remote"
  1606. */
  1607. retval = xfs_attr_leaf_getvalue(blk->bp, args);
  1608. if (!retval && (args->rmtblkno > 0)
  1609. && !(args->flags & ATTR_KERNOVAL)) {
  1610. retval = xfs_attr_rmtval_get(args);
  1611. }
  1612. }
  1613. /*
  1614. * If not in a transaction, we have to release all the buffers.
  1615. */
  1616. for (i = 0; i < state->path.active; i++) {
  1617. xfs_da_brelse(args->trans, state->path.blk[i].bp);
  1618. state->path.blk[i].bp = NULL;
  1619. }
  1620. xfs_da_state_free(state);
  1621. return(retval);
  1622. }
  1623. STATIC int /* error */
  1624. xfs_attr_node_list(xfs_attr_list_context_t *context)
  1625. {
  1626. attrlist_cursor_kern_t *cursor;
  1627. xfs_attr_leafblock_t *leaf;
  1628. xfs_da_intnode_t *node;
  1629. xfs_da_node_entry_t *btree;
  1630. int error, i;
  1631. xfs_dabuf_t *bp;
  1632. cursor = context->cursor;
  1633. cursor->initted = 1;
  1634. /*
  1635. * Do all sorts of validation on the passed-in cursor structure.
  1636. * If anything is amiss, ignore the cursor and look up the hashval
  1637. * starting from the btree root.
  1638. */
  1639. bp = NULL;
  1640. if (cursor->blkno > 0) {
  1641. error = xfs_da_read_buf(NULL, context->dp, cursor->blkno, -1,
  1642. &bp, XFS_ATTR_FORK);
  1643. if ((error != 0) && (error != EFSCORRUPTED))
  1644. return(error);
  1645. if (bp) {
  1646. node = bp->data;
  1647. switch (be16_to_cpu(node->hdr.info.magic)) {
  1648. case XFS_DA_NODE_MAGIC:
  1649. trace_xfs_attr_list_wrong_blk(context);
  1650. xfs_da_brelse(NULL, bp);
  1651. bp = NULL;
  1652. break;
  1653. case XFS_ATTR_LEAF_MAGIC:
  1654. leaf = bp->data;
  1655. if (cursor->hashval > be32_to_cpu(leaf->entries[
  1656. be16_to_cpu(leaf->hdr.count)-1].hashval)) {
  1657. trace_xfs_attr_list_wrong_blk(context);
  1658. xfs_da_brelse(NULL, bp);
  1659. bp = NULL;
  1660. } else if (cursor->hashval <=
  1661. be32_to_cpu(leaf->entries[0].hashval)) {
  1662. trace_xfs_attr_list_wrong_blk(context);
  1663. xfs_da_brelse(NULL, bp);
  1664. bp = NULL;
  1665. }
  1666. break;
  1667. default:
  1668. trace_xfs_attr_list_wrong_blk(context);
  1669. xfs_da_brelse(NULL, bp);
  1670. bp = NULL;
  1671. }
  1672. }
  1673. }
  1674. /*
  1675. * We did not find what we expected given the cursor's contents,
  1676. * so we start from the top and work down based on the hash value.
  1677. * Note that start of node block is same as start of leaf block.
  1678. */
  1679. if (bp == NULL) {
  1680. cursor->blkno = 0;
  1681. for (;;) {
  1682. error = xfs_da_read_buf(NULL, context->dp,
  1683. cursor->blkno, -1, &bp,
  1684. XFS_ATTR_FORK);
  1685. if (error)
  1686. return(error);
  1687. if (unlikely(bp == NULL)) {
  1688. XFS_ERROR_REPORT("xfs_attr_node_list(2)",
  1689. XFS_ERRLEVEL_LOW,
  1690. context->dp->i_mount);
  1691. return(XFS_ERROR(EFSCORRUPTED));
  1692. }
  1693. node = bp->data;
  1694. if (be16_to_cpu(node->hdr.info.magic)
  1695. == XFS_ATTR_LEAF_MAGIC)
  1696. break;
  1697. if (unlikely(be16_to_cpu(node->hdr.info.magic)
  1698. != XFS_DA_NODE_MAGIC)) {
  1699. XFS_CORRUPTION_ERROR("xfs_attr_node_list(3)",
  1700. XFS_ERRLEVEL_LOW,
  1701. context->dp->i_mount,
  1702. node);
  1703. xfs_da_brelse(NULL, bp);
  1704. return(XFS_ERROR(EFSCORRUPTED));
  1705. }
  1706. btree = node->btree;
  1707. for (i = 0; i < be16_to_cpu(node->hdr.count);
  1708. btree++, i++) {
  1709. if (cursor->hashval
  1710. <= be32_to_cpu(btree->hashval)) {
  1711. cursor->blkno = be32_to_cpu(btree->before);
  1712. trace_xfs_attr_list_node_descend(context,
  1713. btree);
  1714. break;
  1715. }
  1716. }
  1717. if (i == be16_to_cpu(node->hdr.count)) {
  1718. xfs_da_brelse(NULL, bp);
  1719. return(0);
  1720. }
  1721. xfs_da_brelse(NULL, bp);
  1722. }
  1723. }
  1724. ASSERT(bp != NULL);
  1725. /*
  1726. * Roll upward through the blocks, processing each leaf block in
  1727. * order. As long as there is space in the result buffer, keep
  1728. * adding the information.
  1729. */
  1730. for (;;) {
  1731. leaf = bp->data;
  1732. if (unlikely(be16_to_cpu(leaf->hdr.info.magic)
  1733. != XFS_ATTR_LEAF_MAGIC)) {
  1734. XFS_CORRUPTION_ERROR("xfs_attr_node_list(4)",
  1735. XFS_ERRLEVEL_LOW,
  1736. context->dp->i_mount, leaf);
  1737. xfs_da_brelse(NULL, bp);
  1738. return(XFS_ERROR(EFSCORRUPTED));
  1739. }
  1740. error = xfs_attr_leaf_list_int(bp, context);
  1741. if (error) {
  1742. xfs_da_brelse(NULL, bp);
  1743. return error;
  1744. }
  1745. if (context->seen_enough || leaf->hdr.info.forw == 0)
  1746. break;
  1747. cursor->blkno = be32_to_cpu(leaf->hdr.info.forw);

Large files files are truncated, but you can click here to view the full file