xfs_attr.c 38 KB

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