xfs_attr.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537
  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_format.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_trans.h"
  24. #include "xfs_trans_priv.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_mount.h"
  28. #include "xfs_da_btree.h"
  29. #include "xfs_bmap_btree.h"
  30. #include "xfs_attr_sf.h"
  31. #include "xfs_dinode.h"
  32. #include "xfs_inode.h"
  33. #include "xfs_alloc.h"
  34. #include "xfs_inode_item.h"
  35. #include "xfs_bmap.h"
  36. #include "xfs_bmap_util.h"
  37. #include "xfs_attr.h"
  38. #include "xfs_attr_leaf.h"
  39. #include "xfs_attr_remote.h"
  40. #include "xfs_error.h"
  41. #include "xfs_quota.h"
  42. #include "xfs_trans_space.h"
  43. #include "xfs_trace.h"
  44. /*
  45. * xfs_attr.c
  46. *
  47. * Provide the external interfaces to manage attribute lists.
  48. */
  49. /*========================================================================
  50. * Function prototypes for the kernel.
  51. *========================================================================*/
  52. /*
  53. * Internal routines when attribute list fits inside the inode.
  54. */
  55. STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
  56. /*
  57. * Internal routines when attribute list is one block.
  58. */
  59. STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
  60. STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
  61. STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
  62. /*
  63. * Internal routines when attribute list is more than one block.
  64. */
  65. STATIC int xfs_attr_node_get(xfs_da_args_t *args);
  66. STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
  67. STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
  68. STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
  69. STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
  70. STATIC int
  71. xfs_attr_name_to_xname(
  72. struct xfs_name *xname,
  73. const unsigned char *aname)
  74. {
  75. if (!aname)
  76. return EINVAL;
  77. xname->name = aname;
  78. xname->len = strlen((char *)aname);
  79. if (xname->len >= MAXNAMELEN)
  80. return EFAULT; /* match IRIX behaviour */
  81. return 0;
  82. }
  83. int
  84. xfs_inode_hasattr(
  85. struct xfs_inode *ip)
  86. {
  87. if (!XFS_IFORK_Q(ip) ||
  88. (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
  89. ip->i_d.di_anextents == 0))
  90. return 0;
  91. return 1;
  92. }
  93. /*========================================================================
  94. * Overall external interface routines.
  95. *========================================================================*/
  96. STATIC int
  97. xfs_attr_get_int(
  98. struct xfs_inode *ip,
  99. struct xfs_name *name,
  100. unsigned char *value,
  101. int *valuelenp,
  102. int flags)
  103. {
  104. xfs_da_args_t args;
  105. int error;
  106. if (!xfs_inode_hasattr(ip))
  107. return ENOATTR;
  108. /*
  109. * Fill in the arg structure for this request.
  110. */
  111. memset((char *)&args, 0, sizeof(args));
  112. args.name = name->name;
  113. args.namelen = name->len;
  114. args.value = value;
  115. args.valuelen = *valuelenp;
  116. args.flags = flags;
  117. args.hashval = xfs_da_hashname(args.name, args.namelen);
  118. args.dp = ip;
  119. args.whichfork = XFS_ATTR_FORK;
  120. /*
  121. * Decide on what work routines to call based on the inode size.
  122. */
  123. if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
  124. error = xfs_attr_shortform_getvalue(&args);
  125. } else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK)) {
  126. error = xfs_attr_leaf_get(&args);
  127. } else {
  128. error = xfs_attr_node_get(&args);
  129. }
  130. /*
  131. * Return the number of bytes in the value to the caller.
  132. */
  133. *valuelenp = args.valuelen;
  134. if (error == EEXIST)
  135. error = 0;
  136. return(error);
  137. }
  138. int
  139. xfs_attr_get(
  140. xfs_inode_t *ip,
  141. const unsigned char *name,
  142. unsigned char *value,
  143. int *valuelenp,
  144. int flags)
  145. {
  146. int error;
  147. struct xfs_name xname;
  148. XFS_STATS_INC(xs_attr_get);
  149. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  150. return(EIO);
  151. error = xfs_attr_name_to_xname(&xname, name);
  152. if (error)
  153. return error;
  154. xfs_ilock(ip, XFS_ILOCK_SHARED);
  155. error = xfs_attr_get_int(ip, &xname, value, valuelenp, flags);
  156. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  157. return(error);
  158. }
  159. /*
  160. * Calculate how many blocks we need for the new attribute,
  161. */
  162. STATIC int
  163. xfs_attr_calc_size(
  164. struct xfs_inode *ip,
  165. int namelen,
  166. int valuelen,
  167. int *local)
  168. {
  169. struct xfs_mount *mp = ip->i_mount;
  170. int size;
  171. int nblks;
  172. /*
  173. * Determine space new attribute will use, and if it would be
  174. * "local" or "remote" (note: local != inline).
  175. */
  176. size = xfs_attr_leaf_newentsize(namelen, valuelen,
  177. mp->m_sb.sb_blocksize, local);
  178. nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
  179. if (*local) {
  180. if (size > (mp->m_sb.sb_blocksize >> 1)) {
  181. /* Double split possible */
  182. nblks *= 2;
  183. }
  184. } else {
  185. /*
  186. * Out of line attribute, cannot double split, but
  187. * make room for the attribute value itself.
  188. */
  189. uint dblocks = XFS_B_TO_FSB(mp, valuelen);
  190. nblks += dblocks;
  191. nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
  192. }
  193. return nblks;
  194. }
  195. STATIC int
  196. xfs_attr_set_int(
  197. struct xfs_inode *dp,
  198. struct xfs_name *name,
  199. unsigned char *value,
  200. int valuelen,
  201. int flags)
  202. {
  203. xfs_da_args_t args;
  204. xfs_fsblock_t firstblock;
  205. xfs_bmap_free_t flist;
  206. int error, err2, committed;
  207. struct xfs_mount *mp = dp->i_mount;
  208. struct xfs_trans_res tres;
  209. int rsvd = (flags & ATTR_ROOT) != 0;
  210. int local;
  211. /*
  212. * Attach the dquots to the inode.
  213. */
  214. error = xfs_qm_dqattach(dp, 0);
  215. if (error)
  216. return error;
  217. /*
  218. * If the inode doesn't have an attribute fork, add one.
  219. * (inode must not be locked when we call this routine)
  220. */
  221. if (XFS_IFORK_Q(dp) == 0) {
  222. int sf_size = sizeof(xfs_attr_sf_hdr_t) +
  223. XFS_ATTR_SF_ENTSIZE_BYNAME(name->len, valuelen);
  224. if ((error = xfs_bmap_add_attrfork(dp, sf_size, rsvd)))
  225. return(error);
  226. }
  227. /*
  228. * Fill in the arg structure for this request.
  229. */
  230. memset((char *)&args, 0, sizeof(args));
  231. args.name = name->name;
  232. args.namelen = name->len;
  233. args.value = value;
  234. args.valuelen = valuelen;
  235. args.flags = flags;
  236. args.hashval = xfs_da_hashname(args.name, args.namelen);
  237. args.dp = dp;
  238. args.firstblock = &firstblock;
  239. args.flist = &flist;
  240. args.whichfork = XFS_ATTR_FORK;
  241. args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
  242. /* Size is now blocks for attribute data */
  243. args.total = xfs_attr_calc_size(dp, name->len, valuelen, &local);
  244. /*
  245. * Start our first transaction of the day.
  246. *
  247. * All future transactions during this code must be "chained" off
  248. * this one via the trans_dup() call. All transactions will contain
  249. * the inode, and the inode will always be marked with trans_ihold().
  250. * Since the inode will be locked in all transactions, we must log
  251. * the inode in every transaction to let it float upward through
  252. * the log.
  253. */
  254. args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);
  255. /*
  256. * Root fork attributes can use reserved data blocks for this
  257. * operation if necessary
  258. */
  259. if (rsvd)
  260. args.trans->t_flags |= XFS_TRANS_RESERVE;
  261. tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
  262. M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
  263. tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
  264. tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
  265. error = xfs_trans_reserve(args.trans, &tres, args.total, 0);
  266. if (error) {
  267. xfs_trans_cancel(args.trans, 0);
  268. return(error);
  269. }
  270. xfs_ilock(dp, XFS_ILOCK_EXCL);
  271. error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
  272. rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
  273. XFS_QMOPT_RES_REGBLKS);
  274. if (error) {
  275. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  276. xfs_trans_cancel(args.trans, XFS_TRANS_RELEASE_LOG_RES);
  277. return (error);
  278. }
  279. xfs_trans_ijoin(args.trans, dp, 0);
  280. /*
  281. * If the attribute list is non-existent or a shortform list,
  282. * upgrade it to a single-leaf-block attribute list.
  283. */
  284. if ((dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
  285. ((dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) &&
  286. (dp->i_d.di_anextents == 0))) {
  287. /*
  288. * Build initial attribute list (if required).
  289. */
  290. if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
  291. xfs_attr_shortform_create(&args);
  292. /*
  293. * Try to add the attr to the attribute list in
  294. * the inode.
  295. */
  296. error = xfs_attr_shortform_addname(&args);
  297. if (error != ENOSPC) {
  298. /*
  299. * Commit the shortform mods, and we're done.
  300. * NOTE: this is also the error path (EEXIST, etc).
  301. */
  302. ASSERT(args.trans != NULL);
  303. /*
  304. * If this is a synchronous mount, make sure that
  305. * the transaction goes to disk before returning
  306. * to the user.
  307. */
  308. if (mp->m_flags & XFS_MOUNT_WSYNC) {
  309. xfs_trans_set_sync(args.trans);
  310. }
  311. if (!error && (flags & ATTR_KERNOTIME) == 0) {
  312. xfs_trans_ichgtime(args.trans, dp,
  313. XFS_ICHGTIME_CHG);
  314. }
  315. err2 = xfs_trans_commit(args.trans,
  316. XFS_TRANS_RELEASE_LOG_RES);
  317. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  318. return(error == 0 ? err2 : error);
  319. }
  320. /*
  321. * It won't fit in the shortform, transform to a leaf block.
  322. * GROT: another possible req'mt for a double-split btree op.
  323. */
  324. xfs_bmap_init(args.flist, args.firstblock);
  325. error = xfs_attr_shortform_to_leaf(&args);
  326. if (!error) {
  327. error = xfs_bmap_finish(&args.trans, args.flist,
  328. &committed);
  329. }
  330. if (error) {
  331. ASSERT(committed);
  332. args.trans = NULL;
  333. xfs_bmap_cancel(&flist);
  334. goto out;
  335. }
  336. /*
  337. * bmap_finish() may have committed the last trans and started
  338. * a new one. We need the inode to be in all transactions.
  339. */
  340. if (committed)
  341. xfs_trans_ijoin(args.trans, dp, 0);
  342. /*
  343. * Commit the leaf transformation. We'll need another (linked)
  344. * transaction to add the new attribute to the leaf.
  345. */
  346. error = xfs_trans_roll(&args.trans, dp);
  347. if (error)
  348. goto out;
  349. }
  350. if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  351. error = xfs_attr_leaf_addname(&args);
  352. } else {
  353. error = xfs_attr_node_addname(&args);
  354. }
  355. if (error) {
  356. goto out;
  357. }
  358. /*
  359. * If this is a synchronous mount, make sure that the
  360. * transaction goes to disk before returning to the user.
  361. */
  362. if (mp->m_flags & XFS_MOUNT_WSYNC) {
  363. xfs_trans_set_sync(args.trans);
  364. }
  365. if ((flags & ATTR_KERNOTIME) == 0)
  366. xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
  367. /*
  368. * Commit the last in the sequence of transactions.
  369. */
  370. xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
  371. error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
  372. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  373. return(error);
  374. out:
  375. if (args.trans)
  376. xfs_trans_cancel(args.trans,
  377. XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  378. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  379. return(error);
  380. }
  381. int
  382. xfs_attr_set(
  383. xfs_inode_t *dp,
  384. const unsigned char *name,
  385. unsigned char *value,
  386. int valuelen,
  387. int flags)
  388. {
  389. int error;
  390. struct xfs_name xname;
  391. XFS_STATS_INC(xs_attr_set);
  392. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  393. return (EIO);
  394. error = xfs_attr_name_to_xname(&xname, name);
  395. if (error)
  396. return error;
  397. return xfs_attr_set_int(dp, &xname, value, valuelen, flags);
  398. }
  399. /*
  400. * Generic handler routine to remove a name from an attribute list.
  401. * Transitions attribute list from Btree to shortform as necessary.
  402. */
  403. STATIC int
  404. xfs_attr_remove_int(xfs_inode_t *dp, struct xfs_name *name, int flags)
  405. {
  406. xfs_da_args_t args;
  407. xfs_fsblock_t firstblock;
  408. xfs_bmap_free_t flist;
  409. int error;
  410. xfs_mount_t *mp = dp->i_mount;
  411. /*
  412. * Fill in the arg structure for this request.
  413. */
  414. memset((char *)&args, 0, sizeof(args));
  415. args.name = name->name;
  416. args.namelen = name->len;
  417. args.flags = flags;
  418. args.hashval = xfs_da_hashname(args.name, args.namelen);
  419. args.dp = dp;
  420. args.firstblock = &firstblock;
  421. args.flist = &flist;
  422. args.total = 0;
  423. args.whichfork = XFS_ATTR_FORK;
  424. /*
  425. * we have no control over the attribute names that userspace passes us
  426. * to remove, so we have to allow the name lookup prior to attribute
  427. * removal to fail.
  428. */
  429. args.op_flags = XFS_DA_OP_OKNOENT;
  430. /*
  431. * Attach the dquots to the inode.
  432. */
  433. error = xfs_qm_dqattach(dp, 0);
  434. if (error)
  435. return error;
  436. /*
  437. * Start our first transaction of the day.
  438. *
  439. * All future transactions during this code must be "chained" off
  440. * this one via the trans_dup() call. All transactions will contain
  441. * the inode, and the inode will always be marked with trans_ihold().
  442. * Since the inode will be locked in all transactions, we must log
  443. * the inode in every transaction to let it float upward through
  444. * the log.
  445. */
  446. args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
  447. /*
  448. * Root fork attributes can use reserved data blocks for this
  449. * operation if necessary
  450. */
  451. if (flags & ATTR_ROOT)
  452. args.trans->t_flags |= XFS_TRANS_RESERVE;
  453. error = xfs_trans_reserve(args.trans, &M_RES(mp)->tr_attrrm,
  454. XFS_ATTRRM_SPACE_RES(mp), 0);
  455. if (error) {
  456. xfs_trans_cancel(args.trans, 0);
  457. return(error);
  458. }
  459. xfs_ilock(dp, XFS_ILOCK_EXCL);
  460. /*
  461. * No need to make quota reservations here. We expect to release some
  462. * blocks not allocate in the common case.
  463. */
  464. xfs_trans_ijoin(args.trans, dp, 0);
  465. /*
  466. * Decide on what work routines to call based on the inode size.
  467. */
  468. if (!xfs_inode_hasattr(dp)) {
  469. error = XFS_ERROR(ENOATTR);
  470. goto out;
  471. }
  472. if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
  473. ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
  474. error = xfs_attr_shortform_remove(&args);
  475. if (error) {
  476. goto out;
  477. }
  478. } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  479. error = xfs_attr_leaf_removename(&args);
  480. } else {
  481. error = xfs_attr_node_removename(&args);
  482. }
  483. if (error) {
  484. goto out;
  485. }
  486. /*
  487. * If this is a synchronous mount, make sure that the
  488. * transaction goes to disk before returning to the user.
  489. */
  490. if (mp->m_flags & XFS_MOUNT_WSYNC) {
  491. xfs_trans_set_sync(args.trans);
  492. }
  493. if ((flags & ATTR_KERNOTIME) == 0)
  494. xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
  495. /*
  496. * Commit the last in the sequence of transactions.
  497. */
  498. xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
  499. error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
  500. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  501. return(error);
  502. out:
  503. if (args.trans)
  504. xfs_trans_cancel(args.trans,
  505. XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  506. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  507. return(error);
  508. }
  509. int
  510. xfs_attr_remove(
  511. xfs_inode_t *dp,
  512. const unsigned char *name,
  513. int flags)
  514. {
  515. int error;
  516. struct xfs_name xname;
  517. XFS_STATS_INC(xs_attr_remove);
  518. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  519. return (EIO);
  520. error = xfs_attr_name_to_xname(&xname, name);
  521. if (error)
  522. return error;
  523. xfs_ilock(dp, XFS_ILOCK_SHARED);
  524. if (!xfs_inode_hasattr(dp)) {
  525. xfs_iunlock(dp, XFS_ILOCK_SHARED);
  526. return XFS_ERROR(ENOATTR);
  527. }
  528. xfs_iunlock(dp, XFS_ILOCK_SHARED);
  529. return xfs_attr_remove_int(dp, &xname, flags);
  530. }
  531. /*========================================================================
  532. * External routines when attribute list is inside the inode
  533. *========================================================================*/
  534. /*
  535. * Add a name to the shortform attribute list structure
  536. * This is the external routine.
  537. */
  538. STATIC int
  539. xfs_attr_shortform_addname(xfs_da_args_t *args)
  540. {
  541. int newsize, forkoff, retval;
  542. trace_xfs_attr_sf_addname(args);
  543. retval = xfs_attr_shortform_lookup(args);
  544. if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
  545. return(retval);
  546. } else if (retval == EEXIST) {
  547. if (args->flags & ATTR_CREATE)
  548. return(retval);
  549. retval = xfs_attr_shortform_remove(args);
  550. ASSERT(retval == 0);
  551. }
  552. if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
  553. args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
  554. return(XFS_ERROR(ENOSPC));
  555. newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
  556. newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
  557. forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
  558. if (!forkoff)
  559. return(XFS_ERROR(ENOSPC));
  560. xfs_attr_shortform_add(args, forkoff);
  561. return(0);
  562. }
  563. /*========================================================================
  564. * External routines when attribute list is one block
  565. *========================================================================*/
  566. /*
  567. * Add a name to the leaf attribute list structure
  568. *
  569. * This leaf block cannot have a "remote" value, we only call this routine
  570. * if bmap_one_block() says there is only one block (ie: no remote blks).
  571. */
  572. STATIC int
  573. xfs_attr_leaf_addname(xfs_da_args_t *args)
  574. {
  575. xfs_inode_t *dp;
  576. struct xfs_buf *bp;
  577. int retval, error, committed, forkoff;
  578. trace_xfs_attr_leaf_addname(args);
  579. /*
  580. * Read the (only) block in the attribute list in.
  581. */
  582. dp = args->dp;
  583. args->blkno = 0;
  584. error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
  585. if (error)
  586. return error;
  587. /*
  588. * Look up the given attribute in the leaf block. Figure out if
  589. * the given flags produce an error or call for an atomic rename.
  590. */
  591. retval = xfs_attr3_leaf_lookup_int(bp, args);
  592. if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
  593. xfs_trans_brelse(args->trans, bp);
  594. return retval;
  595. } else if (retval == EEXIST) {
  596. if (args->flags & ATTR_CREATE) { /* pure create op */
  597. xfs_trans_brelse(args->trans, bp);
  598. return retval;
  599. }
  600. trace_xfs_attr_leaf_replace(args);
  601. args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
  602. args->blkno2 = args->blkno; /* set 2nd entry info*/
  603. args->index2 = args->index;
  604. args->rmtblkno2 = args->rmtblkno;
  605. args->rmtblkcnt2 = args->rmtblkcnt;
  606. }
  607. /*
  608. * Add the attribute to the leaf block, transitioning to a Btree
  609. * if required.
  610. */
  611. retval = xfs_attr3_leaf_add(bp, args);
  612. if (retval == ENOSPC) {
  613. /*
  614. * Promote the attribute list to the Btree format, then
  615. * Commit that transaction so that the node_addname() call
  616. * can manage its own transactions.
  617. */
  618. xfs_bmap_init(args->flist, args->firstblock);
  619. error = xfs_attr3_leaf_to_node(args);
  620. if (!error) {
  621. error = xfs_bmap_finish(&args->trans, args->flist,
  622. &committed);
  623. }
  624. if (error) {
  625. ASSERT(committed);
  626. args->trans = NULL;
  627. xfs_bmap_cancel(args->flist);
  628. return(error);
  629. }
  630. /*
  631. * bmap_finish() may have committed the last trans and started
  632. * a new one. We need the inode to be in all transactions.
  633. */
  634. if (committed)
  635. xfs_trans_ijoin(args->trans, dp, 0);
  636. /*
  637. * Commit the current trans (including the inode) and start
  638. * a new one.
  639. */
  640. error = xfs_trans_roll(&args->trans, dp);
  641. if (error)
  642. return (error);
  643. /*
  644. * Fob the whole rest of the problem off on the Btree code.
  645. */
  646. error = xfs_attr_node_addname(args);
  647. return(error);
  648. }
  649. /*
  650. * Commit the transaction that added the attr name so that
  651. * later routines can manage their own transactions.
  652. */
  653. error = xfs_trans_roll(&args->trans, dp);
  654. if (error)
  655. return (error);
  656. /*
  657. * If there was an out-of-line value, allocate the blocks we
  658. * identified for its storage and copy the value. This is done
  659. * after we create the attribute so that we don't overflow the
  660. * maximum size of a transaction and/or hit a deadlock.
  661. */
  662. if (args->rmtblkno > 0) {
  663. error = xfs_attr_rmtval_set(args);
  664. if (error)
  665. return(error);
  666. }
  667. /*
  668. * If this is an atomic rename operation, we must "flip" the
  669. * incomplete flags on the "new" and "old" attribute/value pairs
  670. * so that one disappears and one appears atomically. Then we
  671. * must remove the "old" attribute/value pair.
  672. */
  673. if (args->op_flags & XFS_DA_OP_RENAME) {
  674. /*
  675. * In a separate transaction, set the incomplete flag on the
  676. * "old" attr and clear the incomplete flag on the "new" attr.
  677. */
  678. error = xfs_attr3_leaf_flipflags(args);
  679. if (error)
  680. return(error);
  681. /*
  682. * Dismantle the "old" attribute/value pair by removing
  683. * a "remote" value (if it exists).
  684. */
  685. args->index = args->index2;
  686. args->blkno = args->blkno2;
  687. args->rmtblkno = args->rmtblkno2;
  688. args->rmtblkcnt = args->rmtblkcnt2;
  689. if (args->rmtblkno) {
  690. error = xfs_attr_rmtval_remove(args);
  691. if (error)
  692. return(error);
  693. }
  694. /*
  695. * Read in the block containing the "old" attr, then
  696. * remove the "old" attr from that block (neat, huh!)
  697. */
  698. error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
  699. -1, &bp);
  700. if (error)
  701. return error;
  702. xfs_attr3_leaf_remove(bp, args);
  703. /*
  704. * If the result is small enough, shrink it all into the inode.
  705. */
  706. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  707. xfs_bmap_init(args->flist, args->firstblock);
  708. error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
  709. /* bp is gone due to xfs_da_shrink_inode */
  710. if (!error) {
  711. error = xfs_bmap_finish(&args->trans,
  712. args->flist,
  713. &committed);
  714. }
  715. if (error) {
  716. ASSERT(committed);
  717. args->trans = NULL;
  718. xfs_bmap_cancel(args->flist);
  719. return(error);
  720. }
  721. /*
  722. * bmap_finish() may have committed the last trans
  723. * and started a new one. We need the inode to be
  724. * in all transactions.
  725. */
  726. if (committed)
  727. xfs_trans_ijoin(args->trans, dp, 0);
  728. }
  729. /*
  730. * Commit the remove and start the next trans in series.
  731. */
  732. error = xfs_trans_roll(&args->trans, dp);
  733. } else if (args->rmtblkno > 0) {
  734. /*
  735. * Added a "remote" value, just clear the incomplete flag.
  736. */
  737. error = xfs_attr3_leaf_clearflag(args);
  738. }
  739. return error;
  740. }
  741. /*
  742. * Remove a name from the leaf attribute list structure
  743. *
  744. * This leaf block cannot have a "remote" value, we only call this routine
  745. * if bmap_one_block() says there is only one block (ie: no remote blks).
  746. */
  747. STATIC int
  748. xfs_attr_leaf_removename(xfs_da_args_t *args)
  749. {
  750. xfs_inode_t *dp;
  751. struct xfs_buf *bp;
  752. int error, committed, forkoff;
  753. trace_xfs_attr_leaf_removename(args);
  754. /*
  755. * Remove the attribute.
  756. */
  757. dp = args->dp;
  758. args->blkno = 0;
  759. error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
  760. if (error)
  761. return error;
  762. error = xfs_attr3_leaf_lookup_int(bp, args);
  763. if (error == ENOATTR) {
  764. xfs_trans_brelse(args->trans, bp);
  765. return error;
  766. }
  767. xfs_attr3_leaf_remove(bp, args);
  768. /*
  769. * If the result is small enough, shrink it all into the inode.
  770. */
  771. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  772. xfs_bmap_init(args->flist, args->firstblock);
  773. error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
  774. /* bp is gone due to xfs_da_shrink_inode */
  775. if (!error) {
  776. error = xfs_bmap_finish(&args->trans, args->flist,
  777. &committed);
  778. }
  779. if (error) {
  780. ASSERT(committed);
  781. args->trans = NULL;
  782. xfs_bmap_cancel(args->flist);
  783. return error;
  784. }
  785. /*
  786. * bmap_finish() may have committed the last trans and started
  787. * a new one. We need the inode to be in all transactions.
  788. */
  789. if (committed)
  790. xfs_trans_ijoin(args->trans, dp, 0);
  791. }
  792. return 0;
  793. }
  794. /*
  795. * Look up a name in a leaf attribute list structure.
  796. *
  797. * This leaf block cannot have a "remote" value, we only call this routine
  798. * if bmap_one_block() says there is only one block (ie: no remote blks).
  799. */
  800. STATIC int
  801. xfs_attr_leaf_get(xfs_da_args_t *args)
  802. {
  803. struct xfs_buf *bp;
  804. int error;
  805. trace_xfs_attr_leaf_get(args);
  806. args->blkno = 0;
  807. error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
  808. if (error)
  809. return error;
  810. error = xfs_attr3_leaf_lookup_int(bp, args);
  811. if (error != EEXIST) {
  812. xfs_trans_brelse(args->trans, bp);
  813. return error;
  814. }
  815. error = xfs_attr3_leaf_getvalue(bp, args);
  816. xfs_trans_brelse(args->trans, bp);
  817. if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
  818. error = xfs_attr_rmtval_get(args);
  819. }
  820. return error;
  821. }
  822. /*========================================================================
  823. * External routines when attribute list size > XFS_LBSIZE(mp).
  824. *========================================================================*/
  825. /*
  826. * Add a name to a Btree-format attribute list.
  827. *
  828. * This will involve walking down the Btree, and may involve splitting
  829. * leaf nodes and even splitting intermediate nodes up to and including
  830. * the root node (a special case of an intermediate node).
  831. *
  832. * "Remote" attribute values confuse the issue and atomic rename operations
  833. * add a whole extra layer of confusion on top of that.
  834. */
  835. STATIC int
  836. xfs_attr_node_addname(xfs_da_args_t *args)
  837. {
  838. xfs_da_state_t *state;
  839. xfs_da_state_blk_t *blk;
  840. xfs_inode_t *dp;
  841. xfs_mount_t *mp;
  842. int committed, retval, error;
  843. trace_xfs_attr_node_addname(args);
  844. /*
  845. * Fill in bucket of arguments/results/context to carry around.
  846. */
  847. dp = args->dp;
  848. mp = dp->i_mount;
  849. restart:
  850. state = xfs_da_state_alloc();
  851. state->args = args;
  852. state->mp = mp;
  853. state->blocksize = state->mp->m_sb.sb_blocksize;
  854. state->node_ents = state->mp->m_attr_node_ents;
  855. /*
  856. * Search to see if name already exists, and get back a pointer
  857. * to where it should go.
  858. */
  859. error = xfs_da3_node_lookup_int(state, &retval);
  860. if (error)
  861. goto out;
  862. blk = &state->path.blk[ state->path.active-1 ];
  863. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  864. if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
  865. goto out;
  866. } else if (retval == EEXIST) {
  867. if (args->flags & ATTR_CREATE)
  868. goto out;
  869. trace_xfs_attr_node_replace(args);
  870. args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
  871. args->blkno2 = args->blkno; /* set 2nd entry info*/
  872. args->index2 = args->index;
  873. args->rmtblkno2 = args->rmtblkno;
  874. args->rmtblkcnt2 = args->rmtblkcnt;
  875. args->rmtblkno = 0;
  876. args->rmtblkcnt = 0;
  877. }
  878. retval = xfs_attr3_leaf_add(blk->bp, state->args);
  879. if (retval == ENOSPC) {
  880. if (state->path.active == 1) {
  881. /*
  882. * Its really a single leaf node, but it had
  883. * out-of-line values so it looked like it *might*
  884. * have been a b-tree.
  885. */
  886. xfs_da_state_free(state);
  887. state = NULL;
  888. xfs_bmap_init(args->flist, args->firstblock);
  889. error = xfs_attr3_leaf_to_node(args);
  890. if (!error) {
  891. error = xfs_bmap_finish(&args->trans,
  892. args->flist,
  893. &committed);
  894. }
  895. if (error) {
  896. ASSERT(committed);
  897. args->trans = NULL;
  898. xfs_bmap_cancel(args->flist);
  899. goto out;
  900. }
  901. /*
  902. * bmap_finish() may have committed the last trans
  903. * and started a new one. We need the inode to be
  904. * in all transactions.
  905. */
  906. if (committed)
  907. xfs_trans_ijoin(args->trans, dp, 0);
  908. /*
  909. * Commit the node conversion and start the next
  910. * trans in the chain.
  911. */
  912. error = xfs_trans_roll(&args->trans, dp);
  913. if (error)
  914. goto out;
  915. goto restart;
  916. }
  917. /*
  918. * Split as many Btree elements as required.
  919. * This code tracks the new and old attr's location
  920. * in the index/blkno/rmtblkno/rmtblkcnt fields and
  921. * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
  922. */
  923. xfs_bmap_init(args->flist, args->firstblock);
  924. error = xfs_da3_split(state);
  925. if (!error) {
  926. error = xfs_bmap_finish(&args->trans, args->flist,
  927. &committed);
  928. }
  929. if (error) {
  930. ASSERT(committed);
  931. args->trans = NULL;
  932. xfs_bmap_cancel(args->flist);
  933. goto out;
  934. }
  935. /*
  936. * bmap_finish() may have committed the last trans and started
  937. * a new one. We need the inode to be in all transactions.
  938. */
  939. if (committed)
  940. xfs_trans_ijoin(args->trans, dp, 0);
  941. } else {
  942. /*
  943. * Addition succeeded, update Btree hashvals.
  944. */
  945. xfs_da3_fixhashpath(state, &state->path);
  946. }
  947. /*
  948. * Kill the state structure, we're done with it and need to
  949. * allow the buffers to come back later.
  950. */
  951. xfs_da_state_free(state);
  952. state = NULL;
  953. /*
  954. * Commit the leaf addition or btree split and start the next
  955. * trans in the chain.
  956. */
  957. error = xfs_trans_roll(&args->trans, dp);
  958. if (error)
  959. goto out;
  960. /*
  961. * If there was an out-of-line value, allocate the blocks we
  962. * identified for its storage and copy the value. This is done
  963. * after we create the attribute so that we don't overflow the
  964. * maximum size of a transaction and/or hit a deadlock.
  965. */
  966. if (args->rmtblkno > 0) {
  967. error = xfs_attr_rmtval_set(args);
  968. if (error)
  969. return(error);
  970. }
  971. /*
  972. * If this is an atomic rename operation, we must "flip" the
  973. * incomplete flags on the "new" and "old" attribute/value pairs
  974. * so that one disappears and one appears atomically. Then we
  975. * must remove the "old" attribute/value pair.
  976. */
  977. if (args->op_flags & XFS_DA_OP_RENAME) {
  978. /*
  979. * In a separate transaction, set the incomplete flag on the
  980. * "old" attr and clear the incomplete flag on the "new" attr.
  981. */
  982. error = xfs_attr3_leaf_flipflags(args);
  983. if (error)
  984. goto out;
  985. /*
  986. * Dismantle the "old" attribute/value pair by removing
  987. * a "remote" value (if it exists).
  988. */
  989. args->index = args->index2;
  990. args->blkno = args->blkno2;
  991. args->rmtblkno = args->rmtblkno2;
  992. args->rmtblkcnt = args->rmtblkcnt2;
  993. if (args->rmtblkno) {
  994. error = xfs_attr_rmtval_remove(args);
  995. if (error)
  996. return(error);
  997. }
  998. /*
  999. * Re-find the "old" attribute entry after any split ops.
  1000. * The INCOMPLETE flag means that we will find the "old"
  1001. * attr, not the "new" one.
  1002. */
  1003. args->flags |= XFS_ATTR_INCOMPLETE;
  1004. state = xfs_da_state_alloc();
  1005. state->args = args;
  1006. state->mp = mp;
  1007. state->blocksize = state->mp->m_sb.sb_blocksize;
  1008. state->node_ents = state->mp->m_attr_node_ents;
  1009. state->inleaf = 0;
  1010. error = xfs_da3_node_lookup_int(state, &retval);
  1011. if (error)
  1012. goto out;
  1013. /*
  1014. * Remove the name and update the hashvals in the tree.
  1015. */
  1016. blk = &state->path.blk[ state->path.active-1 ];
  1017. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1018. error = xfs_attr3_leaf_remove(blk->bp, args);
  1019. xfs_da3_fixhashpath(state, &state->path);
  1020. /*
  1021. * Check to see if the tree needs to be collapsed.
  1022. */
  1023. if (retval && (state->path.active > 1)) {
  1024. xfs_bmap_init(args->flist, args->firstblock);
  1025. error = xfs_da3_join(state);
  1026. if (!error) {
  1027. error = xfs_bmap_finish(&args->trans,
  1028. args->flist,
  1029. &committed);
  1030. }
  1031. if (error) {
  1032. ASSERT(committed);
  1033. args->trans = NULL;
  1034. xfs_bmap_cancel(args->flist);
  1035. goto out;
  1036. }
  1037. /*
  1038. * bmap_finish() may have committed the last trans
  1039. * and started a new one. We need the inode to be
  1040. * in all transactions.
  1041. */
  1042. if (committed)
  1043. xfs_trans_ijoin(args->trans, dp, 0);
  1044. }
  1045. /*
  1046. * Commit and start the next trans in the chain.
  1047. */
  1048. error = xfs_trans_roll(&args->trans, dp);
  1049. if (error)
  1050. goto out;
  1051. } else if (args->rmtblkno > 0) {
  1052. /*
  1053. * Added a "remote" value, just clear the incomplete flag.
  1054. */
  1055. error = xfs_attr3_leaf_clearflag(args);
  1056. if (error)
  1057. goto out;
  1058. }
  1059. retval = error = 0;
  1060. out:
  1061. if (state)
  1062. xfs_da_state_free(state);
  1063. if (error)
  1064. return(error);
  1065. return(retval);
  1066. }
  1067. /*
  1068. * Remove a name from a B-tree attribute list.
  1069. *
  1070. * This will involve walking down the Btree, and may involve joining
  1071. * leaf nodes and even joining intermediate nodes up to and including
  1072. * the root node (a special case of an intermediate node).
  1073. */
  1074. STATIC int
  1075. xfs_attr_node_removename(xfs_da_args_t *args)
  1076. {
  1077. xfs_da_state_t *state;
  1078. xfs_da_state_blk_t *blk;
  1079. xfs_inode_t *dp;
  1080. struct xfs_buf *bp;
  1081. int retval, error, committed, forkoff;
  1082. trace_xfs_attr_node_removename(args);
  1083. /*
  1084. * Tie a string around our finger to remind us where we are.
  1085. */
  1086. dp = args->dp;
  1087. state = xfs_da_state_alloc();
  1088. state->args = args;
  1089. state->mp = dp->i_mount;
  1090. state->blocksize = state->mp->m_sb.sb_blocksize;
  1091. state->node_ents = state->mp->m_attr_node_ents;
  1092. /*
  1093. * Search to see if name exists, and get back a pointer to it.
  1094. */
  1095. error = xfs_da3_node_lookup_int(state, &retval);
  1096. if (error || (retval != EEXIST)) {
  1097. if (error == 0)
  1098. error = retval;
  1099. goto out;
  1100. }
  1101. /*
  1102. * If there is an out-of-line value, de-allocate the blocks.
  1103. * This is done before we remove the attribute so that we don't
  1104. * overflow the maximum size of a transaction and/or hit a deadlock.
  1105. */
  1106. blk = &state->path.blk[ state->path.active-1 ];
  1107. ASSERT(blk->bp != NULL);
  1108. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1109. if (args->rmtblkno > 0) {
  1110. /*
  1111. * Fill in disk block numbers in the state structure
  1112. * so that we can get the buffers back after we commit
  1113. * several transactions in the following calls.
  1114. */
  1115. error = xfs_attr_fillstate(state);
  1116. if (error)
  1117. goto out;
  1118. /*
  1119. * Mark the attribute as INCOMPLETE, then bunmapi() the
  1120. * remote value.
  1121. */
  1122. error = xfs_attr3_leaf_setflag(args);
  1123. if (error)
  1124. goto out;
  1125. error = xfs_attr_rmtval_remove(args);
  1126. if (error)
  1127. goto out;
  1128. /*
  1129. * Refill the state structure with buffers, the prior calls
  1130. * released our buffers.
  1131. */
  1132. error = xfs_attr_refillstate(state);
  1133. if (error)
  1134. goto out;
  1135. }
  1136. /*
  1137. * Remove the name and update the hashvals in the tree.
  1138. */
  1139. blk = &state->path.blk[ state->path.active-1 ];
  1140. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1141. retval = xfs_attr3_leaf_remove(blk->bp, args);
  1142. xfs_da3_fixhashpath(state, &state->path);
  1143. /*
  1144. * Check to see if the tree needs to be collapsed.
  1145. */
  1146. if (retval && (state->path.active > 1)) {
  1147. xfs_bmap_init(args->flist, args->firstblock);
  1148. error = xfs_da3_join(state);
  1149. if (!error) {
  1150. error = xfs_bmap_finish(&args->trans, args->flist,
  1151. &committed);
  1152. }
  1153. if (error) {
  1154. ASSERT(committed);
  1155. args->trans = NULL;
  1156. xfs_bmap_cancel(args->flist);
  1157. goto out;
  1158. }
  1159. /*
  1160. * bmap_finish() may have committed the last trans and started
  1161. * a new one. We need the inode to be in all transactions.
  1162. */
  1163. if (committed)
  1164. xfs_trans_ijoin(args->trans, dp, 0);
  1165. /*
  1166. * Commit the Btree join operation and start a new trans.
  1167. */
  1168. error = xfs_trans_roll(&args->trans, dp);
  1169. if (error)
  1170. goto out;
  1171. }
  1172. /*
  1173. * If the result is small enough, push it all into the inode.
  1174. */
  1175. if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  1176. /*
  1177. * Have to get rid of the copy of this dabuf in the state.
  1178. */
  1179. ASSERT(state->path.active == 1);
  1180. ASSERT(state->path.blk[0].bp);
  1181. state->path.blk[0].bp = NULL;
  1182. error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
  1183. if (error)
  1184. goto out;
  1185. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  1186. xfs_bmap_init(args->flist, args->firstblock);
  1187. error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
  1188. /* bp is gone due to xfs_da_shrink_inode */
  1189. if (!error) {
  1190. error = xfs_bmap_finish(&args->trans,
  1191. args->flist,
  1192. &committed);
  1193. }
  1194. if (error) {
  1195. ASSERT(committed);
  1196. args->trans = NULL;
  1197. xfs_bmap_cancel(args->flist);
  1198. goto out;
  1199. }
  1200. /*
  1201. * bmap_finish() may have committed the last trans
  1202. * and started a new one. We need the inode to be
  1203. * in all transactions.
  1204. */
  1205. if (committed)
  1206. xfs_trans_ijoin(args->trans, dp, 0);
  1207. } else
  1208. xfs_trans_brelse(args->trans, bp);
  1209. }
  1210. error = 0;
  1211. out:
  1212. xfs_da_state_free(state);
  1213. return(error);
  1214. }
  1215. /*
  1216. * Fill in the disk block numbers in the state structure for the buffers
  1217. * that are attached to the state structure.
  1218. * This is done so that we can quickly reattach ourselves to those buffers
  1219. * after some set of transaction commits have released these buffers.
  1220. */
  1221. STATIC int
  1222. xfs_attr_fillstate(xfs_da_state_t *state)
  1223. {
  1224. xfs_da_state_path_t *path;
  1225. xfs_da_state_blk_t *blk;
  1226. int level;
  1227. trace_xfs_attr_fillstate(state->args);
  1228. /*
  1229. * Roll down the "path" in the state structure, storing the on-disk
  1230. * block number for those buffers in the "path".
  1231. */
  1232. path = &state->path;
  1233. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1234. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1235. if (blk->bp) {
  1236. blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
  1237. blk->bp = NULL;
  1238. } else {
  1239. blk->disk_blkno = 0;
  1240. }
  1241. }
  1242. /*
  1243. * Roll down the "altpath" in the state structure, storing the on-disk
  1244. * block number for those buffers in the "altpath".
  1245. */
  1246. path = &state->altpath;
  1247. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1248. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1249. if (blk->bp) {
  1250. blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
  1251. blk->bp = NULL;
  1252. } else {
  1253. blk->disk_blkno = 0;
  1254. }
  1255. }
  1256. return(0);
  1257. }
  1258. /*
  1259. * Reattach the buffers to the state structure based on the disk block
  1260. * numbers stored in the state structure.
  1261. * This is done after some set of transaction commits have released those
  1262. * buffers from our grip.
  1263. */
  1264. STATIC int
  1265. xfs_attr_refillstate(xfs_da_state_t *state)
  1266. {
  1267. xfs_da_state_path_t *path;
  1268. xfs_da_state_blk_t *blk;
  1269. int level, error;
  1270. trace_xfs_attr_refillstate(state->args);
  1271. /*
  1272. * Roll down the "path" in the state structure, storing the on-disk
  1273. * block number for those buffers in the "path".
  1274. */
  1275. path = &state->path;
  1276. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1277. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1278. if (blk->disk_blkno) {
  1279. error = xfs_da3_node_read(state->args->trans,
  1280. state->args->dp,
  1281. blk->blkno, blk->disk_blkno,
  1282. &blk->bp, XFS_ATTR_FORK);
  1283. if (error)
  1284. return(error);
  1285. } else {
  1286. blk->bp = NULL;
  1287. }
  1288. }
  1289. /*
  1290. * Roll down the "altpath" in the state structure, storing the on-disk
  1291. * block number for those buffers in the "altpath".
  1292. */
  1293. path = &state->altpath;
  1294. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1295. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1296. if (blk->disk_blkno) {
  1297. error = xfs_da3_node_read(state->args->trans,
  1298. state->args->dp,
  1299. blk->blkno, blk->disk_blkno,
  1300. &blk->bp, XFS_ATTR_FORK);
  1301. if (error)
  1302. return(error);
  1303. } else {
  1304. blk->bp = NULL;
  1305. }
  1306. }
  1307. return(0);
  1308. }
  1309. /*
  1310. * Look up a filename in a node attribute list.
  1311. *
  1312. * This routine gets called for any attribute fork that has more than one
  1313. * block, ie: both true Btree attr lists and for single-leaf-blocks with
  1314. * "remote" values taking up more blocks.
  1315. */
  1316. STATIC int
  1317. xfs_attr_node_get(xfs_da_args_t *args)
  1318. {
  1319. xfs_da_state_t *state;
  1320. xfs_da_state_blk_t *blk;
  1321. int error, retval;
  1322. int i;
  1323. trace_xfs_attr_node_get(args);
  1324. state = xfs_da_state_alloc();
  1325. state->args = args;
  1326. state->mp = args->dp->i_mount;
  1327. state->blocksize = state->mp->m_sb.sb_blocksize;
  1328. state->node_ents = state->mp->m_attr_node_ents;
  1329. /*
  1330. * Search to see if name exists, and get back a pointer to it.
  1331. */
  1332. error = xfs_da3_node_lookup_int(state, &retval);
  1333. if (error) {
  1334. retval = error;
  1335. } else if (retval == EEXIST) {
  1336. blk = &state->path.blk[ state->path.active-1 ];
  1337. ASSERT(blk->bp != NULL);
  1338. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1339. /*
  1340. * Get the value, local or "remote"
  1341. */
  1342. retval = xfs_attr3_leaf_getvalue(blk->bp, args);
  1343. if (!retval && (args->rmtblkno > 0)
  1344. && !(args->flags & ATTR_KERNOVAL)) {
  1345. retval = xfs_attr_rmtval_get(args);
  1346. }
  1347. }
  1348. /*
  1349. * If not in a transaction, we have to release all the buffers.
  1350. */
  1351. for (i = 0; i < state->path.active; i++) {
  1352. xfs_trans_brelse(args->trans, state->path.blk[i].bp);
  1353. state->path.blk[i].bp = NULL;
  1354. }
  1355. xfs_da_state_free(state);
  1356. return(retval);
  1357. }